js類比jquery的slide和fadeIn和fadeOut功能_javascript技巧

來源:互聯網
上載者:User
就想了想 jquery是怎麼實現的呢,無奈,哥能力差,還不到研究jquery源碼的時候。那好吧,自己做個很簡陋的版本,呵呵!好像還行,等哥的能力上去了再來最佳化下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>三欄布局</title> <style> #main1{ margin:20px auto; width:200px; background-color:#6c9; overflow:hidden; } </style> <script type="text/javascript"> var Animate = { obj : null, itime : 0, flag : 1, maxHeight : 0, _$ : function(id){ return "string" == typeof(id) ? document.getElementById(id) : id; }, slideDown : function(){ if(parseInt(this.obj.style.height) > this.maxHeight){ this.flag = 0; return }; this.obj.style.height = parseInt(this.obj.style.height) + 1 + 'px'; setTimeout("Animate.slideDown()",this.itime); }, slideUp: function(){ if(parseInt(this.obj.style.height) < 1){ this.flag = 1; return }; this.obj.style.height = parseInt(this.obj.style.height) - 1 + 'px'; var repeat = "Animate.slideUp()"; setTimeout(repeat,this.itime); }, slide : function(objid,itime){ this.obj = this._$(objid); this.itime = itime; if(this.flag){ this.maxHeight = this.maxHeight ? this.maxHeight :parseInt(this.obj.style.height); this.obj.style.display = "block"; this.obj.style.height = "0px"; this.slideDown(); } else { this.slideUp(); } }, fadeIn : function(objid,itime){ this.obj = this._$(objid); this.itime = itime; var that = this; this.maxHeight =parseInt( this.obj.style.height); var i = 100; var timer = setInterval(function(){ that.obj.style.cssText ="filter :alpha(opacity=" + i + ");-moz-opacity:"+i*0.01+";opacity: "+i*0.01+";height:"+that.maxHeight+"px;"; i--; if(i == 0) clearInterval(timer); },that.itime); }, fadeOut : function(objid,itime){ this.obj = this._$(objid); this.itime = itime; var that = this; this.maxHeight =parseInt( this.obj.style.height); var i = 0; var timer = setInterval(function(){ that.obj.style.cssText ="filter :alpha(opacity=" + i + ");-moz-opacity:"+i*0.01+";opacity: "+i*0.01+";height:"+that.maxHeight+"px;"; i++; if(i ==100) clearInterval(timer); },that.itime); } } </script> </head> <body> <input type="button" value="slide" onclick="Animate.slide('main1',10)" /> <input type="button" value="fadeIn" onclick="Animate.fadeIn('main1',10)" /> <input type="button" value="fadeOut" onclick="Animate.fadeOut('main1',10)" /> <div id="main1" > slide && fadeIn && fadeIn && fadeOut </div> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

運行後,大家看效果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.