Current ScrollSpy behavior had some shortcomings when it comes to detecting the correct active class.
Sometimes none of the classes are active
After clicking to go to a section, it immediately shows as non active
Solution
To fix this I simplified the current logic, by adding an active state for all sections and keeping track of the previous highlighted section. The current rational is to highlight the first section that is intersecting at all times.
Different logic for scrolling up or down and custom thresholds are removed due to not being needed anymore.
Note: This is a first draft, so I didn't delve deeper into all of the code that can be simplified. I would be grateful for any feedback. Thank you for your time and for this amazing project!
Changes
Scroll Behaviour:
Before ( Note that sometimes the active class disappears and other times it jumps to early ):
Motivation & Context
Current ScrollSpy behavior had some shortcomings when it comes to detecting the correct active class.
Solution
To fix this I simplified the current logic, by adding an active state for all sections and keeping track of the previous highlighted section. The current rational is to highlight the first section that is intersecting at all times.
Different logic for scrolling up or down and custom thresholds are removed due to not being needed anymore.
Note: This is a first draft, so I didn't delve deeper into all of the code that can be simplified. I would be grateful for any feedback. Thank you for your time and for this amazing project!
Changes
Scroll Behaviour:
https://github.com/user-attachments/assets/6db60726-699f-43fa-847b-52e28d59acc2
https://github.com/user-attachments/assets/3c060388-9749-457d-971d-05b6e8b7cedd
Click Behaviour:
https://github.com/user-attachments/assets/ca1f2586-3c90-4e03-8955-c2ed4badcdf3
https://github.com/user-attachments/assets/78e22712-3a31-4188-b53d-08851885e5c4
Type of changes
Checklist
npm run lint
)Live Previews
https://deploy-preview-41016--twbs-bootstrap.netlify.app/docs/5.3/getting-started/introduction/