Native Js implements element fading/fading (the principle is to modify the css transparency of elements)

Source: Internet
Author: User

I often see the gradient display of images on the webpage and write one by myself.
The principle is to modify the css transparency of an element.
Online Preview effect: http://jsfiddle.net/dtdxrk/BHUp9/embedded/result/ Copy codeThe Code is as follows: <! DOCTYPE html>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> native Js element gradient/gradient method </title>
</Head>
<Body>
<Button id = "show"> fade-in </button>
<Button id = "hiden"> fade away </button>

<Script type = "text/javascript">
Function alphaPlay (obj, method) {// method has two values: show or hiden.
Var n = (method = "show ")? Zero: 100,
Ie = (window. ActiveXObject )? True: false;
Var time = setInterval (function (){
If (method = "show "){
If (n <100 ){
N + = 10;
If (ie ){
Obj.style.css Text = "filter: alpha (opacity =" + n + ")";
} Else {
(N = 100 )? Obj. style. opacity = 1: obj. style. opacity = "0." + n;
}
} Else {
ClearTimeout (time );
}
} Else {
If (n> 0 ){
N-= 10;
If (ie ){
Obj.style.css Text = "filter: alpha (opacity =" + n + ")";
} Else {
Obj. style. opacity = "0." + n;
}
} Else {
ClearTimeout (time );
}
}
}, 30 );
}
Var img = document. getElementById ("img ");
Document. getElementById ("show"). onclick = function (){
AlphaPlay (img, "show ");
}
Document. getElementById ("hiden"). onclick = function (){
AlphaPlay (img, "hiden ");
}
</Script>
</Body>
</Html>
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.