jQuery event to trigger action when a div is made visible

后端 未结 22 2265
你的背包
你的背包 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:20

    What helped me here is recent ResizeObserver spec polyfill:

    const divEl = $('#section60');
    
    const ro = new ResizeObserver(() => {
        if (divEl.is(':visible')) {
            console.log("it's visible now!");
        }
    });
    ro.observe(divEl[0]);
    

    Note that it's crossbrowser and performant (no polling).

提交回复
热议问题