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