javascript 可控式透明特效實(shí)現(xiàn)代碼
function text(el){
var node = (typeof el == "string")? document.getElementById(el) : el;
var i = 0;
var repeat = function(){
setTimeout(function(){
node.innerHTML = "<h1>"+i+"</h1>";
i++;
if(i <= 100){
setTimeout(arguments.callee, 100);
}
},100)
}
repeat();
}
我們來試一下最簡(jiǎn)單的淡入特效,就是把node.innerHTML那一行改成透明度的設(shè)置。
function fadeIn(el){
var node = (typeof el == "string")? document.getElementById(el) : el;
var i = 0;
var fade = function(){
setTimeout(function(){
!+"\v1"? (node.style.filter="alpha(opacity="+i+")"): (node.style.opacity = i / 100);
i++;
if(i <= 100){
setTimeout(arguments.callee, 100);
}
},100)
}
fade();
}
但是這樣并不完美,因?yàn)镮E的濾鏡可能會(huì)在IE7中失效,我們必須要用zoom=1來激活hasLayout。我們?cè)偬砑右恍┛芍贫▍?shù)擴(kuò)充它。注釋已經(jīng)非常詳細(xì),不明白在留言里再問我吧。
function opacity(el){
//必選參數(shù)
var node = (typeof el == "string")? document.getElementById(el) : el,
//可選參數(shù)
options = arguments[1] || {},
//變化的持續(xù)時(shí)間
duration = options.duration || 1.0,
//開始時(shí)透明度
from = options.from || 0.0 ,
//結(jié)束時(shí)透明度
to = options.to || 0.5,
operation = 1,
init = 0;
if(to - from < 0){
operation = -1,
init = 1;
}
//內(nèi)部參數(shù)
//setTimeout執(zhí)行的間隔時(shí)間,單位毫秒
var frequency = 100,
//設(shè)算重復(fù)調(diào)用的次數(shù)
count = duration * 1000 / frequency,
// 設(shè)算每次透明度的遞增量
detal = Math.abs(to - from) /count,
// 正在進(jìn)行的次數(shù)
i = 0;
var main = function(){
setTimeout(function(){
if(!+"\v1"){
if(node.currentStyle.hasLayout) node.style.zoom = 1;//防止濾鏡失效
node.style.filter="alpha(opacity="+ (init * 100 + operation * detal * i * 100).toFixed(1) +")"
}else{
node.style.opacity = (init + operation * detal * i).toFixed(3)
}
node.innerHTML = (init + operation * detal * i).toFixed(3)
i++;
if(i <= count){
setTimeout(arguments.callee, frequency);
}
},frequency)
}
main();
}
效果演示:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
<div class="text" onclick="opacity(this,{duration:4.0,from:0.0,to:1})"></div>
<div class="text" onclick="opacity(this,{duration:4.0,from:1.0,to:0})"></div>
但上面并不盡善盡美,有一個(gè)Bug。我們是通過短路運(yùn)算符來決定是否使用默認(rèn)參數(shù)還是我們傳入的參數(shù),但在javascript中,數(shù)字0甚至0.0都會(huì)自動(dòng)轉(zhuǎn)換為false。因此在第個(gè)例子,如果我們?cè)趖o中傳入0,它永遠(yuǎn)不會(huì)用到這個(gè)0,而是默認(rèn)的0.5。解決方法讓它變成字符串“0”。另,參數(shù)i也不是必須的,我們可以省去它,用count負(fù)責(zé)所有的循環(huán),但這樣一來,我們的思維就要逆過來想了。原來是加的,我們要變成減的。
function opacity(el){
//必選參數(shù)
var node = (typeof el == "string")? document.getElementById(el) : el,
//可選參數(shù)
options = arguments[1] || {},
//變化的持續(xù)時(shí)間
duration = options.duration || 1.0,
//開始時(shí)透明度
from = options.from || 0.0 ,
//結(jié)束時(shí)透明度
to = (options.to && options.to + "") || 0.5,
operation = -1,
init = 1;
if(to - from < 0){
operation = 1,
init = 0;
}
//內(nèi)部參數(shù)
//setTimeout執(zhí)行的時(shí)間,單位
var frequency = 100,
//設(shè)算重復(fù)調(diào)用的次數(shù)
count = duration * 1000 / frequency,
// 設(shè)算每次透明度的遞增量
detal = operation * Math.abs(to - from) /count;
var main = function(){
setTimeout(function(){
if(!+"\v1"){
if(node.currentStyle.hasLayout) node.style.zoom = 1;//防止濾鏡失效
node.style.filter="alpha(opacity="+ (init * 100 + detal * count * 100).toFixed(1) +")"
}else{
node.style.opacity = (init + detal * count).toFixed(3)
}
count--;
if(count + 1){
setTimeout(arguments.callee, frequency);
}
},frequency)
}
main();
}
進(jìn)一步優(yōu)化,利用原型共享方法。
function Opacity(el){
var node = (typeof el == "string")? document.getElementById(el) : el,
options = arguments[1] || {},
duration = options.duration || 1.0,
from = options.from || 0.0 ,
to = (options.to && options.to + "") || 0.5,
operation = -1,
init = 1;
if(to - from < 0){
operation = 1,
init = 0;
}
var frequency = 100,
count = duration * 1000 / frequency,
detal = operation * Math.abs(to - from) /count;
this.main(node,init,detal,count,frequency);
}
Opacity.prototype = {
main : function(node,init,detal,count,frequency){
setTimeout(function(){
if(!+"\v1"){
if(node.currentStyle.hasLayout) node.style.zoom = 1;//防止濾鏡失效
node.style.filter="alpha(opacity="+ (init * 100 + detal * count * 100).toFixed(1) +")"
}else{
node.style.opacity = (init + detal * count).toFixed(3)
}
node.innerHTML = (init + detal * count).toFixed(3)
count--;
if(count + 1){
setTimeout(arguments.callee, frequency);
}
},frequency)
}
}
演示代碼:
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
<div class="text" onclick="new Opacity(this,{duration:4.0,from:0.0,to:1})"></div>
<div class="text" onclick="new Opacity(this,{duration:4.0,from:1.0,to:0})"></div>
- javascript實(shí)現(xiàn)帶節(jié)日和農(nóng)歷的日歷特效
- javascript實(shí)例分享---具有立體效果的圖片特效
- 一個(gè)JavaScript的求愛小特效
- 純JavaScript實(shí)現(xiàn)HTML5 Canvas六種特效濾鏡示例
- 23個(gè)Javascript彈出窗口特效整理
- javascript 打字效果的文字特效
- JavaScript妙味課堂 物體平滑移動(dòng)特效
- javascript:google 向上向下滾動(dòng)特效,兼容IE6,7,8,FF
- JavaScript 地震特效
- 同一個(gè)網(wǎng)頁中實(shí)現(xiàn)多個(gè)JavaScript特效的方法
相關(guān)文章
localStorage過期時(shí)間設(shè)置的幾種方法
聊到localStorage想必熟悉前端的朋友都不會(huì)陌生,在實(shí)際的應(yīng)用場(chǎng)景中,我們往往需要讓localStorage設(shè)置的某個(gè)key能在指定時(shí)間內(nèi)自動(dòng)失效,所以基于這種場(chǎng)景,我們?nèi)绾稳ソ鉀Q呢,本文就詳細(xì)的介紹一下2021-12-12IE瀏覽器IFrame對(duì)象內(nèi)存不釋放問題解決方法
IFrame對(duì)象占用的內(nèi)存資源在窗體關(guān)閉后不會(huì)釋放。彈出關(guān)閉反復(fù)多次后,IE瀏覽器內(nèi)存占用可超過數(shù)百M(fèi),嚴(yán)重時(shí)IE瀏覽器報(bào)錯(cuò)2014-08-08JavaScript實(shí)現(xiàn)的XML與JSON互轉(zhuǎn)功能詳解
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的XML與JSON互轉(zhuǎn)功能,結(jié)合實(shí)例形式分析了基于javascript的xml與json相關(guān)轉(zhuǎn)換功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02js 在定義的時(shí)候立即執(zhí)行的函數(shù)表達(dá)式(function)寫法
如果不需要顯示調(diào)用函數(shù), 讓這個(gè)函數(shù)在定義的時(shí)候就執(zhí)行的話, 該如何寫才可以呢,接下來將詳細(xì)介紹實(shí)現(xiàn)步驟,感興趣的朋友可以了解下2013-01-01JavaScript實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)菜單效果
這篇文章主要為大家詳細(xì)介紹了三級(jí)聯(lián)動(dòng)菜單效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08訪問百度和谷歌網(wǎng)速測(cè)試的javascript代碼
訪問百度和谷歌網(wǎng)速測(cè)試的javascript代碼...2007-08-08基于JavaScript代碼實(shí)現(xiàn)自動(dòng)生成表格
本文給大家分享一段js代碼實(shí)現(xiàn)輸入表格行數(shù)、列數(shù)自動(dòng)生成表格源代碼,非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06JS獲得選取checkbox整行數(shù)據(jù)的方法
這篇文章主要介紹了JS獲得選取checkbox整行數(shù)據(jù)的方法,涉及使用js對(duì)DOM節(jié)點(diǎn)的操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-01-01