Javascript native encapsulates a function test instance code for fade-in and fade-out.

Source: Internet
Author: User

Javascript native encapsulates a function test instance code for fade-in and fade-out.

When it comes to js gradient display and disappearance, most of my friends will think of fadeIn (), fadeOut (), or fadeToggle () in JQuery (). But if we call a huge JQuery library just to introduce this effect? Or I can improve myself by using native js to implement some functions ~

Therefore, I simply studied the effect of pure js Code Writing fade-in and fade-out.

If an error occurs, please indicate in the comment that I can correct my own error.

 

(1) FadeIn fade-in Function

The effect of Fade-in and fade-out is actually a setInterval (). With the circular DOM operation, this effect can be achieved by changing the transparency of the element Object node.

So we can extract two necessary things: setInterval (), opacity, and speed.

  • Speed: This is the speed at which the opacity value changes from 0 to 1.
  • Quiet logic: the value of opacity changes from 0 to 1.

Let's take a look at the code implementation!

Html:

<div id="div1"></div><span id="span1">123</span><button>fadein</button><button>fadeOut</button>

Css style

<style> div {  width: 100px;  height: 100px;  background-color: #1d7db1;  opacity:0; } </style>

First, let's look at the first version of the fadeIn function: first, let's take a look at the Implementation ideas.

Function fadeIn (ele, speed) {let num = 0; let time = setInterval () => {num + = speed; ele. style. opacity = num/100; if (num> = 100) {clearInterval (time); // clear timer }}, 30 );}}

When this effect is implemented temporarily, some things are not that simple. If multiple triggers occur, setInterval may be used multiple times at the same time, causing some headaches.

To solve this problem, there is a solution: Add a global status to prevent multiple setInterval triggering.

let Fadeflag = true;function fadeIn(ele, speed) {  let num = 0;  if (Fadeflag) {  let time = setInterval(() => {    num += speed;    Fadeflag = false;    ele.style.opacity = num / 100;    if (num >= 100) {     clearInterval(time);     Fadeflag = true;    }  }, 30);  }}

Compatibility problems !!!

First look at the code

set: function(elem, num) {  elem.style.opacity !== undefined ? elem.style.opacity = num / 100 : elem.style.filter = 'alpha(opacity = '+ num +')';}

Note: The Code sets num/100 because we use ie as the standard to be compatible with ff and GG.

Function attribute for setting DOM node transparency in js: filter = "alpha (opacity =" + value + ")" (compatible with ie)

The filter range of ie is 0 ~ 100

Opacity = value/100 (compatible with FF and GG ).

The opacity of FF and GG is 0 ~ 1 (to be compatible with the filter range of ie, we use num/100)

(2) FadeOut fade-out function

Speed: This is the speed at which the opacity value changes from 1 to 0 (remember to consider compatibility)

Logic of fading out: the value of opacity changes from 1 to 0.

Encapsulation Function

(Function () {let fadeFlag = true; function Fade (selector) {this. elem = typeof selector = 'object '? Selector: document. getElementById (selector);} Fade. prototype = {constructor: Fade, setOpacity: (elem, opacity) =>{// compatible with ie10-elem. filters? Elem. style. filter = 'Alpha (opacity = '+ opacity +') ': elem. style. opacity = opacity/100; return true ;}, setOpacity: function (num) {this. elem. style. opacity! = Undefined? This. elem. style. opacity = num/100: this. elem. style. filter = 'Alpha (opacity = '+ num +') ';}, fadeIn: function (speed, opacity) {/* speed => fade-in speed, positive Integer (optional); opacity ==> fade to the specified transparency, 0 ~ 100 (optional); */speed = speed | 2; opacity = opacity | 100; let num = 0; // the value of transparency change during initialization is 0 if (fadeFlag) {let time = setInterval () => {num + = speed; fadeFlag = false; this. setOpacity (num); this. elem. style. opacity! = Undefined? This. elem. style. opacity = num/100: this. elem. style. filter = 'Alpha (opacity = '+ num +') '; if (num> = opacity) {clearInterval (time); fadeFlag = true ;}}, 20) ;}, fadeOut: function (speed, opacity) {/* speed ==> light-in speed, positive integer (optional); opacity ==> fade-in to the specified transparency, 0 ~ 100 (optional); */speed = speed | 2; opacity = opacity | 0; let num = 100; // The transparency change value is 0 if (fadeFlag) {let time = setInterval () => {num-= speed; fadeFlag = false; this. set (num); this. elem. style. opacity! = Undefined? This. elem. style. opacity = num/100: this. elem. style. filter = 'Alpha (opacity = '+ num +') '; if (num <= opacity) {clearInterval (time); fadeFlag = true ;}}, 20) ;}}; window. fade = Fade ;})();

Test instance:

let btn = document.getElementsByTagName('button')[0]; let btn2 = document.getElementsByTagName('button')[1]; btn.onclick = () => {  let fade = new Fade('div1'); fade.fadeIn(); }; btn2.onclick = () => {  let fade = new Fade('div1');  fade.fadeOut(); }

Summary

The above is a small part of the javascript native encapsulation of a function testing instance code for fade-in and fade-out effect. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.