Fire event when div is visible to visitor with jQuery?

前端 未结 4 1017
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 04:08

The function: I want to change the class of a certain item when a user see a certain div (scrolls down to it).

How I do it now: I\'

4条回答
  •  一个人的身影
    2021-01-01 04:42

    to judge the div is visible or not, you can:

    $(window).scroll(function(event) {
        console.log($(".target").offset().top < $(window).scrollTop() + $(window).outerHeight());
    });
    

    so, i don't think you need any plugin.

    just judge it by the expression like:

    if($(".target").offset().top < $(window).scrollTop() + $(window).outerHeight()) {
        // something when the .target div visible
    } else {
        // something when the .target div invisible
    }
    

提交回复
热议问题