DIV CSS 佈局教程網

用 JavaScript 做可拖拉的分欄 iframe:左樹狀目錄、右內容頁
編輯:JavaScript綜合知識     

舊式的後台介面很常見這種配置:左邊一個 iframe 放樹狀目錄,右邊一個 iframe 放內容頁,中間那條分隔線可以拖動來調整寬度。這篇記錄當時的實作思路,也把它的效能問題一併講清楚。

操作方式

滑鼠移到兩個 iframe 之間時游標會變成拖拉樣式,可以水平拖(改變寬度)、也可以垂直拖(改變高度)。

程式碼

<html>
<script language="javascript">
var mouseX = 0;
var mouseY = 0;
var w = 5;                       // 分隔條寬度

function divonmousemove() {
  var obj1  = document.getElementById("a");
  var obj2  = document.getElementById("b");
  var 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">
<div id='ab'
     onmousemove="divonmousemove();"
     onmousedown="divonmousedown();"
     onmouseup="divonmouseup();">
  <table style="width:100%;height:100%;" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td style="width:150px;">
        <iframe id="a" src="tree.htm"
                style="width:100%;height:100%;"></iframe>
      </td>
      <td style="width:3px;cursor:e-resize;background-color:#cccccc;"
          align="center" valign="middle">
        <font style="background-color:#eeeeee;cursor:pointer;">&lt;&lt;</font>
      </td>
      <td>
        <iframe id="b" name="ContentFrame" src="index.htm"
                style="width:100%;height:100%;"></iframe>
      </td>
    </tr>
  </table>
</div>
</body>
</html>

效能上的真實問題

作者自己點出了關鍵:有些實作會在放開滑鼠之後才套用新尺寸,幾乎不吃 CPU;而這份程式碼是邊拖邊改,每一次 mousemove 都在重排版兩個 iframe, iframe 裡的內容還得跟著重繪,所以拖起來明顯卡頓。

改良方向很直接:拖動過程中只更新一條浮動的提示線(一個 position: absolute 的細 div),滑鼠放開時才真正去改 iframe 的寬高。這樣 mousemove 只動一個元素,成本極低。

還有一個字串引號的 bug

原始碼裡 onmouseleave 那段寫成 style.cursor='' 卻嵌在單引號屬性值中,引號會提早結束,HTML 結構就壞了。正確寫法是屬性用雙引號、程式碼內用單引號,或反過來,兩層不要互相打架。

今天的做法

iframe 分欄拖拉這個需求,現在有兩個更好的選擇:CSS 的 resize: horizontal 可以讓元素自帶縮放手把;而真正的可調整分割面板,用 display: grid 加上 grid-template-columns: 200px 1fr 再搭配 Pointer Events 更新那個欄寬值,程式碼量不到這裡的三分之一,而且觸控裝置也能用。上面這份留著當教材還不錯,能看清「拖動」本質上就是持續更新某個元素的寬度值這件事。

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