成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

淺析Javascript透明特效的可控式實現

開發 前端
這里將介紹Javascript透明特效的可控式實現,也就是特效可以被控制,希望本文能對大家有所幫助。

可控式Javascript透明特效也就是透明度可以自行設置,但是這種方法在IE7下極有可能失效,不過這些東西對大家了解Javascript透明特效還是有所幫助的。

Javascript透明特效是script.aculo.us提到的特效中最簡單的特效之一。既然是特效,必須涉及時間與空間的概念。時間我們可以用setTimeout與setInterval,個人比較喜歡setTimeout,雖然它每次調用都重復注冊,但可控性比較好。空間就全憑CSS的絕對定位實現位移了。在開始之前,我們練習一下setTimeout的遞歸用法(用來模擬setInterval)。

01.function text(el){
02.  var node  = (typeof el == "string")? document.getElementById(el) : el;
03.  var i = 0;
04.  var repeat = function(){
05.    setTimeout(function(){
06.      node.innerHTML = "<h1>"+i+"</h1>";
07.      i++;
08.      if(i <= 100){
09.        setTimeout(arguments.callee, 100);
10.      }
11.    },100)
12.  }
13.  repeat();
14.}

我們來試一下最簡單的淡入特效,就是把node.innerHTML那一行改成透明度的設置。

01.function fadeIn(el){
02.  var node  = (typeof el == "string")? document.getElementById(el) : el;
03.  var i = 0;
04.  var fade = function(){
05.    setTimeout(function(){     
06.        !+"\v1"? (node.style.filter="alpha(opacity="+i+")"): (node.style.opacity = i / 100);
07.      i++;
08.      if(i <= 100){
09.        setTimeout(arguments.callee, 100);
10.      }
11.    },100)
12.  }
13.  fade();
14.}

但是這樣并不完美,因為IE的濾鏡可能會在IE7中失效,我們必須要用zoom=1來激活hasLayout。我們再添加一些可制定參數擴充它。注釋已經非常詳細,不明白在留言里再問我吧。

01.function opacity(el){
02.  //必選參數
03.  var node  = (typeof el == "string")? document.getElementById(el) : el,
04.  //可選參數
05.  options = arguments[1] || {},
06.  //變化的持續時間
07.  duration = options.duration || 1.0,
08.  //開始時透明度
09.  from = options.from || 0.0 ,
10.  //結束時透明度
11.  to = options.to || 0.5,
12.  operation = 1,
13.  init = 0;
14.  if(to - from < 0){
15.    operation = -1,
16.    init = 1;
17.  }
18.  //內部參數
19.  //setTimeout執行的間隔時間,單位毫秒
20.  var frequency = 100,
21.  //設算重復調用的次數
22.  count = duration * 1000 / frequency,
23.  // 設算每次透明度的遞增量
24.  detal = Math.abs(to - from)  /count,
25.  // 正在進行的次數
26.  i = 0;
27.  var main = function(){
28.    setTimeout(function(){
29.      if(!+"\v1"){
30.        if(node.currentStyle.hasLayout)  node.style.zoom = 1;//防止濾鏡失效
31.        node.style.filter="alpha(opacity="+ (init * 100 + operation * detal * i * 100).toFixed(1) +")"
32.      }else{
33.        node.style.opacity =  (init + operation * detal * i).toFixed(3)
34.      }
35.      node.innerHTML =  (init + operation * detal * i).toFixed(3)
36.      i++;
37.      if(i <= count){
38.        setTimeout(arguments.callee, frequency);
39.      }
40.    },frequency)
41.  }
42.  main();
43.}
100.0
50.0
1.<div class="text" onclick="opacity(this,{duration:4.0,from:0.0,to:1})"></div>
2.<div class="text" onclick="opacity(this,{duration:4.0,from:1.0,to:0})"></div>

但上面并不盡善盡美,有一個Bug。我們是通過短路運算符來決定是否使用默認參數還是我們傳入的參數,但在Javascript中,數字0甚至0.0都會自動轉換為false。因此在第個例子,如果我們在to中傳入0,它永遠不會用到這個0,而是默認的0.5。解決方法讓它變成字符串“0”。另,參數i也不是必須的,我們可以省去它,用count負責所有的循環,但這樣一來,我們的思維就要逆過來想了。原來是加的,我們要變成減的。

01.function opacity(el){
02.  //必選參數
03.  var node  = (typeof el == "string")? document.getElementById(el) : el,
04.  //可選參數
05.  options = arguments[1] || {},
06.  //變化的持續時間
07.  duration = options.duration || 1.0,
08.  //開始時透明度
09.  from = options.from || 0.0 ,
10.  //結束時透明度
11.  to = (options.to && options.to + "") || 0.5,
12.  operation = -1,
13.  init = 1;
14.  if(to - from < 0){
15.    operation = 1,
16.    init = 0;
17.  }
18.  //內部參數
19.  //setTimeout執行的時間,單位
20.  var frequency = 100,
21.  //設算重復調用的次數
22.  count = duration * 1000 / frequency,
23.  // 設算每次透明度的遞增量
24.  detal = operation * Math.abs(to - from) /count;
25.  var main = function(){
26.    setTimeout(function(){
27.      if(!+"\v1"){
28.        if(node.currentStyle.hasLayout)  node.style.zoom = 1;//防止濾鏡失效
29.        node.style.filter="alpha(opacity="+ (init * 100 +  detal * count * 100).toFixed(1) +")"
30.      }else{
31.        node.style.opacity =  (init +  detal * count).toFixed(3)
32.      }
33.      count--;
34.      if(count + 1){
35.        setTimeout(arguments.callee, frequency);
36.      }
37.    },frequency)
38.  }
39.  main();
40.}

