开发者

jQuery selector to check if an element is animating to hidden

Is there a way to tell if an element is either hidden or is currently in the process of hiding (via 开发者_JS百科an animation)? The only way I can think to do it is to store a flag in the element's data when you call show or hide, but I was wondering if there was another way?


Could you do a custom jQuery selector for it

 (function($) {
  var endOpacity,
      oldStep = jQuery.fx.step.opacity;

  $.fx.step.opacity = function( fx ) {
      endOpacity = fx.end;

      return oldStep(fx);
  };

$.expr[':'].hiding = function(obj){
  var $this = $(obj);

   return ($this.is(':hidden') || ($this.is(':animated') && endOpacity === 0));
};

})(jQuery);

This worked for me (it may require some more testing though).

So just add :hiding it will match hidden elements, and elements that are currently being animated to 0. It will now only match elements that are disappearing, not appearing.


You get the hidden one with $(":hidden") and then the animating ones with $(":animated") and with the :animated check the .queue() if it has the hide method inside.


You can check if the element is animated like this:

 if( !$('.your-element').is(':animated') ) {
   // do animation...
 } else {
   return false;
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