annotate static/bootstrap/js/affix.js @ 149:d29e2f337b00

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