Event detect when css property changed using Jquery

后端 未结 5 1106
北海茫月
北海茫月 2020-11-22 12:30

Is there a way to detect if the \"display\" css property of an element is changed (to whether none or block or inline-block...)? if not, any plugin? Thanks

5条回答
  •  梦谈多话
    2020-11-22 12:40

    Note

    Mutation events have been deprecated since this post was written, and may not be supported by all browsers. Instead, use a mutation observer.

    Yes you can. DOM L2 Events module defines mutation events; one of them - DOMAttrModified is the one you need. Granted, these are not widely implemented, but are supported in at least Gecko and Opera browsers.

    Try something along these lines:

    document.documentElement.addEventListener('DOMAttrModified', function(e){
      if (e.attrName === 'style') {
        console.log('prevValue: ' + e.prevValue, 'newValue: ' + e.newValue);
      }
    }, false);
    
    document.documentElement.style.display = 'block';
    

    You can also try utilizing IE's "propertychange" event as a replacement to DOMAttrModified. It should allow to detect style changes reliably.

提交回复
热议问题