U
U
Umid2015-09-20 18:35:55
css
Umid, 2015-09-20 18:35:55

How to fix a block after passing a certain part of the page in javascript??

Hello, you need to fix the div block, but only after the user has scrolled a certain part of the page!

For example, in the header part there is no block that needs to be fixed,
it is in the content part,
and when scrolling through the entire content, it must be fixed, and when raised to the header area, it remains in the content

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Inchin ☢, 2015-09-20
@In4in

If I understand correctly what you need, then I cannot but say that one hundred thousand two hundred and three times this topic has been discussed.

document.addEventListener("scroll", function(){
    var elem = document.querySelector("header");
    document.querySelector("#block").classList[elem.getBoundingClientRect().top < elem.offsetHeight ? "add" : "remove"]("visible");
//Когда header не видим, элементу #block присваивается класс visible
});

So, for reference:
getBoundingClientRect().top is the distance from the top of the element to the top border of the browser window.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question