jQuery event to trigger action when a div is made visible

后端 未结 22 2505
你的背包
你的背包 2020-11-22 12:03

I\'m using jQuery in my site and I would like to trigger certain actions when a certain div is made visible.

Is it possible to attach some sort of \"isvisible\" even

22条回答
  •  孤城傲影
    2020-11-22 12:22

    redsquare's solution is the right answer.

    But as an IN-THEORY solution you can write a function which is selecting the elements classed by .visibilityCheck (not all visible elements) and check their visibility property value; if true then do something.

    Afterward, the function should be performed periodically using the setInterval() function. You can stop the timer using the clearInterval() upon successful call-out.

    Here's an example:

    function foo() {
        $('.visibilityCheck').each(function() {
            if ($(this).is(':visible')){
                // do something
            }
        });
    }
    
    window.setInterval(foo, 100);
    

    You can also perform some performance improvements on it, however, the solution is basically absurd to be used in action. So...

提交回复
热议问题