Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/bootstrap.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorJacob Thornton <jacobthornton@gmail.com>2012-08-15 08:06:08 +0400
committerJacob Thornton <jacobthornton@gmail.com>2012-08-15 08:06:08 +0400
commitdee57462e2805421a0fe0d786229446bbaef677b (patch)
tree49c7e0f61ba1bac7c8804ad07b96a3ad0ec654f3 /js
parent4bf93a2d76ddec19e22be771ef1832ef563c91a5 (diff)
get affix actually working and update docs
Diffstat (limited to 'js')
-rw-r--r--js/bootstrap-affix.js38
1 files changed, 20 insertions, 18 deletions
diff --git a/js/bootstrap-affix.js b/js/bootstrap-affix.js
index 7563029b14..92eea8521b 100644
--- a/js/bootstrap-affix.js
+++ b/js/bootstrap-affix.js
@@ -28,36 +28,38 @@
var Affix = function (element, options) {
this.options = $.extend({}, $.fn.affix.defaults, options)
- this.$window = $(window)
- .on('scroll.affix.data-api', $.proxy(this.checkPosition, this))
- .on('resize.affix.data-api', $.proxy(this.refresh, this))
+ this.$window = $(window).on('scroll.affix.data-api', $.proxy(this.checkPosition, this))
this.$element = $(element)
- this.refresh()
- }
-
- Affix.prototype.refresh = function () {
- this.position = this.$element.offset()
+ this.checkPosition();
}
Affix.prototype.checkPosition = function () {
if (!this.$element.is(':visible')) return
- var scrollLeft = this.$window.scrollLeft()
+ var scrollHeight = $(document).height()
, scrollTop = this.$window.scrollTop()
- , position = this.position
+ , position = this.$element.offset()
, offset = this.options.offset
+ , offsetBottom = offset.bottom
+ , offsetTop = offset.top
+ , reset = 'affix affix-top affix-bottom'
, affix
- if (typeof offset != 'object') offset = { x: offset, y: offset }
+ if (typeof offset != 'object') offsetBottom = offsetTop = offset
+ if (typeof offsetTop == 'function') offsetTop = offset.top()
+ if (typeof offsetBottom == 'function') offsetBottom = offset.bottom()
- affix = (offset.x == null || (position.left - scrollLeft <= offset.x))
- && (offset.y == null || (position.top - scrollTop <= offset.y))
+ 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 (affix == this.affixed) return
+ if (this.affixed === affix) return
this.affixed = affix
+ this.unpin = affix == 'bottom' ? position.top - scrollTop : null
- this.$element[affix ? 'addClass' : 'removeClass']('affix')
+ this.$element.removeClass(reset).addClass('affix' + (affix ? '-' + affix : ''))
}
@@ -84,15 +86,15 @@
/* AFFIX DATA-API
* ============== */
- $(function () {
+ $(window).on('load', function () {
$('[data-spy="affix"]').each(function () {
var $spy = $(this)
, data = $spy.data()
data.offset = data.offset || {}
- data.offsetX && (data.offset.x = data.offsetX)
- data.offsetY && (data.offset.y = data.offsetY)
+ data.offsetBottom && (data.offset.bottom = data.offsetBottom)
+ data.offsetTop && (data.offset.top = data.offsetTop)
$spy.affix(data)
})