Implementation of JavaScript gradient effect

Source: Internet
Author: User

Let's take a look at the demo:

  • Simplified
  • Ming
  • Now
  • Generation
  • Magic
  • Method

The transparency of the above elements increases gradually when the mouse moves up, while the transparency decreases gradually when the mouse moves out.

Key code:

var speed = 0;if(target>obj.alpha){speed = 5;}else{speed = -5;}

Based on the comparison between the target value and the current value, it is determined whether the speed is positive or negative.

for(i=0; i < runs_li.length; i++){runs_li[i].timer = null;runs_li[i].alpha = 30;runs_li[i].onmouseover = function(){startrun(this,100);}runs_li[i].onmouseout = function(){startrun(this,30);}}

Each element is added with its own transparency value, and their respective transparency changes are separated.

All code:

<Style> # runs {width: 300px; margin: 10px auto;} # runs li {width: 80px; height: 80px; background: # 06c; list-style: none; float: left; margin: 10px; display: inline; filter: alpha (opacity = 30); opacity: 0.3 ;}</style> <script> window. onload = function () {var runs = document. getElementById ("runs"); var runs_li = runs. getElementsByTagName ("li"); var I = 0; for (I = 0; I <runs_li.length; I ++) {runs_li [I]. timer = null; runs_li [I]. alpha = 30; runs_li [I]. onmouseover = function () {startrun (this, 100);} runs_li [I]. onmouseout = function () {startrun (this, 30) ;}} function startrun (obj, target) {clearInterval (obj. timer); obj. timer = setInterval (function () {var speed = 0; if (target> obj. alpha) {speed = 5;} else {speed =-5;} if (obj. alpha = target) {clearInterval (obj. timer);} else {obj. alpha = obj. alpha + speed; obj. style. filter = "alpha (opacity =" + obj. alpha + ")"; obj. style. opacity = obj. alpha/100 ;}}, 30 )} </script> <ul id = "runs"> <li> simplified </li> <li> Ming </li> <li> current </li> <li> generation </li> <li> magic </li> <li> method </li> </ul>

Related Article

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.