css - 为什么这个 css 动画不工作?

标签 css

你能告诉我为什么动画在这段代码中不起作用吗?

.allTopics {
  height: 100px;
  width: 100%;
  background: linear-gradient(135deg, #ff3300, #cc00cc);
  animation: topicBackground 10s ease infinite;
}

/* Safari 4.0 - 8.0 */
@-webkit-keyframes topicBackground {
  0%   { background-position: 0% 50%;}
  50%  { background-position: 100% 50%;}
  100% { background-position: 0% 50%;}
}

/* Standard syntax */
@keyframes topicBackground {
   0%   { background-position: 0% 50%;}
  50%  { background-position: 100% 50%;}
  100% { background-position: 0% 50%;}
}
<div class="allTopics"></div>

目标是使 div 具有渐变背景并在两种颜色之间来回动画。

最佳答案

您正在尝试为 background-position 设置动画,但这不适用于线性渐变。它不是图像,因此它没有 background-position

不幸的是,没有直接的方法在 CSS 中设置渐变动画,但您可以查看 this tutorial解决方法。

编辑

在评论中讨论后,您显然可以通过使用值高于 100% 的 background-size 属性来完成这项工作,例如 background-size: 400% 400%.

关于css - 为什么这个 css 动画不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46178593/

相关文章:

css - SharePoint:如何使 Web 部件中的链接处于非事件状态

css - 动态DIV高度

javascript - 调整大小时如何让浏览器边框锁定到div?

javascript - 在 js 运行之前,我必须在我的提交按钮上单击两次。为什么?

html - 外部容器(围绕 div 元素的图像)

Javascript获取img的宽度和高度并将其放入另一个类的css中

javascript - native 日期输入忽略 CSS

html - 表格在 HTML 页面上溢出

javascript - 在 javascript 中更改 CSS "left"属性在 IE 中不起作用

javascript - Vue.js 数据绑定(bind)样式 backgroundImage 不工作