四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

javascript 可控式透明特效實(shí)現(xiàn)代碼

空間就全憑CSS的絕對定位實(shí)現(xiàn)位移了。在開始之前,我們練習(xí)一下setTimeout的遞歸用法(用來模擬setInterval)。
復(fù)制代碼 代碼如下:
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();
}

我們來試一下最簡單的淡入特效,就是把node.innerHTML那一行改成透明度的設(shè)置。
復(fù)制代碼 代碼如下:
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。我們再添加一些可制定參數(shù)擴(kuò)充它。注釋已經(jīng)非常詳細(xì),不明白在留言里再問我吧。
復(fù)制代碼 代碼如下:
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è)例子,如果我們在to中傳入0,它永遠(yuǎn)不會(huì)用到這個(gè)0,而是默認(rèn)的0.5。解決方法讓它變成字符串“0”。另,參數(shù)i也不是必須的,我們可以省去它,用count負(fù)責(zé)所有的循環(huán),但這樣一來,我們的思維就要逆過來想了。原來是加的,我們要變成減的。
復(fù)制代碼 代碼如下:
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)化,利用原型共享方法。
復(fù)制代碼 代碼如下:
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ù)javascript 可控式透明特效實(shí)現(xiàn)代碼,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 妻子的秘密免费看全集| 大奉打更人电视剧免费在线观看| 斯维特拜克之歌| 译码器及其应用实验报告| 大空头 电影| 台州林毅| 爱情赏味期| 山东卫视体育频道| 风之谷钢琴谱| junk boy| 野性狂欢大派对| 电视剧昨夜星辰| 黄金太阳2| tina kay| 邵雨薇为艺术做出贡献的电影有哪些| 宇宙刑事卡邦| 十一码复式22块钱中奖对照表| 免费看黄在线看| 康熙微服记四部免费观看在线| 黑帮团伙美国电影| 白雪公主和七个小矮人的原文| 欧美日本视频在线| 玛雅历险记| 市川美织| 355 电影| 穿上触手内衣被调教堕落| 温柔地杀死我| 4人免费剧本及答案| 楼南光电影| 日本电影继父| 真的爱你中文谐音歌词| 台湾李丽萍十部必看电影| 首映式| 林正英僵尸大全免费看| 一个馒头引起的血案| 被侵犯| 新爱情乐园| 电影画皮3免费| 男人上路| 相见故明月| 大学生职业规划ppt成品 |