DIV CSS 佈局教程網

用 JS 打造可拖曳調整寬度的雙 Iframe 版面
編輯:JavaScript綜合知識     

左側 iframe 放樹狀目錄、右側 iframe 顯示內容頁,中間那條分隔線若能讓使用者拖動調整寬度,操作體驗會好很多。本文提供三個由簡到精的實作版本,並說明各自的取捨。

版本一:直接監聽滑鼠座標

最直白的做法是在分隔用的 div 上綁定 mousemove、mousedown、mouseup,透過滑鼠位移量即時改寫左右兩個 iframe 的寬高。作者特別提到一個小技巧:把分隔條設為 filter:alpha(opacity=0) 讓它透明但仍可接收滑鼠事件。

缺點也很明顯——這種「邊拖邊重繪」的方式會持續吃 CPU,操作起來略顯卡頓。作者原本的靈感來自某些論壇「放開滑鼠才調整大小」的做法,效能表現反而更好。

<html>
<script language="javascript">
 var mouseX = 0;
 var mouseY = 0;
 var w=5;
 function divonmousemove(){
 obj1=document.getElementById("a");
 obj2=document.getElementById("b");
 obj12=document.getElementById("ab");
 if (mouseX!==event.x && mouseY!==event.y)obj12.style.cursor='se-resize';
 else if (mouseX!==event.x)obj12.style.cursor='e-resize';
 else if (mouseY!==event.y)obj12.style.cursor='s-resize';
 else obj12.style.cursor='';
 if (event.button==1){
 obj1.style.width=parseInt(obj1.offsetWidth)+(event.x - mouseX);
 mouseX=event.x;
 obj1.style.height=parseInt(obj1.offsetHeight)+(event.y - mouseY);
 mouseY= event.y;
 obj12.style.width=108;
 obj12.style.left=obj1.offsetWidth-obj12.offsetWidth/2;
 obj12.style.height=obj1.clientHeight;
 obj2.style.height=obj1.clientHeight;
 obj2.style.left=obj1.clientWidth+w;
 obj2.style.width=screen.width-obj1.offsetWidth-w;
 }}
function divonmousedown(){  
 mouseX = event.x;
 mouseY = event.y;
 }
function divonmouseup(){
 obj12.style.left=obj1.offsetWidth;
 obj12.style.width=w;
 mouseX = 0;
 mouseY = 0;}
 </script>
 <body style='margin:0'>
 <iframe zindex=1 id="a" src="https://www.jb51.net/Tree/tree.htm" style="width:200;height:610;position:absolute;z-index:9 "></iframe>
 <div zindex=0 id='ab' onmousemove='divonmousemove();' onmouseleave='document.getElementById("ab").style.cursor="";'
 onmousedown='divonmousedown();' onmouseup='divonmouseup();'
 style='filter:alpha(opacity=0);width:5;height:799;background:#aaffaa;position:absolute;left:200;z-index:100' title='按下鼠標拖動大小'></div> 
 <iframe zindex=1 id="b" name="ContentFrame" src="https://www.jb51.net/index.htm" style="width:799;height:612;position:absolute;left:205;z-index:10"></iframe>
 </body>
  </html>

版本二:改用 table 三欄式排版

把絕對定位換成 table 的三欄結構,中間那一欄就是分隔條。這個版本用 setCapture() 與 releaseCapture() 鎖定滑鼠事件,並以 previousSibling.offsetWidth 取得左欄目前寬度,程式碼乾淨許多,也更接近現代前端思維。

<script language="javascript">
var isResizing=false;
function Resize_mousedown(event,obj){
obj.mouseDownX=event.clientX;
obj.leftTdW=obj.previousSibling.offsetWidth;
obj.setCapture();
isResizing=true;
}
function Resize_mousemove(event,obj){
if(!isResizing) return ;
var newWidth=obj.leftTdW*1+event.clientX*1-obj.mouseDownX;
if(newWidth>0) obj.previousSibling.style.width = newWidth;
else obj.previousSibling.style.width=1;
}
function Resize_mouseup(event,obj){
if(!isResizing) return;
obj.releaseCapture();
isResizing=false;
}
</script>
<body style='margin:0'>
<table style="width:100%;height:100%;" border=0 cellspacing=0 cellpadding=0px>
<tr>
<td style="width:150px;">
<iframe zindex=1 id="a" src="https://www.jb51.net/Tree/tree.htm" style="width:100%;height:100%;z-index:9"></iframe>
</td>
<td style="width:2px;cursor:e-resize;background-color:#cccccc;" onmousedown="Resize_mousedown(event,this);" onmouseup="Resize_mouseup(event,this);" onmousemove="Resize_mousemove(event,this);">
</td>
<td>
<iframe zindex=1 id="b" name="ContentFrame" src="https://www.jb51.net/index.htm" style="width:100%;height:100%;z-index:10"></iframe>
</td>
</tr>
</table>
</body>

版本三:加上一鍵收合

在版本二的基礎上多寫一個 Resize_setDefault(),點擊分隔條上的「<」符號就能把左欄收起到只剩 1px,再點一次恢復成 150px。對經常需要專注看右側內容的使用者來說,這個小功能非常實用。

<script language="javascript">
var isResizing=false;
function Resize_mousedown(event,obj){
obj.mouseDownX=event.clientX;
obj.leftTdW=obj.previousSibling.offsetWidth;
obj.setCapture();
isResizing=true;
}
function Resize_mousemove(event,obj){
if(!isResizing) return ;
var newWidth=obj.leftTdW*1+event.clientX*1-obj.mouseDownX;
if(newWidth>0) obj.previousSibling.style.width = newWidth;
else obj.previousSibling.style.width=1;
}
function Resize_mouseup(event,obj){
if(!isResizing) return;
obj.releaseCapture();
isResizing=false;
}
function Resize_setDefault(event,obj){
if(obj.innerText=="<") {
obj.parentNode.previousSibling.style.width=1;
obj.innerText=">";
}
else{
obj.parentNode.previousSibling.style.width=150;
obj.innerText="<";
}
event.cancelBubble=true;
}
</script>
<body style='margin:0'>
<table style="width:100%;height:100%;" border=0 cellspacing=0 cellpadding=0px>
<tr>
<td style="width:150px;">
<iframe zindex=1 id="a" src="https://www.jb51.net/Tree/tree.htm" style="width:100%;height:100%;z-index:9"></iframe>
</td>
<td style="width:3px;cursor:e-resize;background-color:#cccccc;" align="center" valign="middle" onmousedown="Resize_mousedown(event,this);" onmouseup="Resize_mouseup(event,this);" onmousemove="Resize_mousemove(event,this);">
<font style="size:3px;background-color:#eeeeee;cursor:pointer;" onmousedown="Resize_setDefault(event,this);"><</font>
</td>
<td>
<iframe zindex=1 id="b" name="ContentFrame" src="https://www.jb51.net/index.htm" style="width:100%;height:100%;z-index:10"></iframe>
</td>
</tr>
</table>
</body>

後續優化方向

這些範例都屬於較早期的寫法,依賴 IE 專屬的 setCapture 與 event 全域物件。若要在今日的瀏覽器上運行,建議改用標準事件模型(addEventListener、event.pageX)並搭配 CSS resize 或 pointer events,同時在拖曳期間對重繪做節流(throttle),就能兼顧流暢度與效能。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved