Home > Article > Web Front-end > js control keyframes
This article shares with you the code about js controlling keyframes. Friends in need can refer to it
createKeyFramesStyleSheetRules() { //创建一个单独的css文件存放动画 let style = null, sheet = null, token = null, dist = null, len = null, rules = null; len = this.loopSlideObjs.length-1; //计算移动到最后一张图片需要的距离 dist = this.loopContainer.totalHeight - this.loopSlideObjs[len].height; // 创建css文件 style = document.createElement('style'); style.setAttribute('type', 'text/css'); document.head.appendChild(style); // 判断是否是webkit浏览器内核 token = window.WebKitCSSKeyframesRule? '-webkit-':''; // 添加keyframes动画的样式 rules = '@' + token + 'keyframes imagesLoopMove{from{transform: translate3d(0,0px,0);}to{transform: translate3d(0,-' + dist + 'px,0);}}'; sheet = style.sheet; sheet.insertRule(rules, 0); // 给轮播图片添加动态的样式 rules = '.loop-animate{animation: imagesLoopMove ' + 200 + 'ms linear 0ms infinite;}'; sheet.insertRule(rules, 1); this.animateStyle = style; this.cssRule = sheet.cssRules[1]; this.animateTimeCurve(); console.group('function createKeyFramesStyleSheetRules'); console.log('dist:', dist); console.log('style:', style); console.log('sheet:', sheet); console.log('token:', token); console.log('cssRule:', this.cssRule); console.groupEnd('End'); len = null; dist = null; style = null; sheet = null; token = null; rules = null; }
Note, if it reports a DOMExpersion error, then it is a writing error in your cssRules, such as @keyframes missing a name, etc.
createKeyFramesStyleSheetRules() { //创建一个单独的css文件存放动画 let style = null, sheet = null, token = null, dist = null, len = null, rules = null; len = this.loopSlideObjs.length-1; //计算移动到最后一张图片需要的距离 dist = this.loopContainer.totalHeight - this.loopSlideObjs[len].height; // 创建css文件 style = document.createElement('style'); style.setAttribute('type', 'text/css'); document.head.appendChild(style); // 判断是否是webkit浏览器内核 token = window.WebKitCSSKeyframesRule? '-webkit-':''; // 添加keyframes动画的样式 rules = '@' + token + 'keyframes imagesLoopMove{from{transform: translate3d(0,0px,0);}to{transform: translate3d(0,-' + dist + 'px,0);}}'; sheet = style.sheet; sheet.insertRule(rules, 0); // 给轮播图片添加动态的样式 rules = '.loop-animate{animation: imagesLoopMove ' + 200 + 'ms linear 0ms infinite;}'; sheet.insertRule(rules, 1); this.animateStyle = style; this.cssRule = sheet.cssRules[1]; this.animateTimeCurve(); console.group('function createKeyFramesStyleSheetRules'); console.log('dist:', dist); console.log('style:', style); console.log('sheet:', sheet); console.log('token:', token); console.log('cssRule:', this.cssRule); console.groupEnd('End'); len = null; dist = null; style = null; sheet = null; token = null; rules = null; }
Note, if it reports a DOMExpersion error, then it is a writing error in your cssRules, such as @keyframes Missing names, etc.
Related recommendations:
10 recommended articles about keyframes
The above is the detailed content of js control keyframes. For more information, please follow other related articles on the PHP Chinese website!