diff --git a/js/dist/scrollspy.js b/js/dist/scrollspy.js index a748f7782..69fc579ed 100644 --- a/js/dist/scrollspy.js +++ b/js/dist/scrollspy.js @@ -27,7 +27,8 @@ var ScrollSpy = (function ($) { var JQUERY_NO_CONFLICT = $.fn[NAME]; var Default = { - offset: 10 + offset: 10, + method: 'auto' }; var Event = { @@ -48,6 +49,11 @@ var ScrollSpy = (function ($) { LI: 'li' }; + var OffsetMethod = { + OFFSET: 'offset', + POSITION: 'position' + }; + /** * ------------------------------------------------------------------------ * Class Definition @@ -81,13 +87,11 @@ var ScrollSpy = (function ($) { value: function refresh() { var _this = this; - var offsetMethod = 'offset'; - var offsetBase = 0; + var autoMethod = this._scrollElement !== this._scrollElement.window ? OffsetMethod.POSITION : OffsetMethod.OFFSET; - if (this._scrollElement !== this._scrollElement.window) { - offsetMethod = 'position'; - offsetBase = this._getScrollTop(); - } + var offsetMethod = this._config.method === 'auto' ? autoMethod : this._config.method; + + var offsetBase = offsetMethod === OffsetMethod.POSITION ? this._getScrollTop() : 0; this._offsets = []; this._targets = []; diff --git a/js/dist/scrollspy.js.map b/js/dist/scrollspy.js.map index 9a134b521..98292c7fd 100644 Binary files a/js/dist/scrollspy.js.map and b/js/dist/scrollspy.js.map differ diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index d1be3a45f..3f4a145a1 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -25,7 +25,8 @@ const ScrollSpy = (($) => { const JQUERY_NO_CONFLICT = $.fn[NAME] const Default = { - offset : 10 + offset : 10, + method : 'auto' } const Event = { @@ -46,6 +47,11 @@ const ScrollSpy = (($) => { LI : 'li' } + const OffsetMethod = { + OFFSET : 'offset', + POSITION : 'position' + } + /** * ------------------------------------------------------------------------ @@ -86,13 +92,14 @@ const ScrollSpy = (($) => { // public refresh() { - let offsetMethod = 'offset' - let offsetBase = 0 + let autoMethod = this._scrollElement !== this._scrollElement.window ? + OffsetMethod.POSITION : OffsetMethod.OFFSET - if (this._scrollElement !== this._scrollElement.window) { - offsetMethod = 'position' - offsetBase = this._getScrollTop() - } + let offsetMethod = this._config.method === 'auto' ? + autoMethod : this._config.method + + let offsetBase = offsetMethod === OffsetMethod.POSITION ? + this._getScrollTop() : 0 this._offsets = [] this._targets = [] diff --git a/js/tests/unit/scrollspy.js b/js/tests/unit/scrollspy.js index bf5fa0bff..bf963b3ab 100644 --- a/js/tests/unit/scrollspy.js +++ b/js/tests/unit/scrollspy.js @@ -275,4 +275,90 @@ $(function () { .then(function () { return testElementIsActiveAfterScroll('#li-100-1', '#div-100-1') }) }) + QUnit.test('should allow passed in option offset method: offset', function (assert) { + assert.expect(4) + + var testOffsetMethod = function (type) { + var deferred = $.Deferred() + var navbarHtml = + '' + var contentHtml = + '