I have an Id in a div <div id="Section1"> abc </div>
and link <a id="link" href="#Section1">Section1</a>
Question: When I scroll page and page reach at id #Section1
a class should be add in the link, link should like<a id="link" href="#Section1" class="ok">Section1</a>
You can use like this:
$(window).scroll(function (event) {
var scroll = $(window).scrollTop();
$('#link').toggleClass('ok',
//add 'ok' class when div position match or exceeds else remove the 'ok' class.
scroll >= $('#Section1').offset().top
);
});
//trigger the scroll
$(window).scroll();//ensure if you're in current position when page is refreshed
See the docs for toggleClass.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With