說在前面
元素拖拽和縮放現在也是一個很常見的功能,讓我們從實現div元素的拖拽縮放開始來了解元素拖拽縮放的具體原理和實現方法吧。
效果展示
實現步驟
畫一個div
首先我們需要先畫一個div,并給它8個方位(上、下、左、右、左上、右上、右下、左下)加上縮放錨點,我們可以通過這幾個錨點來對div進行縮放,具體代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="./index.css" />
</head>
<body>
<div class="box" id="drag">
<div class="resize-handle top-left"></div>
<div class="resize-handle top"></div>
<div class="resize-handle top-right"></div>
<div class="resize-handle right"></div>
<div class="resize-handle bottom-right"></div>
<div class="resize-handle bottom"></div>
<div class="resize-handle bottom-left"></div>
<div class="resize-handle left"></div>
</div>
</body>
<script src="./index.js"></script>
</html>
加點css
將錨點定位到其特定的位置上,并設置不同的鼠標指示效果,具體代碼如下:
.box {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 200px;
height: 200px;
background-color: #f0f0f0;
cursor: move;
}
.resize-handle {
position: absolute;
width: 10px;
height: 10px;
background-color: #000;
}
.top-left {
top: -5px;
left: -5px;
cursor: nw-resize;
}
.top {
top: -5px;
left: calc(50% - 5px);
cursor: ns-resize;
}
.top-right {
top: -5px;
right: -5px;
cursor: ne-resize;
}
.right {
top: calc(50% - 5px);
right: -5px;
cursor: ew-resize;
}
.bottom-right {
bottom: -5px;
right: -5px;
cursor: se-resize;
}
.bottom {
bottom: -5px;
left: calc(50% - 5px);
cursor: ns-resize;
}
.bottom-left {
bottom: -5px;
left: -5px;
cursor: sw-resize;
}
.left {
top: calc(50% - 5px);
left: -5px;
cursor: ew-resize;
}
效果是這樣的:
完成拖拽和縮放功能
拖拽功能
我們需要鼠標在div內按下不松開的時候,div可以跟著鼠標的位置移動,這里我們可以從鼠標的三種事件的觸發來入手:
首先我們需要監聽鼠標按下事件。
const dragElement = document.getElementById("drag");
dragElement.addEventListener("mousedown", startDrag);
判斷點擊事件是否為錨點觸發,是的話則不執行拖拽邏輯。這里我們可以通過樣式名來判斷,錨點我們都給它加上了resize-handle
,我們只需要判斷樣式名是否包含resize-handle
就可以。
function startDrag(event) {
event.preventDefault();
const currentHandle = event.target;
const isResizeHandle = currentHandle.className.includes("resize-handle");
if (isResizeHandle) return;
}
記錄下鼠標點擊的坐標及拖拽元素所在位置,用于后面計算鼠標移動距離和對拖拽元素進行移動,監聽鼠標移動和抬起事件
const startX = event.clientX;
const startY = event.clientY;
const startLeft = dragElement.offsetLeft;
const startTop = dragElement.offsetTop;
document.addEventListener("mousemove", drag);
document.addEventListener("mouseup", stopDrag);
鼠標移動的時候計算鼠標當前位置與鼠標點擊位置的相對距離,將拖拽元素的位置也移動相應的相對距離
function drag(event) {
const dx = event.clientX - startX;
const dy = event.clientY - startY;
const newLeft = startLeft + dx;
const newTop = startTop + dy;
dragElement.style.left = newLeft + "px";
dragElement.style.top = newTop + "px";
}
鼠標抬起的時候將鼠標移動和鼠標抬起的監聽事件移除。
function stopDrag() {
document.removeEventListener("mousemove", drag);
document.removeEventListener("mouseup", stopDrag);
}
到這里我們就完成了一個最基本的可拖拽元素了,接下來就該來實現縮放功能了。
縮放功能
我們希望在點擊縮放錨點進行移動的時候,元素會隨著鼠標繼續縮小或放大,這里我們仍然是要從鼠標觸發的三種事件來入手:
我們需要監聽所有錨點的鼠標按下事件,首先我們需要先獲取所有的錨點元素:
const resizeHandles = document.getElementsByClassName("resize-handle");
再監聽所有錨點元素的鼠標按下事件:
Array.from(resizeHandles).forEach((handle) => {
handle.addEventListener("mousedown", startResize);
});
記錄鼠標按下的初始位置及縮放元素的初始位置和寬高,便于后面對縮放元素進行縮放操作,并為縮放元素加上鼠標移動和鼠標抬起的監聽事件。
function startResize(event) {
event.preventDefault();
const currentHandle = event.target;
const direction = currentHandle.className.split(" ")[1];
startX = event.clientX;
startY = event.clientY;
startWidth = dragElement.offsetWidth;
startHeight = dragElement.offsetHeight;
startLeft = dragElement.offsetLeft;
startTop = dragElement.offsetTop;
document.addEventListener("mousemove", resize);
document.addEventListener("mouseup", stopResize);
}
鼠標移動的時候我們需要判斷當前是從哪個錨點觸發的縮放事件,我們可以從錨點的樣式名className
來做區分。
const currentHandle = event.target;
const direction = currentHandle.className.split(" ")[1];
不同錨點我們需要對元素進行不同的操作,我們可以分為四個方位來進行判斷并區分操作:
(1)左邊
判斷樣式名是否包含left
,左邊錨點會觸發元素的寬度變化及左右位置的變化,這時候鼠標左移相對于元素來說是放大,右移相對于元素來說是縮小,所以元素的寬度應該減去鼠標的位移距離。
if (direction.includes("left")) {
width = startWidth - dx + "px";
left = startLeft + dx / 2 + "px";
}
(2)右邊
判斷樣式名是否包含right
,右邊錨點會觸發元素的寬度變化及左右位置的變化,這時候鼠標左移相對于元素來說是縮小,右移相對于元素來說是放大,所以元素的寬度應該加上鼠標的位移距離。
if (direction.includes("right")) {
width = startWidth + dx + "px";
left = startLeft + dx / 2 + "px";
}
(3)上邊
判斷樣式名是否包含top
,上邊錨點會觸發元素的高度變化及上下位置的變化,這時候鼠標上移相對于元素來說是放大,下移相對于元素來說是縮小,所以元素的高度應該減去鼠標的位移距離。
if (direction.includes("top")) {
height = startHeight - dy + "px";
top = startTop + dy / 2 + "px";
}
(4)下邊
判斷樣式名是否包含bottom
,下邊錨點會觸發元素的高度變化及上下位置的變化,這時候鼠標上移相對于元素來說是縮小,下移相對于元素來說是放大,所以元素的高度應該加上鼠標的位移距離。
if (direction.includes("bottom")) {
height = startHeight + dy + "px";
top = startTop + dy / 2 + "px";
}
我們還需要判斷當前元素的寬度和高度是否小于等于0,等于0的時候我們不再對其進行縮放操作,大于0則對元素進行賦值操作。
if (parseInt(width) <= 0 || parseInt(height) <= 0) return;
dragElement.style.width = width;
dragElement.style.height = height;
dragElement.style.left = left;
dragElement.style.top = top;
完整代碼如下:
function resize(event) {
const dx = event.clientX - startX;
const dy = event.clientY - startY;
let width = startWidth,
height = startHeight,
left = startLeft,
top = startTop;
if (direction.includes("left")) {
width = startWidth - dx + "px";
left = startLeft + dx / 2 + "px";
}
if (direction.includes("right")) {
width = startWidth + dx + "px";
left = startLeft + dx / 2 + "px";
}
if (direction.includes("top")) {
height = startHeight - dy + "px";
top = startTop + dy / 2 + "px";
}
if (direction.includes("bottom")) {
height = startHeight + dy + "px";
top = startTop + dy / 2 + "px";
}
if (parseInt(width) <= 0 || parseInt(height) <= 0) return;
dragElement.style.width = width;
dragElement.style.height = height;
dragElement.style.left = left;
dragElement.style.top = top;
}
鼠標抬起的時候將鼠標移動和鼠標抬起的監聽事件移除。
function stopResize() {
document.removeEventListener("mousemove", resize);
document.removeEventListener("mouseup", stopResize);
}
到這里我們就完成了一個最基本的可拖拽縮放的元素了。
完整代碼
完整的JavaScrip代碼如下:
const dragElement = document.getElementById("drag");
// 拖拽功能
dragElement.addEventListener("mousedown", startDrag);
function startDrag(event) {
event.preventDefault();
const currentHandle = event.target;
const isResizeHandle = currentHandle.className.includes("resize-handle");
if (isResizeHandle) return;
const startX = event.clientX;
const startY = event.clientY;
const startLeft = dragElement.offsetLeft;
const startTop = dragElement.offsetTop;
document.addEventListener("mousemove", drag);
document.addEventListener("mouseup", stopDrag);
function drag(event) {
const dx = event.clientX - startX;
const dy = event.clientY - startY;
const newLeft = startLeft + dx;
const newTop = startTop + dy;
dragElement.style.left = newLeft + "px";
dragElement.style.top = newTop + "px";
}
function stopDrag() {
document.removeEventListener("mousemove", drag);
document.removeEventListener("mouseup", stopDrag);
}
}
// 縮放功能
const resizeHandles = document.getElementsByClassName("resize-handle");
Array.from(resizeHandles).forEach((handle) => {
handle.addEventListener("mousedown", startResize);
});
function startResize(event) {
event.preventDefault();
const currentHandle = event.target;
const direction = currentHandle.className.split(" ")[1];
const startX = event.clientX;
const startY = event.clientY;
const startWidth = dragElement.offsetWidth;
const startHeight = dragElement.offsetHeight;
const startLeft = dragElement.offsetLeft;
const startTop = dragElement.offsetTop;
document.addEventListener("mousemove", resize);
document.addEventListener("mouseup", stopResize);
function resize(event) {
const dx = event.clientX - startX;
const dy = event.clientY - startY;
let width = startWidth,
height = startHeight,
left = startLeft,
top = startTop;
if (direction.includes("left")) {
width = startWidth - dx + "px";
left = startLeft + dx / 2 + "px";
}
if (direction.includes("right")) {
width = startWidth + dx + "px";
left = startLeft + dx / 2 + "px";
}
if (direction.includes("top")) {
height = startHeight - dy + "px";
top = startTop + dy / 2 + "px";
}
if (direction.includes("bottom")) {
height = startHeight + dy + "px";
top = startTop + dy / 2 + "px";
}
if (parseInt(width) <= 0 || parseInt(height) <= 0) return;
dragElement.style.width = width;
dragElement.style.height = height;
dragElement.style.left = left;
dragElement.style.top = top;
}
function stopResize() {
document.removeEventListener("mousemove", resize);
document.removeEventListener("mouseup", stopResize);
}
}
該文章在 2023/11/1 9:47:47 編輯過