简体   繁体   English

如何绕过css过渡效果,使用JavaScript?

[英]How to bypass the css transition effect, using JavaScript?

I want to create a css transition and it is important, that I can "reset" the width of my "box" in advance, each time my function is called.我想创建一个 css 过渡,重要的是,每次调用我的 function 时,我都可以提前“重置”我的“盒子”的宽度。

By just setting the width of the box to zero, the element shrinks with animation.只需将框的宽度设置为零,元素就会随着 animation 缩小。 But I want to reset without animation.但我想在没有 animation 的情况下重置。 So I created a class which disables and enables the effect before and after resetting.所以我创建了一个 class ,它在重置前后禁用和启用效果。 Unfortunately this doesn't work.不幸的是,这不起作用。 I guess because JavaScript is asynchronous and it just flys over the width=0 command.我猜是因为 JavaScript 是异步的,它只是飞过 width=0 命令。 I figured out, that if I interrupt the runtime with an alert before removing the "notransition" it works, but of course this is not a valid way.我发现,如果我在删除“notransition”之前用警报中断运行时,它会起作用,但这当然不是一种有效的方法。

I always wanted to have a solution for this async problem.我一直想为这个异步问题找到一个解决方案。 I would be very happy for a explanation and solution for my attempt and maybe an even better solution for the resetting.我会很高兴为我的尝试提供解释和解决方案,也许还有更好的重置解决方案。 Thank you!谢谢!

 function test() { var duration = 5; document.getElementById("box").classList.add('notransition'); document.getElementById("box").style.width = 0 + "%" document.getElementById("box").classList.remove('notransition'); //Fill progress bar document.getElementById("box").style.setProperty('transition-duration', duration + 's'); document.getElementById("box").style.width = 100 + "%" setTimeout(function () { console.log("Progress finished") }, duration * 1000); }
 * { box-sizing: border-box; } html { padding: 0; margin: 0; width: 100%; } body { margin: 0; width: 100%; padding: 20px; } #box { border: 1px solid red; height: 20px; width: 10%; background-color: rgba(219, 72, 72, 0.3); transition-property: width; transition-duration: 0s; transition-timing-function: linear; }.notransition { -webkit-transition: none;important: -moz-transition; none:important; -o-transition: none;important; transition: none !important; }
 <div id="box" onclick="test()"></div>

Maybe use setTimeout() set time to zero to let the JS first execute the width:0% and then run the animation ?也许使用setTimeout()将时间设置为零让JS首先执行width:0%然后运行animation

 function test() { var duration = 5; document.getElementById("box").classList.add('notransition'); document.getElementById("box").style.width = 0 + "%" document.getElementById("box").classList.remove('notransition'); setTimeout(function(){ document.getElementById("box").style.setProperty('transition-duration', duration + 's'); document.getElementById("box").style.width = 100 + "%" setTimeout(function () { console.log("Progress finished") }, duration * 1000); },0) }
 * { box-sizing: border-box; } html { padding: 0; margin: 0; width: 100%; } body { margin: 0; width: 100%; padding: 20px; } #box { border: 1px solid red; height: 20px; width: 10%; background-color: rgba(219, 72, 72, 0.3); transition-property: width; transition-duration: 0s; transition-timing-function: linear; }.notransition { -webkit-transition: none;important: -moz-transition; none:important; -o-transition: none;important; transition: none !important; }
 <div id="box" onclick="test()"></div>

Using two classes to toggle between, specifically for the transition-delay property, it works quite fine.使用两个类来切换,特别是对于transition-delay属性,它工作得很好。 You also have to allow a small delay to have the with restored to 10% before the animation starts.在 animation 启动之前,您还必须允许一个小的延迟将其恢复到 10%。

 function test() { // "normal" transition delay is zero seconds let box = document.getElementById("box"); box.style.width = "10%"; // Allow a small delay (10ms) for the width to update on screen setTimeout(function() { // Change the transition delay and the width box.classList.remove('normal'); box.classList.add('transitionning'); box.style.width = "100%"; // When completed, restore the "normal" transition delay setTimeout(function() { console.log("Progress finished") box.classList.remove('transitionning'); box.classList.add('normal'); }, 5000); }, 10) }
 * { box-sizing: border-box; } html { padding: 0; margin: 0; width: 100%; } body { margin: 0; width: 100%; padding: 20px; } #box { border: 1px solid red; height: 20px; width: 10%; background-color: rgba(219, 72, 72, 0.3); transition-property: width; transition-timing-function: linear; }.normal { transition-duration: 0s; }.transitionning { transition-duration: 5s; }
 <div id="box" class="normal" onclick="test()"></div>

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

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