How to make a Scrollspy using JQuery
What is Scroll Spy in Web Designing
ScrollSpy is technique in which the Navigation Menus options are automatically updated based on the scroll position of section/div/element that is visible on the screen or is active in the viewport. Having said that the element that is being spied should be in the same page.
How ScrollSpy works
ScrollSpy works on the principle that whenever the Element with an ID is visible on the screen or is almost the top of the screen, the specific navigation menu option is set active by applying CSS property to it. Adding a CSS, simply means adding a class to it which indicates the menu is "Active".
Making Scrollspy using JQuery
HTML Code
Section One
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Perferendis error aliquam vero quidem expedita, saepe, est earum fugiat iure beatae ex, voluptatem alias maiores! Voluptates officia cum reiciendis temporibus sit.
Section Two
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Perferendis error aliquam vero quidem expedita, saepe, est earum fugiat iure beatae ex, voluptatem alias maiores! Voluptates officia cum reiciendis temporibus sit..
Section Three
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Perferendis error aliquam vero quidem expedita, saepe, est earum fugiat iure beatae ex, voluptatem alias maiores! Voluptates officia cum reiciendis temporibus sit.
Section Four
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Perferendis error aliquam vero quidem expedita, saepe, est earum fugiat iure beatae ex, voluptatem alias maiores! Voluptates officia cum reiciendis temporibus sit.
Section Five
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Perferendis error aliquam vero quidem expedita, saepe, est earum fugiat iure beatae ex, voluptatem alias maiores! Voluptates officia cum reiciendis temporibus sit.
CSS Code
JS Code with Explaination as Comment
Scrollspy Output
Please view this output in Desktop Mode,(This example is not device responsive)
There are other ways to perform ScrollSpy.
- Using Bootstrap util.js
- Using Materialize js
Comments