到达元素时添加 class

Add class when arrive element

我得到了以下代码:

<body>
    <section id="first">
    ...
    </section>

    <section id="second">
    ...
    </div>

    <section id="third">
    ...
    </div>
</body>

我试图在页面到达垂直滚动的 div 时向 section#third 添加一个 class,但没有使用 px 指定高度,只是当页面符合元素。

有人知道怎么做吗?

谢谢大家!

像这样JSFiddle?

每次滚动时调用:

$(window).scroll(function() {

如果您停止的时间超过 250 毫秒,它会检查您是否显示了想要的部分,即:

clearTimeout($.data(this, 'scrollTimer'));
        $.data(this, 'scrollTimer', setTimeout(function() {
            var eTop = $('section#third').offset().top;
            if(eTop - ($(window).scrollTop()+$(window).height()) < 0) {
                alert('I am showing!');
            }
        }, 250));

参考文献(部分摘录)和进一步研究:

Scroll detection

Getting element's location relative to window