js实现类似jquery里animate动画效果的方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了js实现类似jquery里animate动画效果的方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2000字,纯文字阅读大概需要3分钟。
内容图文
![js实现类似jquery里animate动画效果的方法](/upload/InfoBanner/zyjiaocheng/359/b33d798e6dac486286dd5e57492b5e13.jpg)
本文实例讲述了js实现类似jquery里animate动画效果的方法。分享给大家供大家参考。具体分析如下:
该实例可实现鼠标移上,先宽度变化,再高度变化,最后透明度变化,鼠标移出,再依次变回去的效果。
要点一:
startrun(obj,attr,target,fn) box.onmouseover = function(){ startrun(box,"width",200,function(){ startrun(box,"height",200,function(){ startrun(box,"opacity","100") }); }); }
如上面,函数也可以做为参数使用,就可以达到先执行某个动作,再执行某个动作的效果了。
要点二:
if(cur == target){ clearInterval(obj.timer); if(fn){ fn(); } }
当运动到达目标点,关闭定时器,然后就可以执行新的函数了。
最后,上代码:
<!DOCTYPE html> <html> <head> <meta charset="gb2312" /> <title>无标题文档</title> <style> <!-- body{margin:0; padding:0; font:12px/1.5 arial;} #box{width:100px; height:100px; position:absolute; background:#06c; left:0;filter:alpha(opacity=30); opacity:0.3;} --> </style> <script> <!-- function getstyle(obj,name){ if(obj.currentStyle){ return obj.currentStyle[name]; }else{ return getComputedStyle(obj,false)[name]; } } window.onload = function(){ var box = document.getElementById("box"); box.onmouseover = function(){ startrun(box,"width",200,function(){ startrun(box,"height",200,function(){ startrun(box,"opacity","100") }); }); } box.onmouseout = function(){ startrun(box,"height",100,function(){ startrun(box,"width",100,function(){ startrun(box,"opacity","30"); }); }); } } function startrun(obj,attr,target,fn){ clearInterval(obj.timer); obj.timer = setInterval(function(){ var cur = 0; if(attr == "opacity"){ cur = Math.round(parseFloat(getstyle(obj,attr))*100); }else{ cur = parseInt(getstyle(obj,attr)); } var speed = (target-cur)/8; speed = speed>0?Math.ceil(speed):Math.floor(speed); if(cur == target){ clearInterval(obj.timer); if(fn){ fn(); } }else{ if(attr == "opacity"){ obj.style.filter = "alpha(opacity="+(cur+speed)+")"; obj.style.opacity = (cur+speed)/100; }else{ obj.style[attr] = cur + speed + "px"; } } },30) } //--> </script> </head> <body> <div id="box"> </div> </body> </html>
希望本文所述对大家的javascript程序设计有所帮助。
内容总结
以上是互联网集市为您收集整理的js实现类似jquery里animate动画效果的方法全部内容,希望文章能够帮你解决js实现类似jquery里animate动画效果的方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。