From e84b0c0433509bde0b42354754372595db523e1f Mon Sep 17 00:00:00 2001 From: Mike Robinet Date: Tue, 13 May 2014 09:57:04 -0500 Subject: [PATCH 1/2] Adjust for Scrollspy offset when calculating maxScroll. --- js/scrollspy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/scrollspy.js b/js/scrollspy.js index 74e016dc9f..53e1c48baa 100644 --- a/js/scrollspy.js +++ b/js/scrollspy.js @@ -69,7 +69,7 @@ ScrollSpy.prototype.process = function () { var scrollTop = this.$scrollElement.scrollTop() + this.options.offset var scrollHeight = this.$scrollElement[0].scrollHeight || Math.max(this.$body[0].scrollHeight, document.documentElement.scrollHeight) - var maxScroll = scrollHeight - this.$scrollElement.height() + var maxScroll = this.options.offset + scrollHeight - this.$scrollElement.height() var offsets = this.offsets var targets = this.targets var activeTarget = this.activeTarget From c2e94eba8da462ba8cacbc1558f7fdf6b3e28007 Mon Sep 17 00:00:00 2001 From: Mike Robinet Date: Tue, 13 May 2014 12:20:18 -0500 Subject: [PATCH 2/2] Add Scrollspy maxScroll with offset test. --- js/tests/unit/scrollspy.js | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/js/tests/unit/scrollspy.js b/js/tests/unit/scrollspy.js index 90c95d7691..9a00221fd8 100644 --- a/js/tests/unit/scrollspy.js +++ b/js/tests/unit/scrollspy.js @@ -42,7 +42,8 @@ $(function () { ok($topbar.find('.active', true)) }) - test('should only switch active class on current target', function () { + asyncTest('should only switch active class on current target', function () { + expect(1); var sectionHTML = '
' + '
' + '
' + @@ -75,7 +76,40 @@ $(function () { .find('#scrollspy-example') .bootstrapScrollspy({target: '#ss-target'}) + $scrollSpy.on('scroll.bs.scrollspy', function () { + ok($section.hasClass('active'), 'Active class still on root node') + start() + }) $scrollSpy.scrollTop(350); - ok($section.hasClass('active'), 'Active class still on root node') + }) + + asyncTest('middle navigation option correctly selected when large offset is used', function () { + expect(3); + var sectionHTML = '' + + '' + + '
' + + '
' + + '
' + + '
' + + '
', + $section = $(sectionHTML).appendTo('#qunit-fixture'), + $scrollSpy = $section + .show() + .filter('#content') + $scrollSpy.bootstrapScrollspy({target: '#navigation', offset: $scrollSpy.position().top}) + + $scrollSpy.on('scroll.bs.scrollspy', function () { + ok(!$section.find('#one-link').parent().hasClass('active'), 'Active class removed from first section') + ok($section.find('#two-link').parent().hasClass('active'), 'Active class on middle section') + ok(!$section.find('#three-link').parent().hasClass('active'), 'Active class not on last section') + start() + }) + $scrollSpy.scrollTop(550); }) })