]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Clear scrollspy selection above first section
authorTadeu Zagallo <tadeuzagallo@gmail.com>
Fri, 23 May 2014 22:27:05 +0000 (19:27 -0300)
committerHeinrich Fenkart <hnrch02@gmail.com>
Sun, 26 Oct 2014 02:14:08 +0000 (03:14 +0100)
Closes #13563 by merging it.

js/scrollspy.js
js/tests/unit/scrollspy.js

index db2378787e3d578c3b812ba73af42556e99bb0a5..430b5d6aac3ba0a49537baa72506f0e6ccf04873 100644 (file)
@@ -91,8 +91,9 @@
       return activeTarget != (i = targets[targets.length - 1]) && this.activate(i)
     }
 
-    if (activeTarget && scrollTop <= offsets[0]) {
-      return activeTarget != (i = targets[0]) && this.activate(i)
+    if (activeTarget && scrollTop < offsets[0]) {
+      this.activeTarget = null
+      return this.clear()
     }
 
     for (i = offsets.length; i--;) {
   ScrollSpy.prototype.activate = function (target) {
     this.activeTarget = target
 
-    $(this.selector)
-      .parentsUntil(this.options.target, '.active')
-      .removeClass('active')
+    this.clear()
 
     var selector = this.selector +
         '[data-target="' + target + '"],' +
     active.trigger('activate.bs.scrollspy')
   }
 
+  ScrollSpy.prototype.clear = function () {
+    $(this.selector)
+      .parentsUntil(this.options.target, '.active')
+      .removeClass('active')
+  }
+
 
   // SCROLLSPY PLUGIN DEFINITION
   // ===========================
index a54263608d8f015349b603ebc3b4373e6729a108..46e2a1b855320c32ec88b32c630821c80dc28fb0 100644 (file)
@@ -73,7 +73,7 @@ $(function () {
     $scrollspy.scrollTop(350)
   })
 
-  test('middle navigation option correctly selected when large offset is used', function () {
+  test('should correctly select middle navigation option when large offset is used', function () {
     stop()
 
     var sectionHTML = '<div id="header" style="height: 500px;"></div>'
@@ -142,4 +142,45 @@ $(function () {
       .then(function () { return testElementIsActiveAfterScroll('#li-2', '#div-2') })
   })
 
+  test('should clear selection if above the first section', function () {
+    stop()
+
+    var sectionHTML = '<div id="header" style="height: 500px;"></div>'
+        + '<nav id="navigation" class="navbar">'
+        + '<ul class="nav navbar-nav">'
+        + '<li class="active"><a id="one-link" href="#one">One</a></li>'
+        + '<li><a id="two-link" href="#two">Two</a></li>'
+        + '<li><a id="three-link" href="#three">Three</a></li>'
+        + '</ul>'
+        + '</nav>'
+    var $section = $(sectionHTML).appendTo('#qunit-fixture')
+
+    var scrollspyHTML = '<div id="content" style="height: 200px; overflow-y: auto;">'
+        + '<div id="spacer" style="height: 100px;"/>'
+        + '<div id="one" style="height: 100px;"/>'
+        + '<div id="two" style="height: 100px;"/>'
+        + '<div id="three" style="height: 100px;"/>'
+        + '<div id="spacer" style="height: 100px;"/>'
+        + '</div>'
+    var $scrollspy = $(scrollspyHTML).appendTo('#qunit-fixture')
+
+    $scrollspy
+      .bootstrapScrollspy({
+        target: '#navigation',
+        offset: $scrollspy.position().top
+      })
+      .one('scroll.bs.scrollspy', function () {
+        strictEqual($('.active').length, 1, '"active" class on only one element present')
+        strictEqual($('.active').has('#two-link').length, 1, '"active" class on second section')
+
+        $scrollspy
+          .one('scroll.bs.scrollspy', function () {
+            strictEqual($('.active').length, 0, 'selection cleared')
+            start()
+          })
+          .scrollTop(0)
+      })
+      .scrollTop(201)
+  })
+
 })