JS实现可缩放、拖动、关闭和最小化的浮动窗口完整实例
本文实例讲述了JS实现可缩放、拖动、关闭和最小化的浮动窗口方法。分享给大家供大家参考。具体实现方法如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>JS实现可缩放、拖动、关闭和最小化的浮动窗口</title>
</head>
<styletype="text/css">
.divWindow{word-wrap:break-word;position:absolute;overflow:hidden;}
.divBar{border:#0000001pxsolid;position:absolute;border-bottom:#0000001pxsolid;width:100%;height:20px;background-color:#0099FF;cursor:hand;line-height:20px;}
.divChange{position:absolute;right:25px;font-size:10pt;}
.divClose{position:absolute;right:5px;font-size:11pt;}
.divTitle{position:absolute;left:5px;font-size:10pt;white-space:nowrap;text-overflow:ellipsis;-o-text-overflow:ellipsis;overflow:hidden;}
.divContent{border:#0000001pxsolid;border-top:#0000000pxsolid;position:absolute;top:20px;width:100%;background-color:#FFFFFF;overflow-y:auto;
SCROLLBAR-BASE-COLOR:#FFFFFF;SCROLLBAR-ARROW-COLOR:#999999;SCROLLBAR-FACE-COLOR:#EEEEEE;SCROLLBAR-HIGHLIGHT-COLOR:#EEEEEE;
SCROLLBAR-SHADOW-COLOR:#EEEEEE;SCROLLBAR-3DLIGHT-COLOR:#FFFFFF;SCROLLBAR-TRACK-COLOR:#FFFFFF;SCROLLBAR-DARKSHADOW-COLOR:#CCCCCC;}
.divReSize{height:7px;width:7px;overflow:hidden;background-color:#0000FF;position:absolute;bottom:6px;right:6px;cursor:nw-resize}
.divIframe{height:100%;width:100%;}
</style>
<scriptlanguage="javascript">
varzindex=0 //全局变量
functiondragClass(name,title,content,left,top,width,height){
varisMouseDown=false;
varmaximum=false;
varoffX=0; //设置抓取点X坐标
varoffY=0; //设置抓取点Y坐标
varoldLeft; //保存正常状态的X坐标
varoldTop; //保存正常状态的Y坐标
this.mousedown=function(){ //按下拖动点
Bar.setCapture();//设置抓取
offX=parseInt(event.clientX)-parseInt(Window.style.left);
offY=parseInt(event.clientY)-parseInt(Window.style.top);
isMouseDown=true;
if(Window.style.zIndex<=zindex){
zindex++;
Window.style.zIndex=zindex;
}
}
this.mousemove=function(){ //拖动窗口
if(isMouseDown&&!maximum){
Bar.style.cursor='move'
Window.style.left=event.clientX-offX;
Window.style.top=event.clientY-offY;
if(Window.style.zIndex<=zindex){
zindex++;
Window.style.zIndex=zindex;
}
}
}
this.mouseup=function(){ //松开按钮
Bar.releaseCapture(); //取消抓取
Bar.style.cursor='hand';
if(parseInt(Window.style.top)<0){
Window.style.top='0px';
}
if(parseInt(Window.style.left)<0){
Window.style.left='0px';
}
isMouseDown=false;
}
this.dblclick=function(){ //双击最大最小化
if(!maximum){
oldLeft=Window.style.left; //保存正常状态的X坐标
oldTop=Window.style.top; //保存正常状态的Y坐标
Window.style.left='0px';
Window.style.top='0px';
Window.style.width=document.body.clientWidth; //网页可见区域宽
Title.style.width=(document.body.clientWidth-40)+'px'; //设置标题长度
ReSize.style.display='none';
if(Change.innerText=='-'){
Window.style.height='100%';
Content.style.height=document.body.clientHeight-20;//网页可见区域宽-标题高度
}else{
Window.style.height='20px';
}
maximum=true;
}else{
Window.style.left=oldLeft;
Window.style.top=oldTop;
Window.style.width=width+'px';
Title.style.width=(width-40)+'px';
ReSize.style.display='';
if(Change.innerText=='-'){
Window.style.height=height+'px';
Content.style.height=parseInt(height-20)+'px';
}else{
Window.style.height='20px';
}
maximum=false;
}
if(Window.style.zIndex<=zindex){
zindex++;
Window.style.zIndex=zindex;
}
}
this.changeWindow=function(){ //收缩窗口
event.cancelBubble=true;
if(Change.innerText=='-'){
Window.style.height='20px';
Change.innerText='□';
Content.style.display='none';
ReSize.style.display='none';
}else{
if(maximum){
Window.style.height='100%';
Content.style.display='';
ReSize.style.display='';
Content.style.height=document.body.clientHeight-20;//网页可见区域宽-标题高度
}else{
Window.style.height=height+'px';
Content.style.display='';
ReSize.style.display='';
Content.style.height=parseInt(height-20)+'px';
}
Change.innerText='-';
}
}
varWindow=document.createElement("div"); Window.id="divWindow"+name; Window.className="divWindow"; Window.style.left=left+'px'; Window.style.top=top+'px'; Window.style.width=width+'px'; Window.style.height=height+'px'; Window.onclick=function(){ if(parseInt(Window.style.zIndex)<=zindex){ zindex++; Window.style.zIndex=zindex; } } this.Window=Window; //公有属性,类外可操作;若要在类外操作,可将元素改为公有属性 varBar=document.createElement("div"); Bar.id="divBar"+name; Bar.onselectstart="returnfalse"; Bar.className="divBar"; Bar.onmousedown=this.mousedown; Bar.ondblclick=this.dblclick; Bar.onmousemove=this.mousemove; Bar.onmouseup=this.mouseup; Window.appendChild(Bar); varTitle=document.createElement("span"); Title.id="divTitle"+name; Title.className="divTitle"; Title.style.width=(width-40)+'px'; //自适应标题长度 Title.innerText=title; Bar.appendChild(Title); varChange=document.createElement("span"); Change.id="divChange"+name; Change.className="divChange"; Change.innerText="-"; Change.ondblclick=this.changeWindow; Change.onclick=this.changeWindow; Bar.appendChild(Change); varClose=document.createElement("span"); Close.id="divClose"+name; Close.onclick=function(){ Window.style.display='none'; } Close.className="divClose"; Close.innerText="×"; Bar.appendChild(Close); varContent=document.createElement("div"); Content.id="divContent"+name; Content.className="divContent" Content.innerHTML=content; Content.style.height=parseInt(height-20)+'px'; Window.appendChild(Content); varReSize=document.createElement("div"); ReSize.className="divReSize"; ReSize.onmousedown=function(){ if(Window.style.zIndex<=zindex){ zindex++; Window.style.zIndex=zindex; } ReSize.setCapture(); isMouseDown=true; } ReSize.onmousemove=function(){ if(isMouseDown&&!maximum) { width=parseInt(event.clientX)-parseInt(Window.style.left)+5; height=parseInt(event.clientY)-parseInt(Window.style.top)+5; if(width>100){ //设置最小宽度 Window.style.width=width+'px'; Title.style.width=(width-40)+'px'; } if(height>100){ //设置最小高度 Window.style.height=height+'px'; Content.style.height=parseInt(height-20)+'px'; } } } ReSize.onmouseup=function(){ ReSize.releaseCapture(); isMouseDown=false; } Window.appendChild(ReSize); varIframe=document.createElement("iframe");//添加iframe,IE6.0下遮挡<select>控件 Iframe.className="divIframe"; Window.appendChild(Iframe); document.body.appendChild(Window);
} </script> <body> <script> //dragClass(ID,窗口标题,内容,X坐标,Y坐标,宽,长) varc1="窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1窗口1"; objWin1=newdragClass('win1','拖动窗口1',c1,0,150,300,300); varc2="窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2窗口2"; objWin2=newdragClass('win2','拖动窗口2',c2,350,150,300,300); varobjWin3; functionopenWin(){ if(objWin3==null){ varc3="123窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3窗口3"; objWin3=newdragClass('win3',c3,c3,700,150,300,300); }else{ if(objWin3.Window.style.display=='none'){ objWin3.Window.style.display=''; } } } </script> <inputtype="button"value="弹出【窗口3】"onClick="openWin()"/> </body> </html>