This article mainly introduces how js implements a transparency gradient effect. it involves the javascript technique for achieving a gradient effect and is of great practical value, for more information about how to implement the transparency gradient of JavaScript, see the example in this article. 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:
Untitled DocumentScript window. onload = function () {var run = document. getElementById ("run"); var btn = document. getElementById ("btn"); var speed = 1; var timer = null; var alpha = 30; run. onmouseover = function () {startrun (100);} run. onmouseout = function () {startrun (30);} function startrun (target) {clearInterval (timer); timer = setInterval (function () {if (target> alpha) {speed = 2 ;}else {speed =-2 ;}if (alpha = target) {clearInterval (timer) ;}else {alpha = alpha + speed; run. style. filter = 'alpha (opacity = '+ alpha +') '; run. style. opacity = alpha/100; document. title = alpha ;}}, 30) }} script
I hope this article will help you design javascript programs.