[英]CSS Transition on JS createElement() Working Intermittently
I'm using JavaScript to create a div and I'm adding transition: .5s linear top;
我使用JavaScript创建div,然后添加transition: .5s linear top;
onmousedown
, the div is supposed to move to the top and then it is deleted using JS remove()
. onmousedown
,div应该移到顶部,然后使用JS remove()
删除它。 Sometimes the transition works, sometimes not. 有时过渡有效,有时无效。
I suspect that something in the JS is causing it ... What am I missing? 我怀疑是JS中的某种原因导致了...我想念什么?
var redBox = document.createElement("div"), intSet; document.onmousedown = function(){ intSet = setInterval("animate()", 1000); } document.onmouseup = function(){ clearInterval(intSet); } function animate(){ redBox.classList.add("redBox"); document.body.appendChild(redBox); setTimeout (function() { redBox.style.top = "0"; },0); setTimeout (function(){ redBox.remove(redBox.selectedIndex); },500); }
.redBox{ background:red; height: 100px; width: 100px; top: 300px; position: absolute; transition: .5s linear top; }
I figured this out. 我想通了。 Thanks to @Thomas. 感谢@Thomas。
var redBox = document.createElement("div"), intSet; document.onmousedown = function(){ intSet = setInterval("animate()", 1000); } document.onmouseup = function(){ clearInterval(intSet); } function animate(){ redBox.classList.add("redBox"); document.body.appendChild(redBox); redBox.scrollWidth; redBox.style.top = "0"; setTimeout (function(){ redBox.remove(redBox.selectedIndex); },500); }
.redBox{ background:red; height: 100px; width: 100px; top: 300px; position: absolute; transition: .5s linear top; }
I replaced the setTimeout(function(){},0);
我替换了setTimeout(function(){},0);
with redBox.scrollWidth;
与redBox.scrollWidth;
. 。 The CSS animation is now running smoothly ;) CSS动画现在运行平稳;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.