[英]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? 有办法解决吗?
Maybe this: scrollWidth/scrollHeight give invalid dimensions 也许这样: scrollWidth / scrollHeight给出无效的尺寸
Or this: Is "ScrollWidth" property of a span not working on Chrome? 或这样: 跨度的“ ScrollWidth”属性在Chrome上不起作用吗?
can help you ;-) 可以帮助你 ;-)
Regards. 问候。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.