進一步優化,利用原型共享方法。

01.function Opacity(el){
02.  var node  = (typeof el == "string")? document.getElementById(el) : el,
03.  options = arguments[1] || {},
04.  duration = options.duration || 1.0,
05.  from = options.from || 0.0 ,
06.  to = (options.to && options.to + "") || 0.5,
07.  operation = -1,
08.  init = 1;
09.  if(to - from < 0){
10.    operation = 1,
11.    init = 0;
12.  }
13.  var frequency = 100,
14.  count = duration * 1000 / frequency,
15.  detal = operation * Math.abs(to - from) /count;
16.  this.main(node,init,detal,count,frequency);
17.}
18.Opacity.prototype = {
19.  main : function(node,init,detal,count,frequency){
20.    setTimeout(function(){
21.      if(!+"\v1"){
22.        if(node.currentStyle.hasLayout)  node.style.zoom = 1;//防止濾鏡失效
23.        node.style.filter="alpha(opacity="+ (init * 100 +  detal * count * 100).toFixed(1) +")"
24.      }else{
25.        node.style.opacity =  (init +  detal * count).toFixed(3)
26.      }
27.      node.innerHTML =  (init +  detal * count).toFixed(3)
28.      count--;
29.      if(count + 1){
30.        setTimeout(arguments.callee, frequency);
31.      }
32.    },frequency)
33.  }
34.}
1.000
0.000
1.<div class="text" onclick="new Opacity(this,{duration:4.0,from:0.0,to:1})"></div>
2.<div class="text" onclick="new Opacity(this,{duration:4.0,from:1.0,to:0})"></div>

原文標題:javascript的可控式透明特效

鏈接:http://www.cnblogs.com/rubylouvre/archive/2009/09/14/1566532.html

【編輯推薦】

  1. JSON是什么?為JavaScript準備的數據格式
  2. 十個最常用的JavaScript自定義函數
  3. 有關JavaScript事件加載的一些延伸思考
  4. JavaScript使用心得匯總:從BOM和DOM談起
  5. ExtJS在Android模擬器上的運行效果
責任編輯:彭凡 來源: 博客園
相關推薦

2016-10-19 14:35:20

JavaScript函數式編程

2009-08-20 10:10:55

C#透明窗體

2009-10-12 10:33:11

Javascript替

2009-09-11 10:44:07

JavaScript實

2021-07-27 22:56:00

JavaScript編程開發

2009-08-27 14:29:28

顯式實現接口

2020-03-31 08:05:23

分布式開發技術

2011-08-29 16:16:22

Lua函數多線程

2009-09-07 06:56:46

C#透明窗體

2016-09-06 21:37:41

2011-03-07 09:41:10

JavaScript

2021-02-07 22:59:55

JavaScript編程方法鏈

2009-07-24 17:30:37

Javascript閉

2010-09-28 14:12:50

Javascript

2011-03-10 14:19:56

JavaScript

2013-03-22 09:51:36

IP網關網絡遷移VoIP

2011-03-08 09:15:04

JavaScript

2022-01-17 21:37:24

JavaScriptHTMLCSS

2016-09-14 21:28:25

JavaScript事件代理委托

2009-07-14 11:34:42

MyEclipse斷點JavaScript
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美日韩亚洲国产 | 久久久久久久国产精品 | 91精品国产综合久久久动漫日韩 | av一区在线观看 | 超碰在线播 | 亚洲精品免费观看 | 91精品国产综合久久久久久漫画 | 免费观看一级视频 | 亚洲一二三在线观看 | 亚洲日韩中文字幕一区 | 中文字幕的av | 亚洲午夜小视频 | 怡红院怡春院一级毛片 | 国产在线1 | 亚洲精品视频在线看 | 国产亚洲精品久久久优势 | 日韩日b视频 | 亚洲一区二区三区视频 | 久久久片 | 狠狠综合久久av一区二区小说 | www.一区二区三区.com | 成人精品久久久 | 91亚洲国产成人久久精品网站 | 综合久久99 | 在线一区 | 久久久精品日本 | 天天综合网天天综合色 | 欧美精品久久久 | 亚洲精品久久久久久久久久吃药 | 国产成人精品免高潮在线观看 | 99热国产在线播放 | 色天堂影院| 99爱视频| 福利色导航 | 国产一区精品 | 久久久久久国产一区二区三区 | 久久久久久久久国产精品 | 国产精品日韩在线观看 | 国产免费一区二区三区最新6 | 国产在线一级片 | 颜色网站在线观看 |