Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle multiple zero-offset Scrollspy elements. #15593

Merged
merged 1 commit into from
Mar 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/scrollspy.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
for (i = offsets.length; i--;) {
activeTarget != targets[i]
&& scrollTop >= offsets[i]
&& (!offsets[i + 1] || scrollTop <= offsets[i + 1])
&& (offsets[i + 1] === undefined || scrollTop <= offsets[i + 1])
&& this.activate(targets[i])
}
}
Expand Down
38 changes: 38 additions & 0 deletions js/tests/unit/scrollspy.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,44 @@ $(function () {
.then(function () { return testElementIsActiveAfterScroll('#li-2', '#div-2') })
})

QUnit.test('should add the active class correctly when there are nested elements at 0 scroll offset', function (assert) {
var times = 0
var done = assert.async()
var navbarHtml = '<nav id="navigation" class="navbar">'
+ '<ul class="nav">'
+ '<li id="li-1"><a href="#div-1">div 1</a>'
+ '<ul>'
+ '<li id="li-2"><a href="#div-2">div 2</a></li>'
+ '</ul>'
+ '</li>'
+ '</ul>'
+ '</nav>'

var contentHtml = '<div class="content" style="position: absolute; top: 0px; overflow: auto; height: 50px">'
+ '<div id="div-1" style="padding: 0; margin: 0">'
+ '<div id="div-2" style="height: 200px; padding: 0; margin: 0">div 2</div>'
+ '</div>'
+ '</div>'

$(navbarHtml).appendTo('#qunit-fixture')

var $content = $(contentHtml)
.appendTo('#qunit-fixture')
.bootstrapScrollspy({ offset: 0, target: '#navigation' })

!function testActiveElements() {
if (++times > 3) return done()

$content.one('scroll', function () {
assert.ok($('#li-1').hasClass('active'), 'nav item for outer element has "active" class')
assert.ok($('#li-2').hasClass('active'), 'nav item for inner element has "active" class')
testActiveElements()
})

$content.scrollTop($content.scrollTop() + 10)
}()
})

QUnit.test('should clear selection if above the first section', function (assert) {
var done = assert.async()

Expand Down