




免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实现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 = +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 = arguments1 | , 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.1. 2.但上面并不尽善尽美,有一个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 = arguments1 | , 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 = arguments1 | , 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.Ototype = 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.st
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国SD卡用PCB行业市场发展前景及发展趋势与投资战略研究报告(2024-2030)-20241228-144211
- 高中地理教学中实验教学的创新设计与学生实践能力培养研究
- 中国用电标示牌行业市场前景预测及投资价值评估分析报告
- 中国声敏传感器市场供需现状及投资战略研究报告
- 2025-2030年中国钢锭项目投资可行性研究分析报告
- 电气件行业深度研究分析报告(2024-2030版)
- 农用塑料制品生项目投资可行性研究分析报告(2024-2030版)
- 周大福培训课件
- 2025年中国制造执行系统(MES)行业市场深度调查评估及投资方向研究报告
- 中国供应链金融科技解决方案行业发展前景预测及投资规划建议报告
- 069.糖尿病酮症酸中毒诊断和处理
- 杆杠各类题型复习-2024-2025学年浙教版九年级上册科学
- 可持续棕榈油圆桌倡议组织RSPO供应链认证管理手册及程序文件
- DL∕T 516-2017 电力调度自动化运行管理规程
- 江苏省盐城市东台市2023-2024学年八年级下学期期末英语试卷(含答案解析)
- 服装工艺师岗位职责
- 深圳市体育场馆租赁合同
- 轴承座基本工艺专业课程设计
- MOOC 计算机系统局限性-华东师范大学 中国大学慕课答案
- MOOC 管理学原理-东北财经大学 中国大学慕课答案
- 《校园安全用电知识讲座》课件模板(三套)
评论
0/150
提交评论