Javascript method for achieving transparency gradient effect _ javascript skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.