javascript - jQuery selector to check if an element is animating to hidden - Stack Overflow

Is there a way to tell if an element is either hidden or is currently in the process of hiding (via an

Is there a way to tell if an element is either hidden or is currently in the process of hiding (via 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?

Is there a way to tell if an element is either hidden or is currently in the process of hiding (via 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?

Share Improve this question asked May 12, 2010 at 0:48 nickfnickf 547k199 gold badges658 silver badges727 bronze badges 6
  • How did you end up implementing this? – alex Commented Jun 3, 2010 at 5:54
  • Setting a flag in data when the animation starts, and checking for that flag again. – nickf Commented Jun 3, 2010 at 7:14
  • @nickf Ah, a solution, but not so elegant! I would love to see you get the custom selector working :) – alex Commented Jun 3, 2010 at 14:37
  • @alex - haha, a challenge? I don't think using setTimeout as you proposed would work, since then the selection would be running asynchronously. There must be some internal "target opacity" value... – nickf Commented Jun 3, 2010 at 16:12
  • @nickf Yeah that is what I thought... I wonder if it's exposed somehow. I might ask a question here :) – alex Commented Jun 3, 2010 at 22:50
 |  Show 1 more ment

3 Answers 3

Reset to default 3

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;
 }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744364346a4570624.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信