I'm looking to dynamically add a class name "active" to sections on my webpage as I scroll down the page. I want to achieve this by checking if the window's top position is equal to any section's top position. Each section has a unique data-id attribute. I attempted the following code, but it didn't work as expected;
$(window).scroll(function () {
if ($(this).top == $("section").offset().top) {
$("section").addClass("active");
}
});