annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
61
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 /* ==========================================================
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 * bootstrap-affix.js v2.2.2
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 * http://twitter.github.com/bootstrap/javascript.html#affix
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 * ==========================================================
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 * Copyright 2012 Twitter, Inc.
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 *
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 * Licensed under the Apache License, Version 2.0 (the "License");
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 * you may not use this file except in compliance with the License.
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 * You may obtain a copy of the License at
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 *
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 * http://www.apache.org/licenses/LICENSE-2.0
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 *
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 * Unless required by applicable law or agreed to in writing, software
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 * distributed under the License is distributed on an "AS IS" BASIS,
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 * See the License for the specific language governing permissions and
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 * limitations under the License.
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 * ========================================================== */
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 !function ($) {
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 "use strict"; // jshint ;_;
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 /* AFFIX CLASS DEFINITION
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 * ====================== */
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 var Affix = function (element, options) {
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 this.options = $.extend({}, $.fn.affix.defaults, options)
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 this.$window = $(window)
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 .on('scroll.affix.data-api', $.proxy(this.checkPosition, this))
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 .on('click.affix.data-api', $.proxy(function () { setTimeout($.proxy(this.checkPosition, this), 1) }, this))
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 this.$element = $(element)
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 this.checkPosition()
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 }
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 Affix.prototype.checkPosition = function () {
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 if (!this.$element.is(':visible')) return
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 var scrollHeight = $(document).height()
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 , scrollTop = this.$window.scrollTop()
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 , position = this.$element.offset()
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 , offset = this.options.offset
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 , offsetBottom = offset.bottom
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 , offsetTop = offset.top
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 , reset = 'affix affix-top affix-bottom'
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 , affix
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50 if (typeof offset != 'object') offsetBottom = offsetTop = offset
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
51 if (typeof offsetTop == 'function') offsetTop = offset.top()
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
52 if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
53
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
54 affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ?
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
55 false : offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ?
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
56 'bottom' : offsetTop != null && scrollTop <= offsetTop ?
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
57 'top' : false
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
58
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
59 if (this.affixed === affix) return
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
60
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 this.affixed = affix
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
62 this.unpin = affix == 'bottom' ? position.top - scrollTop : null
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 this.$element.removeClass(reset).addClass('affix' + (affix ? '-' + affix : ''))
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 }
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68 /* AFFIX PLUGIN DEFINITION
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
69 * ======================= */
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
70
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
71 var old = $.fn.affix
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
73 $.fn.affix = function (option) {
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 return this.each(function () {
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 var $this = $(this)
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 , data = $this.data('affix')
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 , options = typeof option == 'object' && option
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 if (!data) $this.data('affix', (data = new Affix(this, options)))
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79 if (typeof option == 'string') data[option]()
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 })
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 }
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 $.fn.affix.Constructor = Affix
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 $.fn.affix.defaults = {
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 offset: 0
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 }
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 /* AFFIX NO CONFLICT
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 * ================= */
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 $.fn.affix.noConflict = function () {
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94 $.fn.affix = old
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 return this
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96 }
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 /* AFFIX DATA-API
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 * ============== */
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 $(window).on('load', function () {
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 $('[data-spy="affix"]').each(function () {
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104 var $spy = $(this)
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 , data = $spy.data()
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 data.offset = data.offset || {}
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 data.offsetBottom && (data.offset.bottom = data.offsetBottom)
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 data.offsetTop && (data.offset.top = data.offsetTop)
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112 $spy.affix(data)
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 })
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 })
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
116
130eccd396d8 Now using Boostrap with LESS.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 }(window.jQuery);