【发布时间】:2017-12-15 21:38:11
【问题描述】:
我在 body 元素上运行了一个 CSS 动画,它的默认持续时间为 20 秒。我希望能够通过用户输入来更改它,但我似乎无法让它工作。
我的默认 CSS 是:
body {
...
animation: MoveBG 20s ease infinite;
}
如果我运行这个 jQuery 代码:
$('body').css('animation', 'MoveBG 2s ease infinite');
动画仍会有 20 秒的持续时间。有趣的是,如果我跑步:
alert($('body').css('animation-duration'));
它会告诉我动画持续时间是 2 秒(显然不是)。
// the animation duration should change to 2s, but it still animates at 20s
var animation = 'MoveBG 2s ease infinite';
$('body').css('animation', animation);
// it even says that it is animate at 2s
//alert($('body').css('animation-duration'));
body {
background: linear-gradient(to right, #f00, #0f0, #00f);
background-size: 600% 100%;
-webkit-animation: MoveBG 20s ease infinite;
-moz-animation: MoveBG 20s ease infinite;
-o-animation: MoveBG 20s ease infinite;
animation: MoveBG 20s ease infinite;
}
@-webkit-keyframes MoveBG {
0% {
background-position: 0 0
}
50% {
background-position: 100% 0
}
100% {
background-position: 0 0
}
}
@-moz-keyframes MoveBG {
0% {
background-position: 0 0
}
50% {
background-position: 100% 0
}
100% {
background-position: 0 0
}
}
@-o-keyframes MoveBG {
0% {
background-position: 0 0
}
50% {
background-position: 100% 0
}
100% {
background-position: 0 0
}
}
@keyframes MoveBG {
0% {
background-position: 0 0
}
50% {
background-position: 100% 0
}
100% {
background-position: 0 0
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Jsfiddle:http://jsfiddle.net/sdobc5vx/2/
谁能看到我做错了什么?
这在 Chrome 37 中发生在我身上 - 但它看起来在 Firefox 30 中工作。嗯...
【问题讨论】:
-
这对我也不起作用。此外,我很确定 jQuery 现在会自动添加供应商前缀。
-
似乎是帧刷新问题。如果您隐藏并显示帧刷新。
$('body').css('animation', animation).hide().show(0);jsfiddle.net/a5nv1hq3
标签: javascript html css animation