1
0
Fork 0
mirror of https://github.com/twbs/bootstrap.git synced 2022-11-09 12:25:43 -05:00
twbs--bootstrap/js/affix.js

127 lines
3.8 KiB
JavaScript
Raw Normal View History

2013-05-21 22:30:33 -04:00
/* ========================================================================
2013-05-16 14:06:30 -04:00
* Bootstrap: affix.js v3.0.0
2013-07-25 15:24:13 -04:00
* http://twbs.github.com/bootstrap/javascript.html#affix
2013-05-21 22:30:33 -04:00
* ========================================================================
2013-10-22 12:41:33 -04:00
* Copyright 2013 Twitter, Inc.
2012-07-22 21:28:39 -04:00
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2013-05-21 22:30:33 -04:00
* ======================================================================== */
2012-07-22 21:28:39 -04:00
2013-05-21 22:30:33 -04:00
+function ($) { "use strict";
2012-07-22 21:28:39 -04:00
2013-05-16 14:06:30 -04:00
// AFFIX CLASS DEFINITION
// ======================
2012-07-22 21:28:39 -04:00
var Affix = function (element, options) {
2013-05-16 14:06:30 -04:00
this.options = $.extend({}, Affix.DEFAULTS, options)
this.$window = $(window)
.on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
.on('click.bs.affix.data-api', $.proxy(this.checkPositionWithEventLoop, this))
2013-05-16 14:06:30 -04:00
2012-07-22 21:28:39 -04:00
this.$element = $(element)
2013-05-16 14:06:30 -04:00
this.affixed =
this.unpin = null
2012-08-15 00:23:29 -04:00
this.checkPosition()
2012-07-22 21:28:39 -04:00
}
2013-07-26 02:30:13 -04:00
Affix.RESET = 'affix affix-top affix-bottom'
2013-05-16 14:06:30 -04:00
Affix.DEFAULTS = {
offset: 0
}
Affix.prototype.checkPositionWithEventLoop = function () {
setTimeout($.proxy(this.checkPosition, this), 1)
}
2012-07-22 21:28:39 -04:00
Affix.prototype.checkPosition = function () {
if (!this.$element.is(':visible')) return
var scrollHeight = $(document).height()
2013-05-16 14:06:30 -04:00
var scrollTop = this.$window.scrollTop()
var position = this.$element.offset()
var offset = this.options.offset
var offsetTop = offset.top
var offsetBottom = offset.bottom
if (typeof offset != 'object') offsetBottom = offsetTop = offset
if (typeof offsetTop == 'function') offsetTop = offset.top()
if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
2012-07-22 21:28:39 -04:00
2013-05-16 14:06:30 -04:00
var affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ? false :
offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ? 'bottom' :
offsetTop != null && (scrollTop <= offsetTop) ? 'top' : false
2012-07-22 21:28:39 -04:00
if (this.affixed === affix) return
2013-07-26 02:30:13 -04:00
if (this.unpin) this.$element.css('top', '')
2012-07-22 21:28:39 -04:00
this.affixed = affix
2013-05-16 14:06:30 -04:00
this.unpin = affix == 'bottom' ? position.top - scrollTop : null
2012-07-22 21:28:39 -04:00
2013-07-26 02:30:13 -04:00
this.$element.removeClass(Affix.RESET).addClass('affix' + (affix ? '-' + affix : ''))
if (affix == 'bottom') {
this.$element.offset({ top: document.body.offsetHeight - offsetBottom - this.$element.height() })
}
2012-07-22 21:28:39 -04:00
}
2013-05-16 14:06:30 -04:00
// AFFIX PLUGIN DEFINITION
// =======================
2012-07-22 21:28:39 -04:00
var old = $.fn.affix
2012-07-22 21:28:39 -04:00
$.fn.affix = function (option) {
return this.each(function () {
2013-05-16 14:06:30 -04:00
var $this = $(this)
var data = $this.data('bs.affix')
2013-05-16 14:06:30 -04:00
var options = typeof option == 'object' && option
if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
2012-07-22 21:28:39 -04:00
if (typeof option == 'string') data[option]()
})
}
$.fn.affix.Constructor = Affix
2013-05-16 14:06:30 -04:00
// AFFIX NO CONFLICT
// =================
$.fn.affix.noConflict = function () {
$.fn.affix = old
return this
}
2013-05-16 14:06:30 -04:00
// AFFIX DATA-API
// ==============
2012-07-22 21:28:39 -04:00
$(window).on('load', function () {
2012-07-22 21:28:39 -04:00
$('[data-spy="affix"]').each(function () {
var $spy = $(this)
2013-05-16 14:06:30 -04:00
var data = $spy.data()
2012-07-22 21:28:39 -04:00
data.offset = data.offset || {}
2013-05-16 14:06:30 -04:00
if (data.offsetBottom) data.offset.bottom = data.offsetBottom
if (data.offsetTop) data.offset.top = data.offsetTop
2012-07-22 21:28:39 -04:00
$spy.affix(data)
})
})
}(window.jQuery);