本文實例講述了JavaScript實現的伸展收縮型菜單代碼。分享給大家供大家參考。具體如下:
運行效果截圖如下:
具體代碼如下:
<html><head><title>真正的JavaScript伸展收縮型菜單</title><style type="text/css">#con div{width:100px;margin:5px 0 5px 0;font-size:9pt;height:23px;color:white;}</style><div id="con"><div style="background-color:red">紅色菜單</div><div style="background-color:green">綠色菜單 </div><div style="background-color:blue">藍色</div><div style="background-color:yellow">黃色</div><div style="background-color:pink">這是什么色</div><div style="background-color:orange">桔色</div><div style="background-color:black">黑色超酷</div></div><script language="javascript">function $(e){return document.getElementById(e);}function roulMenu(e,maxW,minW){ var divs = $(e).getElementsByTagName('div'); for(var i=0;i<divs.length;i++){ (function(){ var tims,timss; divs[i].onmouseover=function(){ var self = this; clearInterval(timss); tims=setInterval(function(){ if(self.offsetWidth<maxW){ self.style.width = self.offsetWidth + 5 + 'px'; }else{ clearInterval(tims); } },10); } divs[i].onmouseout=function(){ var self = this; clearInterval(tims); timss=setInterval(function(){ if(self.offsetWidth>minW){ self.style.width = self.offsetWidth - 5 + 'px'; }else{ clearInterval(timss); } },10); } })(); }}//使用方法roulMenu('con',200,100);</script>
希望本文所述對大家的JavaScript程序設計有所幫助。
新聞熱點
疑難解答