Answer the question
In order to leave comments, you need to log in
How to change the class of the body when scrolling to a specific element?
Hello.
It is necessary that when scrolling the page to a block with a specific id, a class is added to the body. And when scrolling above, the class was removed.
How can this be done with jQuery?
Answer the question
In order to leave comments, you need to log in
document.onscroll = function(){
var what = document.querySelector("#id").getBoundingClientRect().top;
document.body.classList.toggle(
"class", what <= window.innerHeight
);
}
$(document).scroll(function(){
var what = $("#id").offset().top,
pos = $(window).height() + $(window).scrollTop()
$("body").toggleClass("class", what <= pos);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question