1 | /* ========================================================================
|
---|
2 | * Bootstrap: affix.js v3.4.1
|
---|
3 | * https://getbootstrap.com/docs/3.4/javascript/#affix
|
---|
4 | * ========================================================================
|
---|
5 | * Copyright 2011-2019 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 | // ======================
|
---|
15 |
|
---|
16 | var Affix = function (element, options) {
|
---|
17 | this.options = $.extend({}, Affix.DEFAULTS, options)
|
---|
18 |
|
---|
19 | var target = this.options.target === Affix.DEFAULTS.target ? $(this.options.target) : $(document).find(this.options.target)
|
---|
20 |
|
---|
21 | this.$target = target
|
---|
22 | .on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
|
---|
23 | .on('click.bs.affix.data-api', $.proxy(this.checkPositionWithEventLoop, this))
|
---|
24 |
|
---|
25 | this.$element = $(element)
|
---|
26 | this.affixed = null
|
---|
27 | this.unpin = null
|
---|
28 | this.pinnedOffset = null
|
---|
29 |
|
---|
30 | this.checkPosition()
|
---|
31 | }
|
---|
32 |
|
---|
33 | Affix.VERSION = '3.4.1'
|
---|
34 |
|
---|
35 | Affix.RESET = 'affix affix-top affix-bottom'
|
---|
36 |
|
---|
37 | Affix.DEFAULTS = {
|
---|
38 | offset: 0,
|
---|
39 | target: window
|
---|
40 | }
|
---|
41 |
|
---|
42 | Affix.prototype.getState = function (scrollHeight, height, offsetTop, offsetBottom) {
|
---|
43 | var scrollTop = this.$target.scrollTop()
|
---|
44 | var position = this.$element.offset()
|
---|
45 | var targetHeight = this.$target.height()
|
---|
46 |
|
---|
47 | if (offsetTop != null && this.affixed == 'top') return scrollTop < offsetTop ? 'top' : false
|
---|
48 |
|
---|
49 | if (this.affixed == 'bottom') {
|
---|
50 | if (offsetTop != null) return (scrollTop + this.unpin <= position.top) ? false : 'bottom'
|
---|
51 | return (scrollTop + targetHeight <= scrollHeight - offsetBottom) ? false : 'bottom'
|
---|
52 | }
|
---|
53 |
|
---|
54 | var initializing = this.affixed == null
|
---|
55 | var colliderTop = initializing ? scrollTop : position.top
|
---|
56 | var colliderHeight = initializing ? targetHeight : height
|
---|
57 |
|
---|
58 | if (offsetTop != null && scrollTop <= offsetTop) return 'top'
|
---|
59 | if (offsetBottom != null && (colliderTop + colliderHeight >= scrollHeight - offsetBottom)) return 'bottom'
|
---|
60 |
|
---|
61 | return false
|
---|
62 | }
|
---|
63 |
|
---|
64 | Affix.prototype.getPinnedOffset = function () {
|
---|
65 | if (this.pinnedOffset) return this.pinnedOffset
|
---|
66 | this.$element.removeClass(Affix.RESET).addClass('affix')
|
---|
67 | var scrollTop = this.$target.scrollTop()
|
---|
68 | var position = this.$element.offset()
|
---|
69 | return (this.pinnedOffset = position.top - scrollTop)
|
---|
70 | }
|
---|
71 |
|
---|
72 | Affix.prototype.checkPositionWithEventLoop = function () {
|
---|
73 | setTimeout($.proxy(this.checkPosition, this), 1)
|
---|
74 | }
|
---|
75 |
|
---|
76 | Affix.prototype.checkPosition = function () {
|
---|
77 | if (!this.$element.is(':visible')) return
|
---|
78 |
|
---|
79 | var height = this.$element.height()
|
---|
80 | var offset = this.options.offset
|
---|
81 | var offsetTop = offset.top
|
---|
82 | var offsetBottom = offset.bottom
|
---|
83 | var scrollHeight = Math.max($(document).height(), $(document.body).height())
|
---|
84 |
|
---|
85 | if (typeof offset != 'object') offsetBottom = offsetTop = offset
|
---|
86 | if (typeof offsetTop == 'function') offsetTop = offset.top(this.$element)
|
---|
87 | if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
|
---|
88 |
|
---|
89 | var affix = this.getState(scrollHeight, height, offsetTop, offsetBottom)
|
---|
90 |
|
---|
91 | if (this.affixed != affix) {
|
---|
92 | if (this.unpin != null) this.$element.css('top', '')
|
---|
93 |
|
---|
94 | var affixType = 'affix' + (affix ? '-' + affix : '')
|
---|
95 | var e = $.Event(affixType + '.bs.affix')
|
---|
96 |
|
---|
97 | this.$element.trigger(e)
|
---|
98 |
|
---|
99 | if (e.isDefaultPrevented()) return
|
---|
100 |
|
---|
101 | this.affixed = affix
|
---|
102 | this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
|
---|
103 |
|
---|
104 | this.$element
|
---|
105 | .removeClass(Affix.RESET)
|
---|
106 | .addClass(affixType)
|
---|
107 | .trigger(affixType.replace('affix', 'affixed') + '.bs.affix')
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (affix == 'bottom') {
|
---|
111 | this.$element.offset({
|
---|
112 | top: scrollHeight - height - offsetBottom
|
---|
113 | })
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | // AFFIX PLUGIN DEFINITION
|
---|
119 | // =======================
|
---|
120 |
|
---|
121 | function Plugin(option) {
|
---|
122 | return this.each(function () {
|
---|
123 | var $this = $(this)
|
---|
124 | var data = $this.data('bs.affix')
|
---|
125 | var options = typeof option == 'object' && option
|
---|
126 |
|
---|
127 | if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
|
---|
128 | if (typeof option == 'string') data[option]()
|
---|
129 | })
|
---|
130 | }
|
---|
131 |
|
---|
132 | var old = $.fn.affix
|
---|
133 |
|
---|
134 | $.fn.affix = Plugin
|
---|
135 | $.fn.affix.Constructor = Affix
|
---|
136 |
|
---|
137 |
|
---|
138 | // AFFIX NO CONFLICT
|
---|
139 | // =================
|
---|
140 |
|
---|
141 | $.fn.affix.noConflict = function () {
|
---|
142 | $.fn.affix = old
|
---|
143 | return this
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | // AFFIX DATA-API
|
---|
148 | // ==============
|
---|
149 |
|
---|
150 | $(window).on('load', function () {
|
---|
151 | $('[data-spy="affix"]').each(function () {
|
---|
152 | var $spy = $(this)
|
---|
153 | var data = $spy.data()
|
---|
154 |
|
---|
155 | data.offset = data.offset || {}
|
---|
156 |
|
---|
157 | if (data.offsetBottom != null) data.offset.bottom = data.offsetBottom
|
---|
158 | if (data.offsetTop != null) data.offset.top = data.offsetTop
|
---|
159 |
|
---|
160 | Plugin.call($spy, data)
|
---|
161 | })
|
---|
162 | })
|
---|
163 |
|
---|
164 | }(jQuery);
|
---|