【发布时间】:2015-01-16 22:07:37
【问题描述】:
我使用以下 JavaScript 代码启动 CSS 动画(element 是 div,name 是现有规则的名称,destination 是元素应该移动到的位置)。第一次调用此函数时,它按预期工作(div 轻轻移动到其最终目的地)。第二次,它只是跳转到目的地(因为我明确设置了left 和top),但没有发生任何移动。如果我在发生名称分配的行中设置断点 (webkitAnimationName),则动画将像第一次一样执行。我需要延迟吗?
function flyTo(element, name, destination) {
var rule = findKeyframesRule(name);
if (rule != null) {
element.style.webkitAnimationName = "none";
// remove the existing 0% and 100% rules
rule.deleteRule("from");
rule.deleteRule("to");
// create new 0% and 100% rules
rule.insertRule("from {top: " + element.style.top + "; left: " + element.style.left + ";}");
rule.insertRule("to {top: " + px(destination.y) + "; left: " + px(destination.x) + ";}");
// assign the animation to our element (which will cause the animation to run)
element.style.left = px(destination.x);
element.style.top = px(destination.y);
element.style.webkitAnimationDuration = "1s";
element.style.webkitAnimationTimingFunction = "linear";
element.style.webkitAnimationName = name;
} else {
element.style.left = px(destination.x);
element.style.top = px(destination.y);
}
}
我正在使用谷歌浏览器
【问题讨论】:
-
找到解决方法:设置动画名称后,我调用了一个延迟函数,将动画名称重置为“none”。如果名称被清除并在一段代码中设置,浏览器将无法识别更改并且不执行任何操作。
标签: javascript css animation