代码如下: 层的扩展与收缩 function StretchDiv(divid,color){ var target; var flag=false; var onMouseDown=function(){ if(flag){ var h=parseInt(target.style.height); onMouseDown.shortDiv=function(){ h=h-1; if(h<20){ return; } target.style.height=h+"px"; setTimeout(onMouseDown.shortDiv,30); } onMouseDown.shortDiv(); } else { //target.style.height=parseInt(target.style.height)+180+"px"; var h=parseInt(target.style.height); onMouseDown.longDiv=function(){ h=h+1; if(h>200){ return; } target.style.height=h+"px"; setTimeout(onMouseDown.longDiv,30); } onMouseDown.longDiv(); } flag=!flag; } function init(){ target=document.getElementById(divid); target.style.position = "absolute"; target.style.width = "200px"; target.style.height = "20px"; target.style.left = "100px"; target.style.top = "100px"; target.style.backgroundColor = color; target.onmousedown = onMouseDown; } init(); } new StretchDiv("div1","blue"); [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]