【问题标题】:Is it possible to animate a CSS line-through text-decoration?是否可以通过文本装饰为 CSS 行设置动画?
【发布时间】:2016-03-28 17:31:39
【问题描述】:

我正在尝试使用 CSS 为一段文本设置一条线的动画,但它实际上并不是动画,只是从隐藏到显示。谁能告诉我我正在尝试的是否真的可行?如果没有,是否有另一种方法来实现这一目标? 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; 
    }

【问题讨论】:

  • 可能无法使用文本修饰。但是你应该能够通过覆盖和定位一个 1px 高的 div 和一个边框并为 div 的宽度设置动画来伪造它。
  • 你有没有注意到你实际上不能告诉浏览器你想要动画看起来像什么?浏览器也不知道。所以它不会尝试。

标签: html css css-animations


【解决方案1】:

你可以像这样使用伪

注意(感谢Phlame),如果要敲击的行中断到第二行,这个从左到右的动画将不起作用。为此,需要使用另一个伪元素和一些脚本来正确定位两者。或者使用其他一些动画效果,例如就像Oriol's answer中建议的那样。

@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>

【讨论】:

  • 如果您不关心某些旧版浏览器支持(由于animationspseudo 元素),这将是可行的方法。很好的解决方案 LGSo
  • 只是一个警告:如果文本中断到另一行,这将不起作用
  • @Phlame -- 真的,谢谢,用注释更新了我的答案。
【解决方案2】:

这取决于您要如何为其设置动画。

由于text-decoration-color 是可动画的,您可以将其从transparent 动画化到auto

但此属性尚未得到广泛支持。

@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>

【讨论】:

  • 它适用于 Firefox 和 Chromium,带有一些实验性标志。
【解决方案3】:

这是accepted 答案的变体,使用图像提供动画“涂鸦”删除线。

html {
  font-family: Helvetica;
  font-size: 24px;
}
.strike { position:relative; }
.strike::after {
  content:' ';
  position:absolute;
  top:50%; left:-3%;
  width:0; height:10px;
  opacity:80%;
  transform:translateY(-50%);
  background:repeat-x url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB0AAAAKAQMAAAByjsdvAAAABlBMVEUAAADdMzNrjRuKAAAAAXRSTlMAQObYZgAAADdJREFUCNdj+MMABP8ZGCQY/h9g+MHw/AHzDwbGD+w/GBhq6h8wMNj/b2BgkP8HVMMPUsn+gQEAsTkQNRVnI4cAAAAASUVORK5CYII=);
  animation: strike 2s linear .3s 1 forwards;
}
@keyframes strike { to { width: 106%; } }
This thing and &lt;span class="strike"&gt;this thing and&lt;/span&gt; this thing.

【讨论】:

  • 这太棒了,但我不认为这是这个问题的答案。我知道您可以使用删除线(直线图像)图像而不是涂鸦图像,但是...
【解决方案4】:

根据W3Schoolstext-decoration 属性是不可动画的。

但是,如果您使用 jQuery,则可以。 (见here

【讨论】:

猜你喜欢
  • 2021-12-18
  • 2021-06-29
  • 1970-01-01
  • 2011-12-23
  • 1970-01-01
  • 2015-10-22
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多