1 /* ========================================================================
2 * Bootstrap: affix.js v3.3.2
3 * http://getbootstrap.com/javascript/#affix
4 * ========================================================================
5 * Copyright 2011-2015 Twitter, Inc.
6 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
7 * ======================================================================== */
13 // AFFIX CLASS DEFINITION
14 // ======================
16 var Affix = function (element, options) {
17 this.options = $.extend({}, Affix.DEFAULTS, options)
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))
23 this.$element = $(element)
26 this.pinnedOffset = null
31 Affix.VERSION = '3.3.2'
33 Affix.RESET = 'affix affix-top affix-bottom'
40 Affix.prototype.getState = function (scrollHeight, height, offsetTop, offsetBottom) {
41 var scrollTop = this.$target.scrollTop()
42 var position = this.$element.offset()
43 var targetHeight = this.$target.height()
45 if (offsetTop != null && this.affixed == 'top') return scrollTop < offsetTop ? 'top' : false
47 if (this.affixed == 'bottom') {
48 if (offsetTop != null) return (scrollTop + this.unpin <= position.top) ? false : 'bottom'
49 return (scrollTop + targetHeight <= scrollHeight - offsetBottom) ? false : 'bottom'
52 var initializing = this.affixed == null
53 var colliderTop = initializing ? scrollTop : position.top
54 var colliderHeight = initializing ? targetHeight : height
56 if (offsetTop != null && scrollTop <= offsetTop) return 'top'
57 if (offsetBottom != null && (colliderTop + colliderHeight >= scrollHeight - offsetBottom)) return 'bottom'
62 Affix.prototype.getPinnedOffset = function () {
63 if (this.pinnedOffset) return this.pinnedOffset
64 this.$element.removeClass(Affix.RESET).addClass('affix')
65 var scrollTop = this.$target.scrollTop()
66 var position = this.$element.offset()
67 return (this.pinnedOffset = position.top - scrollTop)
70 Affix.prototype.checkPositionWithEventLoop = function () {
71 setTimeout($.proxy(this.checkPosition, this), 1)
74 Affix.prototype.checkPosition = function () {
75 if (!this.$element.is(':visible')) return
77 var height = this.$element.height()
78 var offset = this.options.offset
79 var offsetTop = offset.top
80 var offsetBottom = offset.bottom
81 var scrollHeight = $('body').height()
83 if (typeof offset != 'object') offsetBottom = offsetTop = offset
84 if (typeof offsetTop == 'function') offsetTop = offset.top(this.$element)
85 if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
87 var affix = this.getState(scrollHeight, height, offsetTop, offsetBottom)
89 if (this.affixed != affix) {
90 if (this.unpin != null) this.$element.css('top', '')
92 var affixType = 'affix' + (affix ? '-' + affix : '')
93 var e = $.Event(affixType + '.bs.affix')
95 this.$element.trigger(e)
97 if (e.isDefaultPrevented()) return
100 this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
103 .removeClass(Affix.RESET)
105 .trigger(affixType.replace('affix', 'affixed') + '.bs.affix')
108 if (affix == 'bottom') {
109 this.$element.offset({
110 top: scrollHeight - height - offsetBottom
116 // AFFIX PLUGIN DEFINITION
117 // =======================
119 function Plugin(option) {
120 return this.each(function () {
122 var data = $this.data('bs.affix')
123 var options = typeof option == 'object' && option
125 if (!data) $this.data('bs.affix', (data = new Affix(this, options)))
126 if (typeof option == 'string') data[option]()
133 $.fn.affix.Constructor = Affix
139 $.fn.affix.noConflict = function () {
148 $(window).on('load', function () {
149 $('[data-spy="affix"]').each(function () {
151 var data = $spy.data()
153 data.offset = data.offset || {}
155 if (data.offsetBottom != null) data.offset.bottom = data.offsetBottom
156 if (data.offsetTop != null) data.offset.top = data.offsetTop
158 Plugin.call($spy, data)