Home>Article>Web Front-end> How to achieve continuous fade-in and fade-out in javascript

How to achieve continuous fade-in and fade-out in javascript

青灯夜游
青灯夜游 Original
2021-10-18 15:48:49 2501browse

Implementation method: 1. Use querySelector() to obtain the specified element object; 2. Use the "element object.style.opacity = Math.sin(2 * Math.PI * time)" statement to control the fade-in or fade-out Effect; 3. Use recursion to achieve continuous fade-in and fade-out.

How to achieve continuous fade-in and fade-out in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Javascript implements continuous fade in and out

You only need to find a periodic function with a scope of [0,1]. Use the value of the periodic function as the attribute value of opacity to control the fade-in or fade-out effect.

Use recursion to continue fading in and out.

Implementation code:

床前明月光,疑是地上霜。