Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strike in and out animation on hover

I'm trying to make a strikethrough animation like the effect here:

Strikethrough effect on hover

The strike line appears from left to right and disappears from left to right.

@keyframes strike {
  0% {
    width: 0;
  }
  50% {
    width: 100%;
  }
  100% {
    width: 0;
  }
}

.strike {
  position: relative;
}

.strike:hover:after {
  content: ' ';
  position: absolute;
  top: 50%;
  left: 0;
  width: 100%;
  height: 1px;
  background: black;
  animation-name: strike;
  animation-duration: 1s;
  animation-timing-function: linear;
  animation-iteration-count: 1;
  animation-fill-mode: fill;
  animation-direction: normal;
}
<div>
  The text in the span <span class="strike">is what I want to strike out</span>.
</div>

Is there a way to achieve that only with CSS ?

like image 915
sonia maklouf Avatar asked Jun 19 '17 14:06

sonia maklouf


People also ask

How do I stop an animation from hover?

By setting the animation-play-state to paused on hover, the animation will pause, your click targets will stop moving, and your users will be happy.

How do you animate lines in CSS?

In CSS, it is not possible to animate text-decoration: line-through width. However, you can use a pseudo-element instead (such as ::before or ::after ) to create a strikethrough line that grows in width, for example, from 0 to 100% on hover.


2 Answers

You can use transforms and transform-origin in the keyframe animation to make strike-through apear from left to right and disapear from left to right.
The point is to scale the pseudo element on the X axis for 0 to 1 with a transform origin on the left then back to 0 with a transform origin on the right (similar to this hover effect see last example of the answer).

And here is an example with your markup :

@keyframes strike{
  0%     { transform-origin:  0% 50%;transform:scaleX(0); }
  50%    { transform-origin:  0% 50%;transform:scaleX(1);  }
  50.01% { transform-origin:100% 50%;transform:scaleX(1);  }
  100%   { transform-origin:100% 50%;transform:scaleX(0);  }
}
.strike {
  position: relative;
}
.strike:hover:after {
  content: ' ';
  position: absolute;
  top: 50%;
  left: 0;
  width: 100%;
  height: 1px;
  background: black;
  animation: strike .75s ease-in-out forwards;
}
<div>
    The text in the span <span class="strike">is what I want to strike out</span>.
</div>
like image 198
web-tiki Avatar answered Oct 10 '22 01:10

web-tiki


Here is a sample using transition, where it on hover toggle left/right position.

.strike {
  position: relative;
}
.strike::after {
  content: ' ';
  position: absolute;
  top: 50%;
  left: 0;
  right: 100%;
  height: 1px;
  background: black;
}
.strike:hover::after {
  right: 0;
  left: 100%;
  transition: right .5s .0s, left .5s .5s;  
}
<div>
  The text in the span <span class="strike">is what I want to strike out</span>.
</div>
like image 38
Asons Avatar answered Oct 10 '22 01:10

Asons