简体   繁体   English

如何在CSS过渡中停止转换

[英]How to stop transforms in a CSS transition

I have set a CSS transition like 我已经设置了CSS过渡

 transition: all 2s 

Then I apply a CSS to change the transform like: 然后,我应用CSS来更改转换,例如:

 transform: rotate(20deg); 

Transition starts. 过渡开始。

I want to stop it midway and have it stay there so I can then apply some other JS on it that is application dependent... what that is post-pausing is irrelevant to the question To test, I use: 我想将其停在中间并留在那里,以便随后可以在其上应用依赖于应用程序的其他JS ...暂停后的内容与要测试的问题无关 ,我使用:

 setTimeout(function() {
   ...
 }, 1000);

One crude way to stop the transition is to set CSS display to 'none'. 停止过渡的一种粗略方法是将CSS显示设置为“无”。 Setting transform to 'none' or empty string does not work. 将转换设置为“无”或空字符串不起作用。 The transition goes to the end for transform. 转换将结束进行转换。 Another trick of resetting the CSS to the current one, works for other properties but not for transforms. 将CSS重置为当前CSS的另一种技巧适用于其他属性,但不适用于转换。 Setting transition property to none or empty string also does not stop the transition's transform. 将transition属性设置为none或为空字符串也不会停止过渡的转换。

Surely there must be some way. 当然一定有办法。

Any suggestion? 有什么建议吗? Preferrably in JQuery I do not want to use animation. 最好在JQuery中, 我不想使用动画。

Why not using animation where you can easily manage the state: 为什么不使用可以轻松管理状态的动画:

 $('button').eq(0).click(function() { $('.box').css('animation-play-state', 'paused'); }); $('button').eq(1).click(function() { $('.box').css('animation', 'none'); }); 
 .box { margin: 50px; width: 100px; height: 100px; background: red; display:inline-block; vertical-align:top; animation: anime 10s forwards; } @keyframes anime { to { transform: rotate(180deg); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box"> </div> <button>Stop</button> <button>Reset</button> 

UPDATE UPDATE

Here is a way that you can try with transition: 您可以尝试以下方式进行过渡:

 $('button').eq(0).click(function() { $('.box').addClass('rotate'); }); $('button').eq(1).click(function() { var e = $('.box').css('transform'); // get the current state $('.box').css('transform', e); //apply inline style to override the one defined in the class }); 
 .box { margin: 50px; width: 100px; height: 100px; background: red; display:inline-block; vertical-align:top; transition: all 10s; } .rotate { transform: rotate(180deg); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box"> </div> <button>start</button> <button>stop</button> 

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

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