Event listener for when element becomes visible?

后端 未结 9 2027
感情败类
感情败类 2020-11-28 05:36

I am building a toolbar that is going to be included into a page. the div it is going to be included in will default to display:none. Is there a way i can p

9条回答
  •  情深已故
    2020-11-28 06:15

    There is at least one way, but it's not a very good one. You could just poll the element for changes like this:

    var previous_style,
        poll = window.setInterval(function()
    {
        var current_style = document.getElementById('target').style.display;
        if (previous_style != current_style) {
            alert('style changed');
            window.clearInterval(poll);
        } else {
            previous_style = current_style;
        }
    }, 100);
    

    The DOM standard also specifies mutation events, but I've never had the chance to use them, and I'm not sure how well they're supported. You'd use them like this:

    target.addEventListener('DOMAttrModified', function()
    {
        if (e.attrName == 'style') {
            alert('style changed');
        }
    }, false);
    

    This code is off the top of my head, so I'm not sure if it'd work.

    The best and easiest solution would be to have a callback in the function displaying your target.

提交回复
热议问题