鼠标拖拽调整div大小
实现思路
- 根据鼠标位置改变鼠标样式
- 当鼠标在div的边缘和四个角时显示不同的样式,通过cursor修改
- 当鼠标在div的边缘和四个角按下时记录具体坐标点位置, 并开始根据鼠标的移动修改div的尺寸
- 鼠标松开时结束尺寸修改
代码实现
<!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>let c = document.getElementById('container')document.getElementById('body').addEventListener('mousemove', move)c.addEventListener('mousedown', down)document.getElementById('body').addEventListener('mouseup', up)let resizeable = falselet clientX, clientYlet minW = 8, minH = 8let direc = ''function up() {resizeable = false}function down(e) {let d = getDirection(e)if (d !== '') {resizeable = truedirec = dclientX = e.clientXclientY = e.clientY}}function move(e) {let d = getDirection(e)let cursorif (d === '') cursor = 'default';else cursor = d + '-resize';c.style.cursor = cursor;if (resizeable) {if (direc.indexOf('e') !== -1) {c.style.width = Math.max(minW, c.offsetWidth + (e.clientX - clientX)) + 'px'clientX = e.clientX}if (direc.indexOf('n') !== -1) {c.style.height = Math.max(minH, c.offsetHeight + (clientY - e.clientY)) + 'px'clientY = e.clientY}if (direc.indexOf('s') !== -1) {c.style.height = Math.max(minH, c.offsetHeight + (e.clientY - clientY)) + 'px'clientY = e.clientY}if (direc.indexOf('w') !== -1) {c.style.width = Math.max(minW, c.offsetWidth + (clientX - e.clientX)) + 'px'clientX = e.clientX}}}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>
功能增强,通过八个点拖动元素尺寸
<!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;position: relative;margin: 40px;}.point{display: none;}#container:hover .point{display: block;width: 4px;height: 4px;border-radius: 2px;border: #08253b 1px solid;position: absolute;}.top{top: -2px;left: 50%;}.bottom{bottom: -2px;left: 50%;}.left{left: -2px;top: 50%;}.right{right: -2px;top: 50%;}.top-left{top: -2px;left: -2px;}.bottom-left{bottom: -2px;left: -2px;}.top-right{top: -2px;right: -2px;}.bottom-right{bottom: -2px;right: -2px;}.item {cursor: default;width: 100%;height: 100%;background: #757575;}</style>
</head>
<body id="body">
<div id="container"><div class="item"></div><div class="point top"></div><div class="point left"></div><div class="point right"></div><div class="point bottom"></div><div class="point top-left"></div><div class="point top-right"></div><div class="point bottom-left"></div><div class="point bottom-right"></div>
</div>
<script>let c = document.getElementById('container')document.getElementById('body').addEventListener('mousemove', move)c.addEventListener('mousedown', down)document.getElementById('body').addEventListener('mouseup', up)let resizeable = falselet clientX, clientYlet minW = 8, minH = 8let direc = ''function up() {resizeable = false}function down(e) {let d = getDirection(e)if (d !== '') {resizeable = truedirec = dclientX = e.clientXclientY = e.clientY}}function move(e) {console.log(e);let d = getDirection(e)let cursorif (d === '') cursor = 'default';else cursor = d + '-resize';c.style.cursor = cursor;if (resizeable) {if (direc.indexOf('e') !== -1) {c.style.width = Math.max(minW, c.offsetWidth + (e.clientX - clientX)) + 'px'clientX = e.clientX}if (direc.indexOf('n') !== -1) {c.style.height = Math.max(minH, c.offsetHeight + (clientY - e.clientY)) + 'px'clientY = e.clientY}if (direc.indexOf('s') !== -1) {c.style.height = Math.max(minH, c.offsetHeight + (e.clientY - clientY)) + 'px'clientY = e.clientY}if (direc.indexOf('w') !== -1) {c.style.width = Math.max(minW, c.offsetWidth + (clientX - e.clientX)) + 'px'clientX = e.clientX}}}function getDirection(ev) {let dir = '';if (ev.target.className.indexOf('top') >= 0) dir += 'n';else if (ev.target.className.indexOf('bottom') >= 0) dir += 's';if (ev.target.className.indexOf('left') >= 0) dir += 'w';else if (ev.target.className.indexOf('right') >= 0) dir += 'e';return dir;}
</script>
</body>
</html>