Fire event when div is visible to visitor with jQuery?

前端 未结 4 1008
佛祖请我去吃肉
佛祖请我去吃肉 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:19

    This would probably be useful for you.

    Anyhow, you could probably do it without a plug-in and with a simple expression instead:

    var elem_top    = $("some_element").offset().top;
    var elem_height = $("some_element").height();
    var wind_height = $(window).height();
    var scrollTop   = $(window).scrollTop;
    
    if (elem_top > (wind_height + scrollTop) &&
       !(elem_top + elem_height) < wind_scrollTop) 
    {
        //The element is inside the screen (e.g. it is directly visible)
    }
    

提交回复
热议问题