|
1 |
| -/* ========================================================== |
2 |
| - * bootstrap-affix.js v2.3.2 |
3 |
| - * http://getbootstrap.com/2.3.2/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 |
| - * ====================== */ |
| 1 | +/* ======================================================================== |
| 2 | + * Bootstrap: affix.js v3.2.0 |
| 3 | + * http://getbootstrap.com/javascript/#affix |
| 4 | + * ======================================================================== |
| 5 | + * Copyright 2011-2014 Twitter, Inc. |
| 6 | + * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) |
| 7 | + * ======================================================================== */ |
| 8 | + |
| 9 | + |
| 10 | ++function ($) { |
| 11 | + 'use strict'; |
| 12 | + |
| 13 | + // AFFIX CLASS DEFINITION |
| 14 | + // ====================== |
28 | 15 |
|
29 | 16 | 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) |
| 17 | + this.options = $.extend({}, Affix.DEFAULTS, options) |
| 18 | + |
| 19 | + this.$target = $(this.options.target) |
| 20 | + .on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this)) |
| 21 | + .on('click.bs.affix.data-api', $.proxy(this.checkPositionWithEventLoop, this)) |
| 22 | + |
| 23 | + this.$element = $(element) |
| 24 | + this.affixed = |
| 25 | + this.unpin = |
| 26 | + this.pinnedOffset = null |
| 27 | + |
35 | 28 | this.checkPosition()
|
36 | 29 | }
|
37 | 30 |
|
| 31 | + Affix.VERSION = '3.2.0' |
| 32 | + |
| 33 | + Affix.RESET = 'affix affix-top affix-bottom' |
| 34 | + |
| 35 | + Affix.DEFAULTS = { |
| 36 | + offset: 0, |
| 37 | + target: window |
| 38 | + } |
| 39 | + |
| 40 | + Affix.prototype.getPinnedOffset = function () { |
| 41 | + if (this.pinnedOffset) return this.pinnedOffset |
| 42 | + this.$element.removeClass(Affix.RESET).addClass('affix') |
| 43 | + var scrollTop = this.$target.scrollTop() |
| 44 | + var position = this.$element.offset() |
| 45 | + return (this.pinnedOffset = position.top - scrollTop) |
| 46 | + } |
| 47 | + |
| 48 | + Affix.prototype.checkPositionWithEventLoop = function () { |
| 49 | + setTimeout($.proxy(this.checkPosition, this), 1) |
| 50 | + } |
| 51 | + |
38 | 52 | Affix.prototype.checkPosition = function () {
|
39 | 53 | if (!this.$element.is(':visible')) return
|
40 | 54 |
|
41 | 55 | 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 |
| 56 | + var scrollTop = this.$target.scrollTop() |
| 57 | + var position = this.$element.offset() |
| 58 | + var offset = this.options.offset |
| 59 | + var offsetTop = offset.top |
| 60 | + var offsetBottom = offset.bottom |
| 61 | + |
| 62 | + if (typeof offset != 'object') offsetBottom = offsetTop = offset |
| 63 | + if (typeof offsetTop == 'function') offsetTop = offset.top(this.$element) |
| 64 | + if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element) |
| 65 | + |
| 66 | + var affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ? false : |
| 67 | + offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ? 'bottom' : |
| 68 | + offsetTop != null && (scrollTop <= offsetTop) ? 'top' : false |
58 | 69 |
|
59 | 70 | if (this.affixed === affix) return
|
| 71 | + if (this.unpin != null) this.$element.css('top', '') |
60 | 72 |
|
61 |
| - this.affixed = affix |
62 |
| - this.unpin = affix == 'bottom' ? position.top - scrollTop : null |
| 73 | + var affixType = 'affix' + (affix ? '-' + affix : '') |
| 74 | + var e = $.Event(affixType + '.bs.affix') |
63 | 75 |
|
64 |
| - this.$element.removeClass(reset).addClass('affix' + (affix ? '-' + affix : '')) |
65 |
| - } |
| 76 | + this.$element.trigger(e) |
66 | 77 |
|
| 78 | + if (e.isDefaultPrevented()) return |
67 | 79 |
|
68 |
| - /* AFFIX PLUGIN DEFINITION |
69 |
| - * ======================= */ |
| 80 | + this.affixed = affix |
| 81 | + this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null |
| 82 | + |
| 83 | + this.$element |
| 84 | + .removeClass(Affix.RESET) |
| 85 | + .addClass(affixType) |
| 86 | + .trigger($.Event(affixType.replace('affix', 'affixed'))) |
| 87 | + |
| 88 | + if (affix == 'bottom') { |
| 89 | + this.$element.offset({ |
| 90 | + top: scrollHeight - this.$element.height() - offsetBottom |
| 91 | + }) |
| 92 | + } |
| 93 | + } |
70 | 94 |
|
71 |
| - var old = $.fn.affix |
72 | 95 |
|
73 |
| - $.fn.affix = function (option) { |
| 96 | + // AFFIX PLUGIN DEFINITION |
| 97 | + // ======================= |
| 98 | + |
| 99 | + function Plugin(option) { |
74 | 100 | 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))) |
| 101 | + var $this = $(this) |
| 102 | + var data = $this.data('bs.affix') |
| 103 | + var options = typeof option == 'object' && option |
| 104 | + |
| 105 | + if (!data) $this.data('bs.affix', (data = new Affix(this, options))) |
79 | 106 | if (typeof option == 'string') data[option]()
|
80 | 107 | })
|
81 | 108 | }
|
82 | 109 |
|
83 |
| - $.fn.affix.Constructor = Affix |
| 110 | + var old = $.fn.affix |
84 | 111 |
|
85 |
| - $.fn.affix.defaults = { |
86 |
| - offset: 0 |
87 |
| - } |
| 112 | + $.fn.affix = Plugin |
| 113 | + $.fn.affix.Constructor = Affix |
88 | 114 |
|
89 | 115 |
|
90 |
| - /* AFFIX NO CONFLICT |
91 |
| - * ================= */ |
| 116 | + // AFFIX NO CONFLICT |
| 117 | + // ================= |
92 | 118 |
|
93 | 119 | $.fn.affix.noConflict = function () {
|
94 | 120 | $.fn.affix = old
|
95 | 121 | return this
|
96 | 122 | }
|
97 | 123 |
|
98 | 124 |
|
99 |
| - /* AFFIX DATA-API |
100 |
| - * ============== */ |
| 125 | + // AFFIX DATA-API |
| 126 | + // ============== |
101 | 127 |
|
102 | 128 | $(window).on('load', function () {
|
103 | 129 | $('[data-spy="affix"]').each(function () {
|
104 | 130 | var $spy = $(this)
|
105 |
| - , data = $spy.data() |
| 131 | + var data = $spy.data() |
106 | 132 |
|
107 | 133 | data.offset = data.offset || {}
|
108 | 134 |
|
109 |
| - data.offsetBottom && (data.offset.bottom = data.offsetBottom) |
110 |
| - data.offsetTop && (data.offset.top = data.offsetTop) |
| 135 | + if (data.offsetBottom) data.offset.bottom = data.offsetBottom |
| 136 | + if (data.offsetTop) data.offset.top = data.offsetTop |
111 | 137 |
|
112 |
| - $spy.affix(data) |
| 138 | + Plugin.call($spy, data) |
113 | 139 | })
|
114 | 140 | })
|
115 | 141 |
|
116 |
| - |
117 |
| -}(window.jQuery); |
| 142 | +}(jQuery); |
0 commit comments