简体   繁体   English

CSS3 动画:如何动画从暂停位置到重置位置?

[英]CSS3 animation: How to animate to from paused position to reset position?

startPosition is at left:0% startPosition位于left:0%
Move the box using the toggle button, then pause somewhere into the middle.使用切换按钮移动框,然后在中间某处暂停。
What I want to achieve is animating the box from the paused position to the start position .我想要实现的是将框从paused position动画到start position How do I achieve that?我如何做到这一点?

 const box={moving:false} ; function element(id){return document.getElementById(id)} function move(element){ box.moving=true; element.classList.add('moving') } function stop(element){ box.moving=false; element.classList.remove('moving') } function toggleBox(){ if(!box.moving){move(element('box'))} else{stop(element('box'))} } function resetBox(){ // what should i write here }
 @keyframes moving{ 0%{left:0%} 100%{left:100%} } .box{ animation-name:moving; animation-delay:0s; animation-duration:1s; animation-direction:alternate; animation-fill-mode:both; animation-timing-function:linear; animation-iteration-count:infinite; animation-play-state:paused; display:block; height:10px; width:10px; background:#06f; position:absolute; top:0; } .moving{ animation-play-state:running; } .container{position:relative;width:100px;border:1px solid #6666;height:10px;}
 <p> <button onclick="toggleBox()">Toggle</button> <button onclick="resetBox()">Reset</button> </p> <div class="container"> <span id="box" class="box"></span> </div>

An idea is to read the current state of the animation, remove the animation then reset the properties and they will animate back with a transition. 一个想法是读取动画的当前状态,删除动画然后重置属性,它们将以过渡动画返回。

I added a translation to the animation to avoid the overflow: 我为动画添加了翻译以避免溢出:

 const box = { moving: false }; function element(id) { return document.getElementById(id) } function move(element) { box.moving = true; element.classList.add('moving') } function stop(element) { box.moving = false; element.classList.remove('moving') } function toggleBox() { element('box').classList.remove('reset') if (!box.moving) { move(element('box')) } else { stop(element('box')) } } function resetBox() { element('box').style.left=window.getComputedStyle(element('box'), null).getPropertyValue("left"); element('box').style.transform=window.getComputedStyle(element('box'), null).getPropertyValue("transform"); element('box').classList.add('reset') setTimeout(function(){ element('box').style.left=0; element('box').style.transform="translateX(0)"; },1); } 
 @keyframes moving { 0% { left: 0% } 100% { left: 100%; transform: translateX(-100%); } } .box { animation: moving 1s infinite linear alternate; animation-play-state: paused; display: block; height: 10px; width: 10px; background: #06f; position: absolute; top: 0; left:0; transform:translateX(0); transition:0.5s all; } .reset { animation:none; } .moving { animation-play-state: running; } .container { position: relative; width: 100px; border: 1px solid #6666; height: 10px; } 
 <p> <button onclick="toggleBox()">Toggle</button> <button onclick="resetBox()">Reset</button> </p> <div class="container"> <span id="box" class="box"></span> </div> 

You can "hack" through it by setting its animation-name to none first and then revert it at the next frame. 您可以通过先将其animation-name设置为none来“破解”它,然后在下一帧将其还原。

The animation state will be reset. 动画状态将被重置。

 constbox = { moving: false }; function element(id) { return document.getElementById(id); } function move(element) { box.moving = true; element.classList.add('moving') } function stop(element) { box.moving = false; element.classList.remove('moving') } function toggleBox() { if (!box.moving) { move(element('box')); } else { stop(element('box')); } } function resetBox() { let name = element('box').style.animationName; element('box').style.animationName = 'none'; setTimeout(() => element('box').style.animationName = name, 0); } 
 @keyframes moving { 0% { left: 0%; } 100% { left: 100%; } } .box { animation-name: moving; animation-delay: 0s; animation-duration: 1s; animation-direction: alternate; animation-fill-mode: both; animation-timing-function: linear; animation-iteration-count: infinite; animation-play-state: paused; display: block; height: 10px; width: 10px; background: #06f; position: absolute; top: 0; } .moving { animation-play-state: running; } .container { position: relative; width: 100px; border: 1px solid #6666; height: 10px; } 
 <p> <button onclick="toggleBox()">Toggle</button> <button onclick="resetBox()">Reset</button> </p> <div class="container"> <span id="box" class="box"></span> </div> 

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

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