jQuery event to trigger action when a div is made visible

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

    The problem is being addressed by DOM mutation observers. They allow you to bind an observer (a function) to events of changing content, text or attributes of dom elements.

    With the release of IE11, all major browsers support this feature, check http://caniuse.com/mutationobserver

    The example code is a follows:

    $(function() {
      $('#show').click(function() {
        $('#testdiv').show();
      });
    
      var observer = new MutationObserver(function(mutations) {
        alert('Attributes changed!');
      });
      var target = document.querySelector('#testdiv');
      observer.observe(target, {
        attributes: true
      });
    
    });
    
    
    
    

提交回复
热议问题