简体   繁体   English

使用setInterval时如何设置元素的样式?

[英]How to set style of an element while using setInterval?

I want make effect for my button.我想让我的按钮生效。 When I clicked, I want increase left attribute of button.当我点击时,我想增加按钮的左属性。 And alter auto return left=0px并更改自动返回 left=0px

elm.style.left = "0px";

https://codepen.io/hunh-vn-thin/pen/jOEEPNz https://codepen.io/hunh-vn-thin/pen/jOEEPNz

You can wrap the line:您可以换行:

fade(element);

with setTimeout function:使用setTimeout功能:

setTimeout(function () {
    newleft = 0;

    fade(element);
}, 50);

In this case, it means: waiting until the animation is done.在这种情况下,它的意思是:等待动画完成。

It also because when you run setInterval function, all of the code inside the function will be called imediately.也因为当你运行setInterval函数时,函数内部的所有代码都会被立即调用。 So, if you want to wait 50 miliseconds, you can wrap it with setTimeout .所以,如果你想等待 50 毫秒,你可以用setTimeout包装它。

If you want to repeat the animation for the next click, you can set newleft = 0 inside the setTimeout function.如果要为下一次单击重复动画,可以在setTimeout函数中设置newleft = 0

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

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