How to write onshow event using javascript/jquery?

后端 未结 4 954
温柔的废话
温柔的废话 2021-02-02 16:41

I have an anchor tag on my page, i want an event attached to it, which will fire when the display of this element change.

How can i write this event? and catch whenever

4条回答
  •  悲哀的现实
    2021-02-02 17:29

    The code from this link worked for me: http://viralpatel.net/blogs/jquery-trigger-custom-event-show-hide-element/

    (function ($) {
      $.each(['show', 'hide'], function (i, ev) {
        var el = $.fn[ev];
        $.fn[ev] = function () {
          this.trigger(ev);
          return el.apply(this, arguments);
        };
      });
    })(jQuery); 
    
    $('#foo').on('show', function() {
          console.log('#foo is now visible');
    });
    
    $('#foo').on('hide', function() {
          console.log('#foo is hidden');
    });
    

    However the callback function gets called first and then the element is shown/hidden. So if you have some operation related to the same selector and it needs to be done after being shown or hidden, the temporary fix is to add a timeout for few milliseconds.

提交回复
热议问题