John
John

Reputation: 13757

Prevent flickering while hovering CSS transform

While hovering the vertically rotating anchor if the mouse is not near the center of the image the animated transform flickers using the following code:

@keyframes spin {0% {transform: rotateX(0deg);} 100% {transform: rotateX(360deg);}}
a:focus, a:hover {animation: spin 0.9s 1 linear;}

How do I prevent the flickering without changing the HTML?

https://jsfiddle.net/jabcreations/ahcx0wfv/

Upvotes: 1

Views: 1548

Answers (2)

Temani Afif
Temani Afif

Reputation: 273710

An idea without changing the html is to move the animation to a pseudo element and you will avoid the lose of the hover since the main element won't rotate:

You can also simplify your logic using transition instead of animation:

p a {
  color: #000;
  display: inline-block;
  height: 50px;
  z-index:0;
  position:relative;
  color:transparent;
}
p a:before {
  content:attr(data-text);
  position:absolute;
  z-index:-1;
  top:0;
  left:0;
  right:0;
  bottom:0;
  color:#000;
  transition:transform 0.9s linear;
}
p a:focus::before,
p a:hover::before {
  transform: rotateX(360deg);
  background: radial-gradient(ellipse at center, #777 0%, #222 100%);
  color: #fff;
}
<p>some content here <a href="admin/" data-text="Admin">Admin</a> and more here</p>

Upvotes: 1

Kosh
Kosh

Reputation: 18423

To avoid getting :hover out of a and flickering you might move :hover from a to its parent:

@keyframes spin {
  100% {
    transform: rotateX(360deg);
  }
}

ul {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

li:hover {
  cursor: pointer;
}

a:focus,
li:hover a {
  animation: spin 0.9s 1 linear;
}

ul a {
  color: #000;
  display: inline-block;
  height: 50px;
}

ul a:focus,
ul li:hover a {
  background-image: radial-gradient(ellipse at center, #777 0%, #222 100%);
  color: #fff;
}
<ul>
  <li><a href="admin/">Admin</a></li>
  <li><a href="appointments/">Appointments</a></li>
  <li><a href="blog/">Blog</a></li>
  <li><a href="calendar/">Calendar</a></li>
  <li><a href="contact/">Contact</a></li>
  <li><a href="events/">Events</a></li>
  <li><a href="forms/">Forms</a></li>
  <li><a href="forums/">Forums</a></li>
  <li><a href="guestbook/">Guestbook</a></li>
  <li><a href="mail/">Mail</a></li>
  <li><a href="members/">Members</a></li>
  <li><a href="newsletter/">Newsletter</a></li>
  <li><a href="notifications/">Notifications</a></li>
  <li><a href="search/">Search</a></li>
</ul>

Upvotes: 3

Related Questions