简体   繁体   English

使用JQuery $(class).css()会暂停CSS Webkit动画吗?

[英]Using JQuery $(class).css() halts CSS webkit animations?

I have a CSS element referred to as .row, which looks as follows: 我有一个称为.row的CSS元素,其外观如下:

@-webkit-keyframes slideright {
    from        { background-position: left; }
    to      { background-position: right; }
}
.row {
width: 200%;
height: 100px;
-webkit-animation: slide 100s linear infinite;
}
.row.row-1 {
background-image: url(row/1.png);
-webkit-animation-name: slideright;
}

I want to change the animation speed (100s) using javascript or JQuery, to dynamic values. 我想使用JavaScript或JQuery将动画速度(100s)更改为动态值。 So I tried this: 所以我尝试了这个:

$('.row').css({
'width':                '200%',
'height':               '100px',
    '-webkit-animation':    'slide 50s linear infinite'
});

This causes the animation to stop playing; 这将导致动画停止播放; the row does not move. 该行不动。 I can change other values for instance height to 200px, and the height does change. 我可以将实例高度的其他值更改为200px,并且高度会发生变化。 The problem is, the animation is killed. 问题是动画被杀死了。

How can I solve this? 我该如何解决?

Does this have to be dynamic? 这必须是动态的吗? Meaning, do you already know the new speed, or is it calculated by your JS? 意思是,您已经知道新速度,还是由您的JS计算得出? If you know the speed you wish to change to, your better bet (I think) is to setup two classes, and swap between them as you need (rather than attempt to modify the webkit-animation property directly) 如果您知道要更改的速度,最好的选择是(我认为)设置两个类,然后根据需要在两个类之间交换(而不是尝试直接修改webkit-animation属性)

@-webkit-keyframes slideright {
    from        { background-position: left; }
    to      { background-position: right; }
}
.row {
width: 200%;
height: 100px;
-webkit-animation: slide 100s linear infinite;
}
.row.new {
-webkit-animation: slide 50s linear infinite;
width: 200%; /* change other properties if needed */
}

.row.row-1 {
background-image: url(row/1.png);
-webkit-animation-name: slideright;
}

And your jQuery: 和你的jQuery:

$('.row').addClass("new");

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

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