欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js控制keyframes

程序员文章站 2022-03-16 19:04:41
...
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;
            }

注意,如果它报DOMExpersion错误,那么就是你的cssRules书写错误,例如@keyframes缺少名字等等

相关标签: js @keyframes