aboutsummaryrefslogtreecommitdiff
path: root/js/scrollspy.js
diff options
context:
space:
mode:
authorfat <[email protected]>2013-05-16 17:18:15 -0700
committerfat <[email protected]>2013-05-16 17:18:15 -0700
commit149ecd21bf7c56df897088d09ca309eb43a7dd9d (patch)
tree9138ea6d99244c3abf114069482c8c397a0bc2cd /js/scrollspy.js
parent53bc93282fbeab3c1b779f1914ab040290668369 (diff)
downloadbootstrap-149ecd21bf7c56df897088d09ca309eb43a7dd9d.tar.xz
bootstrap-149ecd21bf7c56df897088d09ca309eb43a7dd9d.zip
straight trash wang
Diffstat (limited to 'js/scrollspy.js')
-rw-r--r--js/scrollspy.js194
1 files changed, 94 insertions, 100 deletions
diff --git a/js/scrollspy.js b/js/scrollspy.js
index 53bd41eef..cb257b2ca 100644
--- a/js/scrollspy.js
+++ b/js/scrollspy.js
@@ -18,130 +18,124 @@
* ============================================================== */
-!function ($) {
+!function ($) { "use strict";
- "use strict"; // jshint ;_;
-
-
- /* SCROLLSPY CLASS DEFINITION
- * ========================== */
+ // 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)
+ var href
+ var process = $.proxy(this.process, this)
+ var $element = $(element).is('body') ? $(window) : $(element)
+
+ this.$body = $('body')
this.$scrollElement = $element.on('scroll.scroll-spy.data-api', process)
- this.selector = (this.options.target
+ this.options = $.extend({}, ScrollSpy.DEFAULTS, options)
+ this.selector = (this.options.target
|| ((href = $(element).attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
|| '') + ' .nav li > a'
- this.$body = $('body')
+ this.offsets = $([])
+ this.targets = $([])
+ this.activeTarget = null
+
this.refresh()
this.process()
}
- ScrollSpy.prototype = {
-
- constructor: ScrollSpy
-
- , refresh: function () {
- var self = this
- , $targets
-
- this.offsets = $([])
- this.targets = $([])
-
- $targets = this.$body
- .find(this.selector)
- .map(function () {
- var $el = $(this)
- , href = $el.data('target') || $el.attr('href')
- , $href = /^#\w/.test(href) && $(href)
- return ( $href
- && $href.length
- && [[ $href.position().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href ]] ) || null
- })
- .sort(function (a, b) { return a[0] - b[0] })
- .each(function () {
- self.offsets.push(this[0])
- self.targets.push(this[1])
- })
- }
-
- , process: function () {
- var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
- , scrollHeight = this.$scrollElement[0].scrollHeight || this.$body[0].scrollHeight
- , maxScroll = scrollHeight - this.$scrollElement.height()
- , offsets = this.offsets
- , targets = this.targets
- , activeTarget = this.activeTarget
- , i
-
- if (scrollTop >= maxScroll) {
- return activeTarget != (i = targets.last()[0])
- && this.activate ( i )
- }
-
- for (i = offsets.length; i--;) {
- activeTarget != targets[i]
- && scrollTop >= offsets[i]
- && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
- && this.activate( targets[i] )
- }
- }
-
- , activate: function (target) {
- var active
- , selector
-
- this.activeTarget = target
-
- $(this.selector)
- .parents('.active')
- .removeClass('active')
-
- selector = this.selector
- + '[data-target="' + target + '"],'
- + this.selector + '[href="' + target + '"]'
-
- active = $(selector)
- .parents('li')
- .addClass('active')
-
- if (active.parent('.dropdown-menu').length) {
- active = active.closest('li.dropdown').addClass('active')
- }
-
- active.trigger('activate')
- }
+ ScrollSpy.DEFAULTS = {
+ offset: 10
+ }
+
+ ScrollSpy.prototype.refresh = function () {
+ this.offsets = $([])
+ this.targets = $([])
+
+ var self = this
+ var $targets = this.$body
+ .find(this.selector)
+ .map(function () {
+ var $el = $(this)
+ var href = $el.data('target') || $el.attr('href')
+ var $href = /^#\w/.test(href) && $(href)
+
+ return ($href
+ && $href.length
+ && [[ $href.position().top + (!$.isWindow(self.$scrollElement.get(0)) && self.$scrollElement.scrollTop()), href ]]) || null
+ })
+ .sort(function (a, b) { return a[0] - b[0] })
+ .each(function () {
+ self.offsets.push(this[0])
+ self.targets.push(this[1])
+ })
+ }
+ ScrollSpy.prototype.process = function () {
+ var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
+ var scrollHeight = this.$scrollElement[0].scrollHeight || this.$body[0].scrollHeight
+ var maxScroll = scrollHeight - this.$scrollElement.height()
+ var offsets = this.offsets
+ var targets = this.targets
+ var activeTarget = this.activeTarget
+ var i
+
+ if (scrollTop >= maxScroll) {
+ return activeTarget != (i = targets.last()[0]) && this.activate(i)
+ }
+
+ for (i = offsets.length; i--;) {
+ activeTarget != targets[i]
+ && scrollTop >= offsets[i]
+ && (!offsets[i + 1] || scrollTop <= offsets[i + 1])
+ && this.activate( targets[i] )
+ }
}
+ ScrollSpy.prototype.activate = function (target) {
+ this.activeTarget = target
+
+ $(this.selector)
+ .parents('.active')
+ .removeClass('active')
+
+ var selector = this.selector
+ + '[data-target="' + target + '"],'
+ + this.selector + '[href="' + target + '"]'
- /* SCROLLSPY PLUGIN DEFINITION
- * =========================== */
+ var active = $(selector)
+ .parents('li')
+ .addClass('active')
+
+ if (active.parent('.dropdown-menu').length) {
+ active = active
+ .closest('li.dropdown')
+ .addClass('active')
+ }
+
+ active.trigger('activate')
+ }
+
+
+ // SCROLLSPY PLUGIN DEFINITION
+ // ===========================
var old = $.fn.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)))
+ var $this = $(this)
+ var data = $this.data('bs-scrollspy')
+ var options = typeof option == 'object' && option
+
+ if (!data) $this.data('bs-scrollspy', (data = new ScrollSpy(this, options)))
if (typeof option == 'string') data[option]()
})
}
$.fn.scrollspy.Constructor = ScrollSpy
- $.fn.scrollspy.defaults = {
- offset: 10
- }
-
- /* SCROLLSPY NO CONFLICT
- * ===================== */
+ // SCROLLSPY NO CONFLICT
+ // =====================
$.fn.scrollspy.noConflict = function () {
$.fn.scrollspy = old
@@ -149,8 +143,8 @@
}
- /* SCROLLSPY DATA-API
- * ================== */
+ // SCROLLSPY DATA-API
+ // ==================
$(window).on('load', function () {
$('[data-spy="scroll"]').each(function () {