comparison static/bootstrap/js/bootstrap-affix.js @ 61:130eccd396d8

Now using Boostrap with LESS.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 06 Feb 2013 08:22:31 -0800
parents
children a5a3d454eac9
comparison
equal deleted inserted replaced
60:8250c977bc50 61:130eccd396d8
1 /* ==========================================================
2 * bootstrap-affix.js v2.2.2
3 * http://twitter.github.com/bootstrap/javascript.html#affix
4 * ==========================================================
5 * Copyright 2012 Twitter, Inc.
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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 * ========================================================== */
19
20
21 !function ($) {
22
23 "use strict"; // jshint ;_;
24
25
26 /* AFFIX CLASS DEFINITION
27 * ====================== */
28
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)
35 this.checkPosition()
36 }
37
38 Affix.prototype.checkPosition = function () {
39 if (!this.$element.is(':visible')) return
40
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'
48 , affix
49
50 if (typeof offset != 'object') offsetBottom = offsetTop = offset
51 if (typeof offsetTop == 'function') offsetTop = offset.top()
52 if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
53
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 ?
57 'top' : false
58
59 if (this.affixed === affix) return
60
61 this.affixed = affix
62 this.unpin = affix == 'bottom' ? position.top - scrollTop : null
63
64 this.$element.removeClass(reset).addClass('affix' + (affix ? '-' + affix : ''))
65 }
66
67
68 /* AFFIX PLUGIN DEFINITION
69 * ======================= */
70
71 var old = $.fn.affix
72
73 $.fn.affix = function (option) {
74 return this.each(function () {
75 var $this = $(this)
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]()
80 })
81 }
82
83 $.fn.affix.Constructor = Affix
84
85 $.fn.affix.defaults = {
86 offset: 0
87 }
88
89
90 /* AFFIX NO CONFLICT
91 * ================= */
92
93 $.fn.affix.noConflict = function () {
94 $.fn.affix = old
95 return this
96 }
97
98
99 /* AFFIX DATA-API
100 * ============== */
101
102 $(window).on('load', function () {
103 $('[data-spy="affix"]').each(function () {
104 var $spy = $(this)
105 , data = $spy.data()
106
107 data.offset = data.offset || {}
108
109 data.offsetBottom && (data.offset.bottom = data.offsetBottom)
110 data.offsetTop && (data.offset.top = data.offsetTop)
111
112 $spy.affix(data)
113 })
114 })
115
116
117 }(window.jQuery);