Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 469
nickf Avatar asked May 12 '10 00:05

nickf


People also ask

How do you check if an element is hidden using jQuery?

You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0; , because they preserve space in the layout even they are not visible to the eye.

How can you tell if an element is currently being animated?

When animating elements with jQuery, you can detect if the animation is in progress by using $(':animated').

How do you know if an element is hidden?

Check if Element is Hidden with .is(":hidden") Alternatively, you can right-click on the page and select "Inspect" from the menu. In Firefox it's "Inspect element". .is(":hidden") will return true if the selected element is hidden. If it's not hidden, then it will return false .

What does show () do in jQuery?

The show() Method in jQuery is used to display the hidden and selected elements. Note: This method display the hidden elements which are using CSS display: none property. The elements are not visible whose visibility is hidden.


2 Answers

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.

like image 120
alex Avatar answered Sep 30 '22 13:09

alex


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.

like image 40
Jakub Hampl Avatar answered Sep 30 '22 13:09

Jakub Hampl