說到js的漸變顯示與消失,多數朋友會想到JQuery裏面的fadeIn()、fadeOut()或fadeToggle()。但若是僅僅是爲了引入這樣的一個效果,而去調用了龐大JQuery庫?或者說我經過用原生js實現一些函數來提升本身~css
因此,我簡單的研究了一下純js代碼寫淡入淡出的效果。html
若是出現錯誤,請在評論中指出,我也好本身糾正本身的錯誤bash
author: thomaszhou函數
(一)FadeIn淡入函數
淡入淡出的效果,其實就是一個setInterval(),加上循環的DOM操做,經過改變element對象節點的透明度,便可實現這種效果。測試
因此咱們提取出兩個必要的東西:setInterval()、opacity、以及speed。ui
html:this
<div id="div1"></div>
<span id="span1">123</span>
<button>fadein</button>
<button>fadeOut</button>
複製代碼
css樣式spa
<style>
div {
width: 100px;
height: 100px;
background-color: #1d7db1;
opacity:0;
}
</style>
複製代碼
首先咱們來看fadeIn函數的初版的寫法:先了解一下實現的思路prototype
function fadeIn(ele, speed) {
let num = 0;
let time = setInterval(() => {
num += speed;
ele.style.opacity = num / 100;
if (num >= 100) {
clearInterval(time); // 清除定時器
}
}, 30);
}
}
複製代碼
當這個效果暫時實現之後,有些東西並無那麼簡單。若是發生屢次的觸發效果,會出現setInterval被屢次同時使用,出現一些讓人頭疼的BUG。code
要解決這個問題,目前有一個方案:添加一個全局狀態,防止屢次觸發setInterval。
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);
}
}
複製代碼
先看代碼
set: function(elem, num) {
elem.style.opacity !== undefined ? elem.style.opacity = num / 100 : elem.style.filter = 'alpha(opacity = '+ num +')';
}
複製代碼
注:代碼設置num/100是由於咱們以ie爲標準去兼容ff和GG
js裏面設置DOM節點透明度的函數屬性:filter= "alpha(opacity=" + value+ ")"(兼容ie)
ie的filter範圍是0~100
opacity=value/100(兼容FF和GG)。
FF和GG的opacity是0~1 (爲了兼容ie的filter範圍,咱們用num/100)
(二)FadeOut淡出函數
(function() {
let fadeFlag = true;
function Fade(selector) {
this.elem = typeof selector == 'Object' ? selector : document.getElementById(selector);
}
Fade.prototype = {
constructor: Fade,
setOpacity: (elem, opacity) => { // 兼容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 ==>淡入的速度,正整數(可選);
opacity ==>淡入到指定的透明度,0~100(可選);
*/
speed = speed || 2;
opacity = opacity || 100;
let num = 0; // 初始化透明度變化值爲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 ==>淡入的速度,正整數(可選);
opacity ==>淡入到指定的透明度,0~100(可選);
*/
speed = speed || 2;
opacity = opacity || 0;
let num = 100; // 初始化透明度變化值爲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;
})();
複製代碼
測試實例:
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();
}
複製代碼