How does Javascript get the label displayed in the current window?

now there is an implementation requirement that when the page scrolls up and down, I need to get the label scrolled to the current visual area from time to time. Is there any way to do that?

at present, it is known that it is not easy to use the visible attribute. Pages often have anchor jumps, and the acquisition of this attribute is not very accurate

.
Mar.23,2021
There is no particularly good way for

. We can only take .getboundingClientRect () for each element, and then cooperate with document.body.scrollTop to judge that the performance will not be too good.


if you need to determine whether certain elements enter the window, you can use the following method:

var isInViewport = function (elem) {
    var bounding = elem.getBoundingClientRect();
    return (
        bounding.top >= 0 &&
        bounding.left >= 0 &&
        bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
        bounding.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
};
window.addEventListener('scroll', function (event) {
    if (isInViewport(elem)) { //elem
        
    }
}, false);

I don't know if I understand it correctly. See if this stickUp plugin is the effect you want
http://lirancohen.github.io/s.

.
Menu