浅析Javascript透明特效的可控式实现
创始人
2024-06-07 00:41:34
0

可控式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 = "

"+i+"

";
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模拟器上的运行效果

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...