2

If I hide an element with display: none;, and then show it at a later time with $(".fa-spin").show() the fa-spin animation doesn't work.

Note that everything works properly if the element is not hidden in the beginning but is hidden later with:

$(".fa-spin").hide()

This is the .fa-spin implementation:

.fa-spin {
  -webkit-animation: fa-spin 2s infinite linear;
  animation: fa-spin 2s infinite linear;
}

@-webkit-keyframes fa-spin {
  0% {
    -webkit-transform: rotate(0deg);
    transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(359deg);
    transform: rotate(359deg);
  }
}

Can you explain this behavior?

I am asking the reason of this behavior, not workarounds.

https://jsfiddle.net/md0ej7pt/

Andrea Casaccia
  • 4,802
  • 4
  • 29
  • 54

4 Answers4

6

jQuery.show() sets the display property to inline when called on the i element. According to WC3 documentation inline elements cannot be animated:

https://drafts.csswg.org/css-transforms-1/

Transformable element

A transformable element is an element in one of these categories: an element whose layout is governed by the CSS box model which is either a block-level or atomic inline-level element, or whose display property computes to table-row, table-row-group, table-header-group, table-footer-group, table-cell, or table-caption [CSS21]

To correct this, use Vallius's suggestion of setting display: inline-block instead of using show(), or wrap the element and hide the parent instead.

https://jsfiddle.net/359zLsdf/2/

<span class="coggy" style="display:none" ><i class="fa fa-cog fa-spin"  aria-hidden="true"></i></span>

<i class="fa fa-cog fa-spin myCog " style="display:none"  aria-hidden="true"></i>

$(".coggy").show();

$(".myCog").css("display","inline-block");
Andrea Casaccia
  • 4,802
  • 4
  • 29
  • 54
Nathan
  • 1,445
  • 2
  • 11
  • 29
0

You just want an explanation of the behavior?

From developer.mozilla.org:

In addition to the many different display box types, the value none lets you turn off the display of an element; when you use none, all descendant elements also have their display turned off. The document is rendered as though the element doesn't exist in the document tree.

Compared to the visibility property:

The visibility property can be used to hide an element while leaving the space where it would have been. It can also hide rows or columns of a table.

Daniel Lambert
  • 346
  • 3
  • 6
-1

Try this:

$(".fa-cog").css("display","inline-block");
Vcasso
  • 1,328
  • 1
  • 8
  • 14
-1

I found a similar question here;

CSS transition on an initially hidden elemement

It appears as though there are work-arounds; like to start with a z-index: -1 instead of display:none, or to start with the element transparent.

Community
  • 1
  • 1
Steve0
  • 2,233
  • 2
  • 13
  • 22