Is it possible to animate a CSS line-through text-decoration?

旧街凉风 提交于 2019-12-23 18:44:05

问题


I am trying to animate with CSS the a line through on a bit of text, but it's not actually animating, just going from hidden to displayed. Can anyone advise if what I'm trying is actually possible? If not, is there another way to achieve this? HTML:

<div>
    The text in the span <span class="strike">is what I want to strike out</span>.
</div>

CSS:

@keyframes strike{
                from{text-decoration: none;}
                to{text-decoration: line-through;}
            }
 .strike{  
    -webkit-animation-name: strike; /* Chrome, Safari, Opera */
    -webkit-animation-duration: 4s; /* Chrome, Safari, Opera */
    animation-name: strike;
    animation-duration: 4s;
    animation-timing-function: linear;
    animation-iteration-count: 1;
    animation-fill-mode: forwards; 
    }

回答1:


You can use a pseudo like this

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



回答2:


It depends on how you want to animate it.

Since text-decoration-color is animatable, you can animate it from transparent to auto.

But this property is not widely supported yet.

@keyframes strike {
  from { text-decoration-color: transparent; }
  to { text-decoration-color: auto; }
}
.strike {  
  text-decoration: line-through;
  animation: strike 4s linear;
}
<div>
    The text in the span <span class="strike">is what I want to strike out</span>.
</div>



回答3:


According to W3Schools, the text-decoration property is not animatable.

However, if you use jQuery, you can. (See here)



来源:https://stackoverflow.com/questions/36267507/is-it-possible-to-animate-a-css-line-through-text-decoration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!