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

Scrollspy selecting the last element when at the bottom of the page #18804

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 7 additions & 1 deletion js/src/scrollspy.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,17 @@ const ScrollSpy = (($) => {
)
}

_getOffsetHeight() {
return this._scrollElement === window ?
window.innerHeight : this._scrollElement.offsetHeight
}

_process() {
let scrollTop = this._getScrollTop() + this._config.offset
let scrollHeight = this._getScrollHeight()
let maxScroll = this._config.offset
+ scrollHeight
- this._scrollElement.offsetHeight
- this._getOffsetHeight()

if (this._scrollHeight !== scrollHeight) {
this.refresh()
Expand All @@ -212,6 +217,7 @@ const ScrollSpy = (($) => {
if (this._activeTarget !== target) {
this._activate(target)
}
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because there is a case when scrollTop is lower than offset of the last target and there is no more space to scroll down in this._scrollElement. In this case this._activeTarget would get overwritten to the last but one target in line 235 if there is no return statement in this line.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, I was on mobile and it looked like this was already the end of the function (obviously it's not).

}

if (this._activeTarget && scrollTop < this._offsets[0]) {
Expand Down
4 changes: 4 additions & 0 deletions js/tests/visual/scrollspy.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<a class="dropdown-item" href="#three" tabindex="-1">three</a>
</div>
</li>
<li class="nav-item"><a class="nav-link" href="#four">Selected when page scrolled to the bottom</a></li>
</ul>
</div>
</nav>
Expand Down Expand Up @@ -76,6 +77,9 @@ <h2 id="three">three</h2>
<p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
<p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
<p>Ad leggings keytar, brunch id art party dolor labore. Pitchfork yr enim lo-fi before they sold out qui. Tumblr farm-to-table bicycle rights whatever. Anim keffiyeh carles cardigan. Velit seitan mcsweeney's photo booth 3 wolf moon irure. Cosby sweater lomo jean shorts, williamsburg hoodie minim qui you probably haven't heard of them et cardigan trust fund culpa biodiesel wes anderson aesthetic. Nihil tattooed accusamus, cred irony biodiesel keffiyeh artisan ullamco consequat.</p>
<hr>
<h2 id="four">four</h2>
<p>One liner</p>

</div>

Expand Down