ffffng/app/bower_components/bootstrap-sass-official/assets/javascripts/bootstrap/affix.js

163 lines
4.7 KiB
JavaScript
Raw Normal View History

2014-05-12 20:08:19 +02:00
/* ========================================================================
2016-05-16 13:33:49 +02:00
* Bootstrap: affix.js v3.3.6
2014-05-12 20:08:19 +02:00
* http://getbootstrap.com/javascript/#affix
* ========================================================================
2016-05-16 13:33:49 +02:00
* Copyright 2011-2015 Twitter, Inc.
2014-05-12 20:08:19 +02:00
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
* ======================================================================== */
+function ($) {
'use strict';
// AFFIX CLASS DEFINITION
// ======================
var Affix = function (element, options) {
this.options = $.extend({}, Affix.DEFAULTS, options)
2014-08-10 12:02:48 +02:00
this.$target = $(this.options.target)
2014-05-12 20:08:19 +02:00
.on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
.on('click.bs.affix.data-api', $.proxy(this.checkPositionWithEventLoop, this))
this.$element = $(element)
2016-05-16 13:33:49 +02:00
this.affixed = null
this.unpin = null
2014-05-12 20:08:19 +02:00
this.pinnedOffset = null
this.checkPosition()
}
2016-05-16 13:33:49 +02:00
Affix.VERSION = '3.3.6'
2014-08-10 12:02:48 +02:00
Affix.RESET = 'affix affix-top affix-bottom'
2014-05-12 20:08:19 +02:00
Affix.DEFAULTS = {
2014-08-10 12:02:48 +02:00
offset: 0,
target: window
2014-05-12 20:08:19 +02:00
}
2016-05-16 13:33:49 +02:00
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
}
2014-05-12 20:08:19 +02:00
Affix.prototype.getPinnedOffset = function () {
if (this.pinnedOffset) return this.pinnedOffset
this.$element.removeClass(Affix.RESET).addClass('affix')
2014-08-10 12:02:48 +02:00
var scrollTop = this.$target.scrollTop()
2014-05-12 20:08:19 +02:00
var position = this.$element.offset()
return (this.pinnedOffset = position.top - scrollTop)
}
Affix.prototype.checkPositionWithEventLoop = function () {
setTimeout($.proxy(this.checkPosition, this), 1)
}
Affix.prototype.checkPosition = function () {
if (!this.$element.is(':visible')) return
2016-05-16 13:33:49 +02:00
var height = this.$element.height()
2014-05-12 20:08:19 +02:00
var offset = this.options.offset
var offsetTop = offset.top
var offsetBottom = offset.bottom
2016-05-16 13:33:49 +02:00
var scrollHeight = Math.max($(document).height(), $(document.body).height())
2014-05-12 20:08:19 +02:00
if (typeof offset != 'object') offsetBottom = offsetTop = offset
if (typeof offsetTop == 'function') offsetTop = offset.top(this.$element)
if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
2016-05-16 13:33:49 +02:00
var affix = this.getState(scrollHeight, height, offsetTop, offsetBottom)
2014-05-12 20:08:19 +02:00
2016-05-16 13:33:49 +02:00
if (this.affixed != affix) {
if (this.unpin != null) this.$element.css('top', '')
2014-05-12 20:08:19 +02:00
2016-05-16 13:33:49 +02:00
var affixType = 'affix' + (affix ? '-' + affix : '')
var e = $.Event(affixType + '.bs.affix')
2014-05-12 20:08:19 +02:00
2016-05-16 13:33:49 +02:00
this.$element.trigger(e)
2014-05-12 20:08:19 +02:00
2016-05-16 13:33:49 +02:00
if (e.isDefaultPrevented()) return
2014-05-12 20:08:19 +02:00
2016-05-16 13:33:49 +02:00
this.affixed = affix
this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
2014-05-12 20:08:19 +02:00
2016-05-16 13:33:49 +02:00
this.$element
.removeClass(Affix.RESET)
.addClass(affixType)
.trigger(affixType.replace('affix', 'affixed') + '.bs.affix')
}
2014-05-12 20:08:19 +02:00
if (affix == 'bottom') {
2014-08-10 12:02:48 +02:00
this.$element.offset({
2016-05-16 13:33:49 +02:00
top: scrollHeight - height - offsetBottom
2014-08-10 12:02:48 +02:00
})
2014-05-12 20:08:19 +02:00
}
}
// AFFIX PLUGIN DEFINITION
// =======================
2014-08-10 12:02:48 +02:00
function Plugin(option) {
2014-05-12 20:08:19 +02:00
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]()
})
}
2014-08-10 12:02:48 +02:00
var old = $.fn.affix
$.fn.affix = Plugin
2014-05-12 20:08:19 +02:00
$.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 || {}
2016-05-16 13:33:49 +02:00
if (data.offsetBottom != null) data.offset.bottom = data.offsetBottom
if (data.offsetTop != null) data.offset.top = data.offsetTop
2014-05-12 20:08:19 +02:00
2014-08-10 12:02:48 +02:00
Plugin.call($spy, data)
2014-05-12 20:08:19 +02:00
})
})
}(jQuery);