简体   繁体   中英

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

elm.style.left = "0px";

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

You can wrap the line:

fade(element);

with setTimeout function:

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. So, if you want to wait 50 miliseconds, you can wrap it with setTimeout .

If you want to repeat the animation for the next click, you can set newleft = 0 inside the setTimeout function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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