aboutsummaryrefslogtreecommitdiff
path: root/js/bootstrap-scrollspy.js
diff options
context:
space:
mode:
authorJacob Thornton <[email protected]>2012-01-31 13:18:34 -0800
committerJacob Thornton <[email protected]>2012-01-31 13:18:34 -0800
commit0bfbe5058d61ae93d82b09f1dff7eb30dc22426e (patch)
tree6ccf3dd9c36ff4b1f6cde5edbce21cdf0de78497 /js/bootstrap-scrollspy.js
parent43cbc9440425b7c97c943690eefd14520de708e1 (diff)
parent4bd1ba4e0dc44d1d16161306576548f378ab1f8a (diff)
downloadbootstrap-0bfbe5058d61ae93d82b09f1dff7eb30dc22426e.tar.xz
bootstrap-0bfbe5058d61ae93d82b09f1dff7eb30dc22426e.zip
Merge branch '2.0-wip'
Conflicts: .gitignore LICENSE Makefile bootstrap.css bootstrap.min.css docs/assets/js/application.js docs/assets/js/google-code-prettify/prettify.css docs/index.html docs/javascript.html examples/container-app.html examples/fluid.html examples/hero.html js/bootstrap-alerts.js js/bootstrap-dropdown.js js/bootstrap-modal.js js/bootstrap-popover.js js/bootstrap-scrollspy.js js/bootstrap-tabs.js js/bootstrap-twipsy.js js/tests/index.html js/tests/unit/bootstrap-modal.js js/tests/unit/bootstrap-popover.js js/tests/unit/bootstrap-tabs.js lib/forms.less lib/mixins.less lib/patterns.less lib/scaffolding.less lib/tables.less
Diffstat (limited to 'js/bootstrap-scrollspy.js')
-rw-r--r--js/bootstrap-scrollspy.js106
1 files changed, 62 insertions, 44 deletions
diff --git a/js/bootstrap-scrollspy.js b/js/bootstrap-scrollspy.js
index efbc43296..e8bd0715c 100644
--- a/js/bootstrap-scrollspy.js
+++ b/js/bootstrap-scrollspy.js
@@ -1,8 +1,8 @@
/* =============================================================
- * bootstrap-scrollspy.js v1.4.0
+ * bootstrap-scrollspy.js v2.0.0
* http://twitter.github.com/bootstrap/javascript.html#scrollspy
* =============================================================
- * Copyright 2011 Twitter, Inc.
+ * Copyright 2012 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,38 +17,46 @@
* limitations under the License.
* ============================================================== */
-
!function ( $ ) {
"use strict"
- var $window = $(window)
-
- function ScrollSpy( topbar, selector ) {
- var processScroll = $.proxy(this.processScroll, this)
- this.$topbar = $(topbar)
- this.selector = selector || 'li > a'
+ /* SCROLLSPY CLASS DEFINITION
+ * ========================== */
+
+ function ScrollSpy( element, options) {
+ var process = $.proxy(this.process, this)
+ , $element = $(element).is('body') ? $(window) : $(element)
+ , href
+ this.options = $.extend({}, $.fn.scrollspy.defaults, options)
+ this.$scrollElement = $element.on('scroll.scroll.data-api', process)
+ this.selector = (this.options.target
+ || ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
+ || '') + ' .nav li > a'
+ this.$body = $('body').on('click.scroll.data-api', this.selector, process)
this.refresh()
- this.$topbar.delegate(this.selector, 'click', processScroll)
- $window.scroll(processScroll)
- this.processScroll()
+ this.process()
}
ScrollSpy.prototype = {
- refresh: function () {
- this.targets = this.$topbar.find(this.selector).map(function () {
- var href = $(this).attr('href')
- return /^#\w/.test(href) && $(href).length ? href : null
- })
+ constructor: ScrollSpy
+
+ , refresh: function () {
+ this.targets = this.$body
+ .find(this.selector)
+ .map(function () {
+ var href = $(this).attr('href')
+ return /^#\w/.test(href) && $(href).length ? href : null
+ })
this.offsets = $.map(this.targets, function (id) {
- return $(id).offset().top
+ return $(id).position().top
})
}
- , processScroll: function () {
- var scrollTop = $window.scrollTop() + 10
+ , process: function () {
+ var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
, offsets = this.offsets
, targets = this.targets
, activeTarget = this.activeTarget
@@ -58,50 +66,60 @@
activeTarget != targets[i]
&& scrollTop >= offsets[i]
&& (!offsets[i + 1] || scrollTop <= offsets[i + 1])
- && this.activateButton( targets[i] )
+ && this.activate( targets[i] )
}
}
- , activateButton: function (target) {
+ , activate: function (target) {
+ var active
+
this.activeTarget = target
- this.$topbar
+ this.$body
.find(this.selector).parent('.active')
.removeClass('active')
- this.$topbar
+ active = this.$body
.find(this.selector + '[href="' + target + '"]')
.parent('li')
.addClass('active')
+
+ if ( active.parent('.dropdown-menu') ) {
+ active.closest('li.dropdown').addClass('active')
+ }
}
}
- /* SCROLLSPY PLUGIN DEFINITION
- * =========================== */
-
- $.fn.scrollSpy = function( options ) {
- var scrollspy = this.data('scrollspy')
- if (!scrollspy) {
- return this.each(function () {
- $(this).data('scrollspy', new ScrollSpy( this, options ))
- })
- }
+ /* SCROLLSPY PLUGIN DEFINITION
+ * =========================== */
- if ( options === true ) {
- return scrollspy
- }
+ $.fn.scrollspy = function ( option ) {
+ return this.each(function () {
+ var $this = $(this)
+ , data = $this.data('scrollspy')
+ , options = typeof option == 'object' && option
+ if (!data) $this.data('scrollspy', (data = new ScrollSpy(this, options)))
+ if (typeof option == 'string') data[option]()
+ })
+ }
- if ( typeof options == 'string' ) {
- scrollspy[options]()
- }
+ $.fn.scrollspy.Constructor = ScrollSpy
- return this
+ $.fn.scrollspy.defaults = {
+ offset: 10
}
- $(document).ready(function () {
- $('body').scrollSpy('[data-scrollspy] li > a')
+
+ /* SCROLLSPY DATA-API
+ * ================== */
+
+ $(function () {
+ $('[data-spy="scroll"]').each(function () {
+ var $spy = $(this)
+ $spy.scrollspy($spy.data())
+ })
})
-}( window.jQuery || window.ender ); \ No newline at end of file
+}( window.jQuery )