简体   繁体   English

注入JavaScript的CSS3关键帧不适用于Chrome

[英]CSS3 Keyframe injected with javascript does not work with Chrome

I am creating a keyframe with javascript because I want to know a specific element's width in order to apply an animation style using that. 我正在使用javascript创建关键帧,因为我想知道特定元素的宽度,以便使用该元素应用动画样式。

Here is the code: 这是代码:

var animation = false,
    animationstring = 'animation',
    prefix = '',
    domPrefixes = 'Webkit Moz O ms'.split(' '),
    pfx  = '',
    elm = document.querySelector('.marquee');

// Checks if the animation implementation is unprefixed
if( elm.style.animationName ) { animation = true; }

// Apply correct prefixes if the animation implementation has prefixes
if( animation === false ) {
    for( var i = 0; i < domPrefixes.length; i++ ) {
        if( elm.style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) {
            pfx = domPrefixes[ i ];
            animationstring = pfx + 'Animation';
            prefix = '-' + pfx.toLowerCase() + '-';
            animation = true;
            break;
        }
    }
}

elm.style[ animationstring ] = 'marquee 20s linear infinite';
var keyframes = '@' + prefix + 'keyframes marquee { '+
    '0% { ' + prefix + 'transform: translateX(100%);}'+
    '100% { ' + prefix + 'transform: translateX(-' + elm.scrollWidth + 'px);}'+
    '}';

document.styleSheets[0].insertRule( keyframes, 0 );

http://jsfiddle.net/69PXa/ http://jsfiddle.net/69PXa/

My problem (I think) is on row 27, I apply elm.scrollWidth as the value for translateX. 我的问题(我认为)是在第27行,我将elm.scrollWidth用作translateX的值。 This apparently breaks the keyframes in Chrome while it works as it should in Firefox. 当它在Firefox中正常工作时,这显然会破坏Chrome中的关键帧。 If I instead just use a fixed number the animation works. 如果我只使用固定的数字,动画将起作用。

Is there a way to solve this? 有办法解决吗?

You must apply the CSS to your .marquee after you define the actual keyframes. 定义实际关键帧后,必须将CSS应用于.marquee Right now you are telling the browser to animate using keyframes that don't exist yet . 现在你是在告诉浏览器使用尚不存在的关键帧动画。

JSFiddle 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM