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>JS实现可缩放、拖动、关闭和最小化的浮动窗口</title>
</head>
<style type="text/css">
 .divWindow{word-wrap:break-word;position:absolute; overflow:hidden;}
 .divBar{border:#000000 1px solid;position:absolute;border-bottom:#000000 1px solid;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:#000000 1px solid;border-top:#000000 0px solid;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>
<script language="javascript">
var zindex=0  //全局变量
function dragClass(name,title,content,left,top,width,height){
var isMouseDown=false;
var maximum=false;
var offX=0;   //设置抓取点X坐标
var offY=0;   //设置抓取点Y坐标
var oldLeft;  //保存正常状态的X坐标
var oldTop;   //保存正常状态的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='-';
     }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgfgdy.html