【问题标题】:css animation on "content:" doesn't work on Safari and Firefox“内容:”上的 css 动画在 Safari 和 Firefox 上不起作用
【发布时间】:2017-02-22 23:29:43
【问题描述】:

我在:before 上设置了一个动画,它在 Chrome 上运行良好,但在 Safari(IOS9 iPhone 或 9 - El Capitan)上也不能在 Firefox 上运行。

.hey {
  color: white;
}
.hey:before {
  content: 'Hey \a there';
  white-space: pre;
  position: absolute;
  color: red;
 -moz-animation: heyThere 2250ms steps(11); /* for firefox */
  -webkit-animation: heyThere 2250ms steps(11); /* for safari, chrome & opera */
}

@keyframes heyThere {
    0% {content: "";}
    1% {content: "";}
    75% {content: "";}
    77% {content: "H";}
    80% {content: "He";}
    83% {content: "Hey";}
    85% {content: "Hey ";}
    87% {content: "Hey \a t";}
    90% {content: "Hey \a th";}
    93% {content: "Hey \a the";}
    95% {content: "Hey \a ther";}
    97% {content: "Hey \a there";}
    100% {content: "Hey \a there";}
}
@-moz-keyframes heyThere { /* animation for firefox */
    0% {content: "";}
    1% {content: "";}
    75% {content: "";}
    77% {content: "H";}
    80% {content: "He";}
    83% {content: "Hey";}
    85% {content: "Hey ";}
    87% {content: "Hey \a t";}
    90% {content: "Hey \a th";}
    93% {content: "Hey \a the";}
    95% {content: "Hey \a ther";}
    97% {content: "Hey \a there";}
    100% {content: "Hey \a there";}
}
@-webkit-keyframes heyThere { /* animation for chrome, safari & opera */
    0% {content: "";}
    1% {content: "";}
    75% {content: "";}
    77% {content: "H";}
    80% {content: "He";}
    83% {content: "Hey";}
    85% {content: "Hey ";}
    87% {content: "Hey \a t";}
    90% {content: "Hey \a th";}
    93% {content: "Hey \a the";}
    95% {content: "Hey \a ther";}
    97% {content: "Hey \a there";}
    100% {content: "Hey \a there";}
}
<div class="hey">Hey there</div>

【问题讨论】:

  • 我看到的一个明显问题是您没有未加前缀的 animation 属性。您应该始终拥有其中之一。另请注意,Firefox 在 4 年内不再需要 animation 的前缀。

标签: css animation css-selectors css-animations


【解决方案1】:

@asimovwasright 答案是正确的。

为了避免在 css 上出现如此多的重复,我使用带有 SCSS 的 @for 循环遍历所有可用跨度(在本例中为 8)

.hey {
    span {
        color: transparent;
        animation: heyThere 500ms ease-out;
        animation-fill-mode: forwards;

        $chars: 8;
        @for $i from 1 through $chars {
            &:nth-of-type(#{$i}) {
                animation-delay: 1200+70ms*$i;
            }
        }
    }
}

还有 HTML:

<h2 class="hey">
   <span>H</span>
   <span>e</span>
   <span>y</span>
   <br>
   <span>t</span>
   <span>h</span>
   <span>e</span>
   <span>r</span>
   <span>e</span>
</h2>

【讨论】:

  • 啊,scss 让它变得更好!
【解决方案2】:

正如@Pavan Kumar Jorrigala 所述,除了桌面版 Chrome 之外,无法为伪元素的 content 属性设置动画。

这是你想要达到的目标的一种向后的方法:

jsfiddle

.hey span {
  color: transparent;
  animation: heyThere 100ms;
  animation-fill-mode: forwards;
}

.hey span:nth-child(1) {
  animation-delay: 100ms;
}
.hey span:nth-child(2) {
  animation-delay: 300ms;
}
.hey span:nth-child(3) {
  animation-delay: 500ms;
}
.hey span:nth-child(4) {
  animation-delay: 700ms;
}
.hey span:nth-child(5) {
  animation-delay: 900ms;
}
.hey span:nth-child(6) {
  animation-delay: 1100ms;
}
.hey span:nth-child(7) {
  animation-delay: 1300ms;
}
.hey span:nth-child(8) {
  animation-delay: 1500ms;
}

