From 571fd32bcee3d069d5f30167a887b47efd492f01 Mon Sep 17 00:00:00 2001 From: fat Date: Tue, 10 Jun 2014 17:37:40 -0700 Subject: [PATCH] fix #13220 Scrollspy generates wrong offsets for a page-- recalculate offsets if `scrollheight` changes --- js/scrollspy.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/js/scrollspy.js b/js/scrollspy.js index 78858680e3..fe8fbc6826 100644 --- a/js/scrollspy.js +++ b/js/scrollspy.js @@ -27,6 +27,7 @@ this.offsets = [] this.targets = [] this.activeTarget = null + this.scrollHeight = 0 this.$scrollElement.on('scroll.bs.scrollspy', process) this.refresh() @@ -39,6 +40,10 @@ offset: 10 } + ScrollSpy.prototype.getScrollHeight = function () { + return this.$scrollElement[0].scrollHeight || Math.max(this.$body[0].scrollHeight, document.documentElement.scrollHeight) + } + ScrollSpy.prototype.refresh = function () { var offsetMethod = 'offset' var offsetBase = 0 @@ -50,6 +55,7 @@ this.offsets = [] this.targets = [] + this.scrollHeight = this.getScrollHeight() var self = this @@ -74,13 +80,17 @@ 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 scrollHeight = this.getScrollHeight() var maxScroll = this.options.offset + scrollHeight - this.$scrollElement.height() var offsets = this.offsets var targets = this.targets var activeTarget = this.activeTarget var i + if (this.scrollHeight != scrollHeight) { + this.refresh() + } + if (scrollTop >= maxScroll) { return activeTarget != (i = targets[targets.length - 1]) && this.activate(i) }