gpt4 book ai didi

javascript - 如何使用 javascript 创建自定义关键帧 css 动画

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:46:42 32 4
gpt4 key购买 nike

我需要使用 javascript 创建和运行自定义 css3 动画。当我需要创建过渡时,我会这样写:

element.style[Modernizr.prefixed('transform')] = 'rotateY(50deg)';

当我需要动画元素时我应该写

element.style[Modernizr.prefixed('animation')] = "'test' 2s ease-out infinite";

但我无法以同样的方式为“打开”创建关键帧。当然,我可能会写类似

document.creteElement("style").innerHTML = rule;

但这是个肮脏的想法,所以在阅读之后 programmatically changing webkit-transformation values in animation rules我这样写:

var style = document.documentElement.appendChild(document.createElement("style")),
index = Modernizr._prefixes.length,
rule = "";

while(index--){
rule+="@"+Modernizr._prefixes[index]+"keyframes 'test'{ 0%{opacity:1;} 50%{opacity:0;} 100%{opacity:1;}} ";
}

style.sheet.insertRule(rule);
$(".mojo")[0].style[Modernizr.prefixed('animation')] = "'test' 2s ease-out infinite";

并得到错误:Uncaught Error: SYNTAX_ERR: DOM Exception 12

我做错了什么,我该如何做更合适?这看起来很可怕。

http://jsfiddle.net/silentimp/273e2/ — 测试

最佳答案

var keyframes = "@-webkit-keyframes{...}";
var s = document.createElement( 'style' );
s.innerHTML = keyframes;

source

https://github.com/krazyjakee/jQuery-Keyframes

关于javascript - 如何使用 javascript 创建自定义关键帧 css 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9777363/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com