Js implements transparency gradient, js implements transparency gradient
This document describes how to implement a progressive transparency effect in JavaScript. Share it with you for your reference. The specific analysis is as follows:
Here, the transparency of the element at the beginning is 30. When the mouse moves up, the transparency increases slowly until the transparency is stopped by 100. Move the mouse out, and the transparency decreases slowly to 30.
Key Aspect 1: Because you cannot directly obtain or change the transparency value, you can assign the transparency value to a variable, change the variable, and finally assign the value of the variable to the transparency value of the element.
var alpha=30;
Key Aspect 2: Determine the target value and the current transparency value to determine whether it is a positive speed or a negative speed.
if(target > alpha){speed = 2;}else{speed = -2;}
Key Aspect 3: If the transparency value reaches the target value, turn off the timer; otherwise, the transparency value continues to change. Finally, assign the value to the element. Because the transparency is incompatible, write two statements.
if(alpha == target){clearInterval(timer);}else{alpha = alpha + speed;run.style.filter = 'alpha(opacity='+alpha+')';run.style.opacity = alpha/100;document.title = alpha;}
Finally, run the following code:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
I hope this article will help you design javascript programs.