JS動畫效果開啟、關閉層的實現方法,js動畫

來源:互聯網
上載者:User

JS動畫效果開啟、關閉層的實現方法,js動畫

本文執行個體講述了JS動畫效果開啟、關閉層的實現方法。分享給大家供大家參考。具體如下:

<!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=utf-8" /><title>層展開、關閉</title><style type="text/css">#main{width:500px; margin:100px;height:500px;border:1px solid red}#test{border:1px solid red;display:none;width:10px;height:10px; background:yellow}</style></head><body><input type="button" value="開啟" id="bt" /><input type="button" value="關閉" id="bt1" /><div id="main"><div id="test"></div></div></body></html><script type="text/javascript">function $ (o) {return document.getElementById(o);}function XslideDown(obj,type,mX,num) {if(!type){return;}try{var type1=type=="height"?"marginTop":"marginLeft";var type2=type=="height"?"top":"left";XSetCss(obj,[type1,XgetOffset(obj)[type2]+"px"]);XSetCss(obj,[type,XgetOffset(obj)[type]+num+"px"]);if(XgetOffset(obj)[type]<mX){setTimeout(function(){XslideDown(obj,type,mX,num);},10); }else{XSetCss(obj,[type,mX])}}catch(e){}}function XslideUp(obj,type,mX,num) {if(!type){return;}try{var type1=type=="height"?"marginTop":"marginLeft";var type2=type=="height"?"top":"left";XSetCss(obj,[type1,XgetOffset(obj)[type2]+"px"]);XSetCss(obj,[type,XgetOffset(obj)[type]-num+"px"]);if(XgetOffset(obj)[type]>mX){ setTimeout(function(){XslideUp(obj,type,mX,num);},1); }else{XSetCss(obj,[type,mX])obj.style.display="none";}}catch(e){}}function XSetCss(obj,cssArgs){if(arguments.length==2){ if(cssArgs.constructor==Object){for(var o in cssArgs){if(obj.style[o]!="undefiend"){obj.style[o]=cssArgs[o]; } } }if(cssArgs.constructor==Array&&cssArgs.length==2){obj.style[cssArgs[0]]=cssArgs[1]; }}}function XgetOffset (obj) {return {height:obj.offsetHeight,width:obj.offsetWidth,top:parseInt((obj.parentNode.offsetHeight-obj.offsetHeight)/2),left:parseInt((obj.parentNode.offsetWidth-obj.offsetWidth)/2)}}function XopenDiv(o){o.style.display="block";XslideDown(o,"width",400,10);XslideDown(o,"height",400,10);}function XcloseDiv(o){XslideUp(o,"width",10,10);XslideUp(o,"height",10,10);}$("bt").onclick=function(){XopenDiv($("test"))}$("bt1").onclick=function(){XcloseDiv($("test"))}</script>

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.