-
-
Notifications
You must be signed in to change notification settings - Fork 79k
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: highlights wrong navigation item when a page is reloaded. #32496
Comments
I refreshed 2-3 times and its working fine |
Did you reload the whole page 3 times? You need to right click on the html
output in the codepen.io and select the "reload frame " option. Before
reloading scroll the page to a different section, say item 3 or 4
…On Wed, Dec 16, 2020, 1:31 AM Neelam-Nishad ***@***.***> wrote:
I refreshed 2-3 times and its working fine
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#32496 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACG6JSK2MHTZMKDJPV6336LSU66CPANCNFSM4U43JHHA>
.
|
You may copy the code in a local html file too and recreate the issue by scrolling to a different section and reloading the page. |
yes I understand the problem now |
I copied the code in my local, and its really working fine there , even after reloads. may be the problem is with codepen.io. |
PLEASE REMEMBER TO paste the code within the following blocks
'''
<!doctype html>
<html lang="en">
</html>
'''
I found the issue while working on a project. In order to report the bug, I
created the demo in codepen. In fact, I copied the codepen code in the
local file and tested the issue locally too. I don't know how you tested
it. As I said, you need to scroll the page to a different section. Then
click on the browser refresh button. Now, you would observe that incorrect
navigation items are highlighted, when you scroll the page through
different sections.
|
@Neelam-Nishad I have pasted the complete demo code here. you may copy this to you local file and test it out.
|
Same thing happens with me! |
Same problem |
I am working on my project for migration v4 to v5. This is same happens with me in v5 after reloading the page scroll again. It was working fine in v4. |
This is not isolated. Scrollspy is extremely buggy. |
I hope this problem will fixed in stable release version |
Exactly! Scrollspy has like 170 issues currently which have not been resolved. Normal scrollspy (not using bs4 to do them) might be a better option. Only that when working with BS you can't use normal scrollspy because of overrides! If you migrate your project to v5 it may not happen, although I am not sure of this |
I am pretty sure this is actually a problem with the javascript of bootsrap scrollspy check #32600 about the same. Shows in depth details about the javascript and source code. I might legit fork this and correct and make some changes. |
So basically, the last refreshed item when reloaded gets highlighted. This is because bootstrap logs the last scroll and puts a certain item in the navbar (class: nav-item) with it! Then sees last scroll made and highlights the nav-item which is paired with it. The javascript has to be reworked on because, scrollspy is very glitchy. |
I don't think this is the issue. From inspecting the scrollspy element, to me it looks like target offsets (stored in '_offsets') are recalculated from the current scroll position on page load instead of from the 0 scroll position, so when the page loads with a scroll position that is not the top, or 0, it will calculate faulty offsets. Didn't look into the code but I am assuming its because the code to get elements offset on page (which i think is deperecated code) was replaced with code like .getBoundingClientRect().top (which i think is new standard and more accurate) but which gets relative offset to current scroll. The problem is that we do not stick to either scheme (we either have to stick to current relative window offset solution or the old scroll page offset solution) The hot fix seems extremely simple, when computing offsets, simply add current scroll position to the relative offset value we have right now. For example, we load/reload page into scroll position 1000, we have a elements at page offsets [200, 600, 1500], but does computed offset will be [-800, -400, 500] so simply add the current position and you will get the original values.
Im fairly new to web dev so might be wrong about some things. Also one issue is that the initial header is going to be calculated wrong but as soon as you scroll it will correct itself, this is just a quick hotfix, not a solution, but this might lend someone to implement a solution within bootstrap itself. |
Chapeau on finding this, especially when stating yourself as fairly new. window.onload = function () {
hotfixScrollSpy();
window.scrollBy(0,1);
} Hopefully the developers will take notice and fix the bootstrap component soon. |
@AvWoN @DigiLive thanks for finding a hotfix! The solution makes sense in principle, but I'm running into an issue with the Seems like I am not the only one with this problem Update window.onload = function () {
new bootstrap.ScrollSpy(document.body, {
target: '#nameOfScrollSpyTargetClass'
}); hotfixScrollSpy();
window.scrollBy(0,1);
} |
I'm no expert either, but this is my 5 cents...
At first I tried to apply the hotfix at |
Same problem here. The hotfix do not solve the problem for me. |
What exactly is the problem with the hot fix? Is there an error in the console? |
Same issue. The hotfix only corrects offsets when page is reloaded but not when it is resized. Been trying to come up with with a hotfix for resizing but no luck so far |
Since the issue was about a reload, it doesn't take a resize in consideration. $(window).resize(function() {
'use strict';
const dataSpyList = [].slice.call(document.querySelectorAll('[data-bs-spy="scroll"]'));
dataSpyList.forEach(function(dataSpyEl) {
bootstrap.ScrollSpy.getInstance(dataSpyEl).refresh();
});
hotfixScrollSpy();
window.scrollBy(0, 1);
}); |
Issue is still in bootstrap-5-beta-3. have you finally any fixed by officially ? @mdo |
Using Bootstrap 5.1 I also stumbled over this issue. To get the suggested hotfixScrollSpy function (including the window.scrollBy(0,1) added by unHumbleBen) to work, I have simply postponed calling hotfixScrollSpy() using setTimeout - a delay of 50 ms worked for me. Maybe this helps others here as well... |
I am also having issue with bootstrap 5.0.1 scroll spy with list group, |
I think 5.0.1 version is more stable than 5.0.0 or beta version. Have you tried scrollspy on 5.0.0 version too? I don't have the CDN (css & js) links. So, i hope someone can help me here |
I tried bootstrap 4.6. Scrollspy works perfectly with 4.6. |
Thank you it work for me too |
I tried bootstrap 5.1.1. Seems it's fixed. |
FINALLY, can confirm, only took 9 months :). Thank you @alpadev , much appreciated! |
This still appears to be the case for me on 5.1.3 On page load, it highlights the last nav-item EDIT: Nevermind. I accidentally had body and another element having the |
https://codepen.io/debobuiz/pen/ZEpempL
Issue: Please refer the screenshot. I have scrolled the page to Item 1. But in the navbar, "Item 3" is highlighted.
How to reproduce the issue:
When the page is opened initially, things will be fine. Scroll the page to item 3 or item 4. Then right-click and "reload frame" in the codepen.io preview section. Now, when you scroll the page through various items, the correct item will not be highlighted in the navbar.
The text was updated successfully, but these errors were encountered: