在悬停时进出动画

时间:2017-06-19 14:45:22

标签: css css3 css-animations

我试图像这里的效果一样制作删除线动画

Strikethrough effect on hover

点击线从左到右显示 ,从从左到右消失。



@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>
&#13;
&#13;
&#13;

有没有办法只用 CSS 来实现?

2 个答案:

答案 0 :(得分:9)

您可以在关键帧动画中使用变换和变换原点,使从左到右突破apear,从左到右消失
关键是要将X轴上的伪元素缩放为0到1,左边是变换原点,然后是右边的变换原点返回0(类似于此hover effect,请参见答案的最后一个示例)

以下是您的标记示例:

&#13;
&#13;
@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;
}
&#13;
<div>
    The text in the span <span class="strike">is what I want to strike out</span>.
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

以下是使用transition的示例,其中鼠标悬停切换左/右位置。

&#13;
&#13;
.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;  
}
&#13;
<div>
  The text in the span <span class="strike">is what I want to strike out</span>.
</div>
&#13;
&#13;
&#13;