aboutsummaryrefslogtreecommitdiffstats
path: root/bootstrap2/static/js/bootstrap-scrollspy.js
diff options
context:
space:
mode:
authorAlexis Metaireau <alexis@notmyidea.org>2012-03-27 15:39:48 -0700
committerAlexis Metaireau <alexis@notmyidea.org>2012-03-27 15:39:48 -0700
commit8cdc0ff6b9c9f25b29db996e5b51f7f99e11236e (patch)
tree6218570b2c68d9111ac23734563bddf585a65604 /bootstrap2/static/js/bootstrap-scrollspy.js
parentcc809c34841e0cb25cde42cff85528b05581e1a1 (diff)
parent6fa854a4396f51a85f58bbde329a5735173062c7 (diff)
downloadpelican-themes-8cdc0ff6b9c9f25b29db996e5b51f7f99e11236e.tar.gz
Merge pull request #17 from farseerfc/master
I made (yet another) bootstrap 2.0 theme
Diffstat (limited to 'bootstrap2/static/js/bootstrap-scrollspy.js')
-rw-r--r--bootstrap2/static/js/bootstrap-scrollspy.js125
1 files changed, 125 insertions, 0 deletions
diff --git a/bootstrap2/static/js/bootstrap-scrollspy.js b/bootstrap2/static/js/bootstrap-scrollspy.js
new file mode 100644
index 0000000..5f09cfe
--- /dev/null
+++ b/bootstrap2/static/js/bootstrap-scrollspy.js
@@ -0,0 +1,125 @@
+/* =============================================================
+ * bootstrap-scrollspy.js v2.0.1
+ * http://twitter.github.com/bootstrap/javascript.html#scrollspy
+ * =============================================================
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============================================================== */
+
+!function ( $ ) {
+
+ "use strict"
+
+ /* 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.process()
+ }
+
+ ScrollSpy.prototype = {
+
+ 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).position().top
+ })
+ }
+
+ , process: function () {
+ var scrollTop = this.$scrollElement.scrollTop() + this.options.offset
+ , offsets = this.offsets
+ , targets = this.targets
+ , activeTarget = this.activeTarget
+ , 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
+
+ this.activeTarget = target
+
+ this.$body
+ .find(this.selector).parent('.active')
+ .removeClass('active')
+
+ 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 ( 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]()
+ })
+ }
+
+ $.fn.scrollspy.Constructor = ScrollSpy
+
+ $.fn.scrollspy.defaults = {
+ offset: 10
+ }
+
+
+ /* SCROLLSPY DATA-API
+ * ================== */
+
+ $(function () {
+ $('[data-spy="scroll"]').each(function () {
+ var $spy = $(this)
+ $spy.scrollspy($spy.data())
+ })
+ })
+
+}( window.jQuery ); \ No newline at end of file