@keyframes heyThere {
    0% {color: transparent;}
  100% {color: red;}
}
<div class="hey">
  <span>H</span>
  <span>e</span>
  <span>y&nbsp;</span>
  <span>T</span>
  <span>h</span>
  <span>e</span>
  <span>r</span>
  <span>e</span>
</div>

【讨论】:

  • 是的,我确实记得昨晚睡着了这个调整,这是一个非常聪明的方法 x)
  • 是的,这有点疯狂,而且扩展性不是很好,但它确实有效! :-) 为什么不是 JS?您可以编写一个更具扩展性的函数。
  • 因为这是网站的开始(介绍),我不想在加载时使用 JS。而且 CSS 比 JS 更流畅/更快。我也想支持没有 JS 的网站。我正在做的一个个人项目;)
  • 还不错:-)
  • 这是在实践中 ;) www.sandrina-p.net
【解决方案3】:

我也翻遍了整个web我没有发现任何东西,根据下面的参考我们只能用JavaScript来实现。

在我自己的测试中,动画内容只能在稳定的桌面上运行 Chrome(撰写本文时为 v46)。其他地方不支持。没有野生动物园 在桌面或 iOS 上。没有火狐。没有 IE。这些浏览器中的每一个都将 忽略动画,只显示伪中的原始内容 元素。

Reference

【讨论】:

    【解决方案4】:

    尝试在 :before 中添加显示属性。例如,添加 display:block;看看这是否会影响动画。

    【讨论】:

    • 您在发布答案之前尝试过这个吗?请在提交之前测试解决方案;您已经意识到您的答案并不能解决 OP 的问题。
    【解决方案5】:

    在 safari 和 firefox 中,代码不是动画,相反,您必须编写 -moz-animation 和 -webkit-animation,如下代码:

    .hey {
      color: white;
    }
    .hey:before {
      content: 'Hey \a there';
      white-space: pre;
      position: absolute;
      color: red;
      animation: heyThere 2250ms steps(11);
      -moz-animation: heyThere 2250ms steps(11); /* for firefox */
      -webkit-animation: heyThere 2250ms steps(11); /* for safari, chrome & opera */
    }
    
    @keyframes heyThere {
        0% {content: "";}
        1% {content: "";}
        75% {content: "";}
        77% {content: "H";}
        80% {content: "He";}
        83% {content: "Hey";}
        85% {content: "Hey ";}
        87% {content: "Hey \a t";}
        90% {content: "Hey \a th";}
        93% {content: "Hey \a the";}
        95% {content: "Hey \a ther";}
        97% {content: "Hey \a there";}
        100% {content: "Hey \a there";}
    }
    @-moz-keyframes heyThere { /* animation for firefox */
        0% {content: "";}
        1% {content: "";}
        75% {content: "";}
        77% {content: "H";}
        80% {content: "He";}
        83% {content: "Hey";}
        85% {content: "Hey ";}
        87% {content: "Hey \a t";}
        90% {content: "Hey \a th";}
        93% {content: "Hey \a the";}
        95% {content: "Hey \a ther";}
        97% {content: "Hey \a there";}
        100% {content: "Hey \a there";}
    }
    @-webkit-keyframes heyThere { /* animation for chrome, safari & opera */
        0% {content: "";}
        1% {content: "";}
        75% {content: "";}
        77% {content: "H";}
        80% {content: "He";}
        83% {content: "Hey";}
        85% {content: "Hey ";}
        87% {content: "Hey \a t";}
        90% {content: "Hey \a th";}
        93% {content: "Hey \a the";}
        95% {content: "Hey \a ther";}
        97% {content: "Hey \a there";}
        100% {content: "Hey \a there";}
    }
    

    `

    【讨论】:

    • 我这样做了,但仍然无法正常工作。其他动画效果很好,我认为这是因为内容:,不起作用
    猜你喜欢
    • 2021-11-02
    • 2021-09-01
    • 2014-02-28
    • 2020-05-21
    • 2019-01-10
    • 2019-02-17
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多