本文實例講述了JS實現可縮放、拖動、關閉和最小化的浮動窗口方法。分享給大家供大家參考。具體實現方法如下:
var Window=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;
//公有屬性,類外可操作;若要在類外操作,可將元素改為公有屬性
var Bar=document.createElement("div");
Bar.id="divBar"+name;
Bar.onselectstart="return false";
Bar.className="divBar";
Bar.onmousedown=this.mousedown;
Bar.ondblclick=this.dblclick;
Bar.onmousemove=this.mousemove;
Bar.onmouseup=this.mouseup;
Window.appendChild(Bar);
var Title=document.createElement("span");
Title.id="divTitle"+ name;
Title.className="divTitle";
Title.style.width=(width-40)+'px'; //自適應標題長度
Title.innerText=title;
Bar.appendChild(Title);
var Change=document.createElement("span");
Change.id="divChange"+ name;
Change.className="divChange";
Change.innerText="-";
Change.ondblclick=this.changeWindow;
Change.onclick=this.changeWindow;
Bar.appendChild(Change);
var Close=document.createElement("span");
Close.id="divClose"+ name;
Close.onclick=function(){
Window.style.display='none';
}
Close.className="divClose";
Close.innerText="×";
Bar.appendChild(Close);
var Content=document.createElement("div");
Content.id="divContent"+ name;
Content.className="divContent"
Content.innerHTML=content;
Content.style.height=parseInt(height-20)+'px';
Window.appendChild(Content);
var ReSize=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);
var Iframe=document.createElement("iframe"); //添加iframe,IE6.0下遮擋<select>控件
Iframe.className="divIframe";
Window.appendChild(Iframe);
document.body.appendChild(Window);
}
</script>
<body>
<script>
//dragClass(ID,窗口標題,內容,X坐標,Y坐標,寬,長)
var c1="窗口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=new dragClass('win1','拖動窗口1',c1,0,150,300,300);
var c2="窗口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=new dragClass('win2','拖動窗口2',c2,350,150,300,300);
var objWin3;
function openWin(){
if(objWin3==null){
var c3="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=new dragClass('win3',c3,c3,700,150,300,300);
}else{
if(objWin3.Window.style.display=='none'){
objWin3.Window.style.display='';
}
}
}
</script>
<input type="button" value="彈出【窗口3】" onClick="openWin()" />
</body>
</html>
希望本文所述對大家的javascript程序設計有所幫助。
新聞熱點
疑難解答
圖片精選