1 /* ==========================================================
2 * bootstrap-affix.js v2.2.2
3 * http://twitter.github.com/bootstrap/javascript.html#affix
4 * ==========================================================
5 * Copyright 2012 Twitter, Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================================================== */
23 "use strict"; // jshint ;_;
26 /* AFFIX CLASS DEFINITION
27 * ====================== */
29 var Affix = function (element, options) {
30 this.options = $.extend({}, $.fn.affix.defaults, options)
31 this.$window = $(window)
32 .on('scroll.affix.data-api', $.proxy(this.checkPosition, this))
33 .on('click.affix.data-api', $.proxy(function () { setTimeout($.proxy(this.checkPosition, this), 1) }, this))
34 this.$element = $(element)
38 Affix.prototype.checkPosition = function () {
39 if (!this.$element.is(':visible')) return
41 var scrollHeight = $(document).height()
42 , scrollTop = this.$window.scrollTop()
43 , position = this.$element.offset()
44 , offset = this.options.offset
45 , offsetBottom = offset.bottom
46 , offsetTop = offset.top
47 , reset = 'affix affix-top affix-bottom'
50 if (typeof offset != 'object') offsetBottom = offsetTop = offset
51 if (typeof offsetTop == 'function') offsetTop = offset.top()
52 if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
54 affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ?
55 false : offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ?
56 'bottom' : offsetTop != null && scrollTop <= offsetTop ?
59 if (this.affixed === affix) return
62 this.unpin = affix == 'bottom' ? position.top - scrollTop : null
64 this.$element.removeClass(reset).addClass('affix' + (affix ? '-' + affix : ''))
68 /* AFFIX PLUGIN DEFINITION
69 * ======================= */
73 $.fn.affix = function (option) {
74 return this.each(function () {
76 , data = $this.data('affix')
77 , options = typeof option == 'object' && option
78 if (!data) $this.data('affix', (data = new Affix(this, options)))
79 if (typeof option == 'string') data[option]()
83 $.fn.affix.Constructor = Affix
85 $.fn.affix.defaults = {
91 * ================= */
93 $.fn.affix.noConflict = function () {
102 $(window).on('load', function () {
103 $('[data-spy="affix"]').each(function () {
107 data.offset = data.offset || {}
109 data.offsetBottom && (data.offset.bottom = data.offsetBottom)
110 data.offsetTop && (data.offset.top = data.offsetTop)