本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)鼠標(biāo)拖拽調(diào)整div大小的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)思路:
- 根據(jù)鼠標(biāo)位置改變鼠標(biāo)樣式
- 當(dāng)鼠標(biāo)在div的邊緣和四個(gè)角時(shí)顯示不同的樣式,通過cursor修改
- 當(dāng)鼠標(biāo)在div的邊緣和四個(gè)角按下時(shí)記錄具體坐標(biāo)點(diǎn)位置, 并開始根據(jù)鼠標(biāo)的移動(dòng)修改div的尺寸
- 鼠標(biāo)松開時(shí)結(jié)束尺寸修改
代碼實(shí)現(xiàn):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < title >Title</ title > < style > body, html { width: 100%; height: 100%; margin: 0; } #container { width: 200px; height: 200px; padding: 15px; border: #00cdcd 2px solid; box-sizing: border-box; } .item { cursor: default; width: 100%; height: 100%; background: #757575; } </ style > </ head > < body id = "body" > < div id = "container" > < div class = "item" ></ div > </ div > < script > //需要調(diào)整尺寸的div let c = document.getElementById('container') // body監(jiān)聽移動(dòng)事件 document.getElementById('body').addEventListener('mousemove', move) // 鼠標(biāo)按下事件 c.addEventListener('mousedown', down) // 鼠標(biāo)松開事件 document.getElementById('body').addEventListener('mouseup', up) // 是否開啟尺寸修改 let resizeable = false // 鼠標(biāo)按下時(shí)的坐標(biāo),并在修改尺寸時(shí)保存上一個(gè)鼠標(biāo)的位置 let clientX, clientY // div可修改的最小寬高 let minW = 8, minH = 8 // 鼠標(biāo)按下時(shí)的位置,使用n、s、w、e表示 let direc = '' // 鼠標(biāo)松開時(shí)結(jié)束尺寸修改 function up() { resizeable = false } // 鼠標(biāo)按下時(shí)開啟尺寸修改 function down(e) { let d = getDirection(e) // 當(dāng)位置為四個(gè)邊和四個(gè)角時(shí)才開啟尺寸修改 if (d !== '') { resizeable = true direc = d clientX = e.clientX clientY = e.clientY } } // 鼠標(biāo)移動(dòng)事件 function move(e) { let d = getDirection(e) let cursor if (d === '') cursor = 'default'; else cursor = d + '-resize'; // 修改鼠標(biāo)顯示效果 c.style.cursor = cursor; // 當(dāng)開啟尺寸修改時(shí),鼠標(biāo)移動(dòng)會(huì)修改div尺寸 if (resizeable) { // 鼠標(biāo)按下的位置在右邊,修改寬度 if (direc.indexOf('e') !== -1) { c.style.width = Math.max(minW, c.offsetWidth + (e.clientX - clientX)) + 'px' clientX = e.clientX } // 鼠標(biāo)按下的位置在上部,修改高度 if (direc.indexOf('n') !== -1) { c.style.height = Math.max(minH, c.offsetHeight + (clientY - e.clientY)) + 'px' clientY = e.clientY } // 鼠標(biāo)按下的位置在底部,修改高度 if (direc.indexOf('s') !== -1) { c.style.height = Math.max(minH, c.offsetHeight + (e.clientY - clientY)) + 'px' clientY = e.clientY } // 鼠標(biāo)按下的位置在左邊,修改寬度 if (direc.indexOf('w') !== -1) { c.style.width = Math.max(minW, c.offsetWidth + (clientX - e.clientX)) + 'px' clientX = e.clientX } } } // 獲取鼠標(biāo)所在div的位置 function getDirection(ev) { let xP, yP, offset, dir; dir = ''; xP = ev.offsetX; yP = ev.offsetY; offset = 10; if (yP < offset ) dir += 'n'; else if (yP > c.offsetHeight - offset) dir += 's'; if (xP < offset ) dir += 'w'; else if (xP > c.offsetWidth - offset) dir += 'e'; return dir; } </ script > </ body > </ html > |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/BDawn/article/details/114374460