DIV CSS 佈局教程網

用 JavaScript 實作滑鼠拖曳調整圖層尺寸的完整做法
編輯:關於JavaScript     

在開發浮動視窗、可視化編輯器或自訂面板這類介面時,「讓使用者拖曳邊框改變區塊大小」幾乎是必備的互動細節。這個效果其實不必依賴任何第三方套件,純粹用原生 JavaScript 加上幾個滑鼠事件就能完成。以下是一份可直接存檔執行的範例。

 <html> <head> <title>拖動改變層的大小</title> <meta content="text/html; charset=gb2312" http-equiv="Content-Type"> <style> { box-sizing: border-box; moz-box-sizing: border-box } #testDiv { background-color: buttonface; background-repeat: repeat; background-attachment: scroll; color: #3969A5; height: 300px; left: 30px; overflow: hidden; width: 500; z-index: 2; border: 2px outset white; margin: 0px; padding: 2px; background-position: 0% 50% } body { font-family: Verdana; font-size: 9pt } #innerNice { background-color: white; background-repeat: repeat; background-attachment: scroll; color: #3969A5; height: 100%; overflow: auto; width: 100%; border: 2px inset white; padding: 8px; background-position: 0% 50% } </style> <SCRIPT language=javascript> var theobject = null; function resizeObject() { this.el = null; //pointer to the object this.dir = ""; //type of current resize (n,s,e,w,ne,nw,se,sw) this.grabx = null; //Some useful values this.graby = null; this.width = null; this.height = null; this.left = null; this.top = null; } function getDirection(el) { var xPos, yPos, offset, dir; dir = ""; xPos = window.event.offsetX; yPos = window.event.offsetY; offset = 8; //The distance from the edge in pixels if (yPos<offset) dir += "n"; else if (yPos > el.offsetHeight-offset) dir += "s"; if (xPos<offset) dir += "w"; else if (xPos > el.offsetWidth-offset) dir += "e"; return dir; } function doDown() { var el = getReal(event.srcElement, "className", "resizeMe"); if (el == null) { theobject = null; return; } dir = getDirection(el); if (dir == "") return; theobject = new resizeObject(); theobject.el = el; theobject.dir = dir; theobject.grabx = window.event.clientX; theobject.graby = window.event.clientY; theobject.width = el.offsetWidth; theobject.height = el.offsetHeight; theobject.left = el.offsetLeft; theobject.top = el.offsetTop; window.event.returnValue = false; window.event.cancelBubble = true; } function doUp() { if (theobject != null) { theobject = null; } } function doMove() { var el, xPos, yPos, str, xMin, yMin; xMin = 8; //The smallest width possible yMin = 8; // height el = getReal(event.srcElement, "className", "resizeMe"); if (el.className == "resizeMe") { str = getDirection(el); //Fix the cursor if (str == "") str = "default"; else str += "-resize"; el.style.cursor = str; } //Dragging starts here if(theobject != null) { if (dir.indexOf("e") != -1) theobject.el.style.width = Math.max(xMin, theobject.width + window.event.clientX - theobject.grabx) + "px"; if (dir.indexOf("s") != -1) theobject.el.style.height = Math.max(yMin, theobject.height + window.event.clientY - theobject.graby) + "px"; if (dir.indexOf("w") != -1) { theobject.el.style.left = Math.min(theobject.left + window.event.clientX - theobject.grabx, theobject.left + theobject.width - xMin) + "px"; theobject.el.style.width = Math.max(xMin, theobject.width - window.event.clientX + theobject.grabx) + "px"; } if (dir.indexOf("n") != -1) { theobject.el.style.top = Math.min(theobject.top + window.event.clientY - theobject.graby, theobject.top + theobject.height - yMin) + "px"; theobject.el.style.height = Math.max(yMin, theobject.height - window.event.clientY + theobject.graby) + "px"; } window.event.returnValue = false; window.event.cancelBubble = true; } } function getReal(el, type, value) { temp = el; while ((temp != null) && (temp.tagName != "BODY")) { if (eval("temp." + type) == value) { el = temp; return el; } temp = temp.parentElement; } return el; } document.onmousedown = doDown; document.onmouseup = doUp; document.onmousemove = doMove; </SCRIPT> </head> <body> <div class="resizeMe" id="testDiv"> <div id="innerNice"> <p align="center"> </p> <p align="center"> 請在邊框處拖動鼠標</p> <p> </p> <p> </p> <p> </p> </div> </div> </body> </html>

一、先判斷滑鼠壓在哪一條邊

核心在 getDirection()。它讀取游標在元素內部的相對座標,再和元素的寬高做比較;只要距離邊緣在 8 像素以內,就把它視為「命中」。上緣回報 n、下緣回報 s、左緣回報 w、右緣回報 e,因此右下角會得到 se 這樣的組合字串,八個方向一次涵蓋。

二、按下滑鼠時建立一份初始快照

doDown() 先用 getReal() 沿著父節點往上找,鎖定帶有 resizeMe 類別的那層容器,接著把元素當下的寬高、left、top 以及游標的 clientX/clientY 全部存進 resizeObject 物件。這份「起點資料」非常重要,後續所有位移計算都以它為基準,避免累積誤差。

三、拖曳過程中即時套用並守住下限

doMove() 同時負責兩件事:一是依目前所在的方向切換游標樣式(例如 se-resize),給使用者視覺回饋;二是真正改寫樣式。用 Math.max 把最小尺寸鎖在 8 像素,用 Math.min 避免往左上拖曳時把 left/top 推過頭,導致寬高變成負值。

現代化提醒

這份範例寫於 IE 盛行的年代,因此大量使用 window.event、srcElement、parentElement 等非標準寫法。若要在今日的專案中使用,建議改用 addEventListener 綁定事件、以 event.offsetX 或 getBoundingClientRect() 取得座標,並搭配 CSS 的 resize、min-width、max-width 等屬性,程式碼會乾淨許多,也更容易維護。

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