简体   繁体   English

如何暂停CSS过渡?

[英]How do I pause CSS transition?

I need to pause a CSS transition at any time during the transition. 在过渡期间,我需要随时暂停CSS过渡。 I know there is a solution with calculating the current value of the transitioning property and applying it directly, however this gave me "jumping" results in Safari and IE11. 我知道有一个解决方案,可以计算过渡属性的当前值并直接应用它,但是这使我在Safari和IE11中“跳转”结果。

What I though of is to increase the transition-duration value to something big, that is in my theory should have almost paused the transition, but it seems it's not the case: 我的目的是将transition-duration值增加到一个很大的值,按照我的理论,这应该几乎已经暂停了过渡,但事实并非如此:

https://jsfiddle.net/j8p0msff/ https://jsfiddle.net/j8p0msff/

$('#pause').on('click', function() {
    $('.ball').css('transition-duration: 5000s');
});

Any other ideas? 还有其他想法吗? Thanks! 谢谢!

Transitions 转场

A solution would be to remove the transition class and save the current state of styles ( margin-left in the example) when stopping. 一种解决方案是在停止时删除transition类并保存样式的当前状态(示例中为margin-left )。 And when starting just add the transition class again. 并且在开始时,只需再次添加transition类。

 var boxOne = document.getElementsByClassName('box')[0]; var running = false; var toggleTransition = function() { if(!running) { boxOne.classList.add('horizTranslate'); } else { var computedStyle = window.getComputedStyle(boxOne), marginLeft = computedStyle.getPropertyValue('margin-left'); boxOne.style.marginLeft = marginLeft; boxOne.classList.remove('horizTranslate'); } running = !running; } 
 .box { margin: 30px; height: 50px; width: 50px; background-color: blue; } .box.horizTranslate { -webkit-transition: 3s; -moz-transition: 3s; -ms-transition: 3s; -o-transition: 3s; transition: 3s; margin-left: 50% !important; } 
 <div class='box'></div> <button class='toggleButton' onclick='toggleTransition()'>Toggle</button> 

Animations 动画制作

You could use: animation-play-state 您可以使用: animation-play-state

With eg Javascript: document.getElementById('myId').style.animationPlayState = 'paused' 使用例如Javascript: document.getElementById('myId').style.animationPlayState = 'paused'

 var toggleAnimation = function(){ state = document.getElementById('myId').style.animationPlayState == 'paused' ? 'running' : 'paused'; document.getElementById('myId').style.animationPlayState = state; } 
 div#myId { width: 100px; height: 100px; background: red; position: relative; -webkit-animation: mymove 5s infinite; /* Safari 4.0 - 8.0 */ -webkit-animation-play-state: running; /* Safari 4.0 - 8.0 */ animation: mymove 5s infinite; animation-play-state: running; } /* Safari 4.0 - 8.0 */ @-webkit-keyframes mymove { from {left: 0px;} to {left: 200px;} } @keyframes mymove { from {left: 0px;} to {left: 200px;} } 
 <div id='myId'></div> <button onclick="toggleAnimation()">Toggle Animation</button> 

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

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