1
lorchess.ru/_assets/vendor/bootstrap/js/affix.js

138 lines
3.9 KiB
JavaScript
Raw Normal View History

2014-01-24 13:24:23 +04:00
/* ========================================================================
2014-04-17 22:36:17 +04:00
* Bootstrap: affix.js v3.1.1
2014-01-24 13:24:23 +04:00
* http://getbootstrap.com/javascript/#affix
* ========================================================================
2014-04-17 22:36:17 +04:00
* Copyright 2011-2014 Twitter, Inc.
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
2014-01-24 13:24:23 +04:00
* ======================================================================== */
2014-04-17 22:36:17 +04:00
+function ($) {
'use strict';
2014-01-24 13:24:23 +04:00
// AFFIX CLASS DEFINITION
// ======================
var Affix = function (element, options) {
this.options = $.extend({}, Affix.DEFAULTS, options)
this.$window = $(window)
.on('scroll.bs.affix.data-api', $.proxy(this.checkPosition, this))
.on('click.bs.affix.data-api', $.proxy(this.checkPositionWithEventLoop, this))
2014-04-17 22:36:17 +04:00
this.$element = $(element)
this.affixed =
this.unpin =
this.pinnedOffset = null
2014-01-24 13:24:23 +04:00
this.checkPosition()
}
Affix.RESET = 'affix affix-top affix-bottom'
Affix.DEFAULTS = {
offset: 0
}
2014-04-17 22:36:17 +04:00
Affix.prototype.getPinnedOffset = function () {
if (this.pinnedOffset) return this.pinnedOffset
this.$element.removeClass(Affix.RESET).addClass('affix')
var scrollTop = this.$window.scrollTop()
var position = this.$element.offset()
return (this.pinnedOffset = position.top - scrollTop)
}
2014-01-24 13:24:23 +04:00
Affix.prototype.checkPositionWithEventLoop = function () {
setTimeout($.proxy(this.checkPosition, this), 1)
}
Affix.prototype.checkPosition = function () {
if (!this.$element.is(':visible')) return
var scrollHeight = $(document).height()
var scrollTop = this.$window.scrollTop()
var position = this.$element.offset()
var offset = this.options.offset
var offsetTop = offset.top
var offsetBottom = offset.bottom
2014-04-17 22:36:17 +04:00
if (this.affixed == 'top') position.top += scrollTop
2014-01-24 13:24:23 +04:00
if (typeof offset != 'object') offsetBottom = offsetTop = offset
2014-04-17 22:36:17 +04:00
if (typeof offsetTop == 'function') offsetTop = offset.top(this.$element)
if (typeof offsetBottom == 'function') offsetBottom = offset.bottom(this.$element)
2014-01-24 13:24:23 +04:00
var affix = this.unpin != null && (scrollTop + this.unpin <= position.top) ? false :
offsetBottom != null && (position.top + this.$element.height() >= scrollHeight - offsetBottom) ? 'bottom' :
offsetTop != null && (scrollTop <= offsetTop) ? 'top' : false
if (this.affixed === affix) return
if (this.unpin) this.$element.css('top', '')
2014-04-17 22:36:17 +04:00
var affixType = 'affix' + (affix ? '-' + affix : '')
var e = $.Event(affixType + '.bs.affix')
this.$element.trigger(e)
if (e.isDefaultPrevented()) return
2014-01-24 13:24:23 +04:00
this.affixed = affix
2014-04-17 22:36:17 +04:00
this.unpin = affix == 'bottom' ? this.getPinnedOffset() : null
2014-01-24 13:24:23 +04:00
2014-04-17 22:36:17 +04:00
this.$element
.removeClass(Affix.RESET)
.addClass(affixType)
.trigger($.Event(affixType.replace('affix', 'affixed')))
2014-01-24 13:24:23 +04:00
if (affix == 'bottom') {
2014-04-17 22:36:17 +04:00
this.$element.offset({ top: scrollHeight - offsetBottom - this.$element.height() })
2014-01-24 13:24:23 +04:00
}
}
// AFFIX PLUGIN DEFINITION
// =======================
var old = $.fn.affix
$.fn.affix = function (option) {
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]()
})
}
$.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 || {}
if (data.offsetBottom) data.offset.bottom = data.offsetBottom
if (data.offsetTop) data.offset.top = data.offsetTop
$spy.affix(data)
})
})
}(jQuery);