鼠标拖拽DIV时的吸附效果

3 篇文章 0 订阅
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>拖动时的吸附效果</title>
<style>
* {
 padding: 0;
 margin: 0;
}
#box1 {
 width: 500px;
 height: 500px;
 background: #999;
 position: relative;
 left: 100px;
 top: 100px;
}
#box {
 width: 100px;
 height: 100px;
 background: #334;
 position: absolute;
 cursor: move;
}
</style>
<body>

<div id="box1">
<div id="box"></div>
</div>
</body>

</html>



<script>
(function () {
 var dragging = false
 var boxX, boxY, mouseX, mouseY, offsetX, offsetY
 var box = document.getElementById('box')
 var box1 = document.getElementById('box1')

//进行吸附的范围,比如距离100像素时产生吸附
 const RANGE = 100;
 
 // 鼠标按下的动作
 box.onmousedown = down
 // 鼠标的移动动作
 document.onmousemove = move
 // 释放鼠标的动作
 document.onmouseup = up
 
 // 鼠标按下后的函数,e为事件对象
 function down(e) {
 dragging = true
 
 // 获取元素所在的坐标
 boxX = box.offsetLeft
 boxY = box.offsetTop
 
 // 获取鼠标所在的坐标
 mouseX = parseInt(getMouseXY(e).x)
 mouseY = parseInt(getMouseXY(e).y)
 
 // 鼠标相对元素左和上边缘的坐标
 offsetX = mouseX - boxX
 offsetY = mouseY - boxY
 }
 
 // 鼠标移动调用的函数
 function move(e){
 if (dragging) {
  // 获取移动后的元素的坐标
  var x = getMouseXY(e).x - offsetX
  var y = getMouseXY(e).y - offsetY
 
  // 计算可移动位置的大小, 保证元素不会超过可移动范围
  // 此处就是父元素的宽度减去子元素宽度
  var width = box1.clientWidth - box.offsetWidth
  var height = box1.clientHeight - box.offsetHeight
 
  // min方法保证不会超过右边界,max保证不会超过左边界
  x = Math.min(Math.max(0, x), width)
  y = Math.min(Math.max(0, y), height)
 
  // 磁性吸附部分
  if (x < RANGE) {x = 0}
  if (width - x < RANGE) {x = width}
  if (y < RANGE) {y = 0}
  if (height - y < RANGE) {y = height}
 
  // 给元素及时定位
  box.style.left = x + 'px'
  box.style.top = y + 'px'
 }
 }
 
 // 释放鼠标的函数
 function up(e){
 dragging = false
 }
 
 // 函数用于获取鼠标的位置
 function getMouseXY(e){
 var x = 0, y = 0
 e = e || window.event
 
 if (e.pageX) {
  x = e.pageX
  y = e.pageY
 } else {
  x = e.clientX + document.body.scrollLeft - document.body.clientLeft
  y = e.clientY + document.body.scrollTop - document.body.clientTop
 }
 return {
  x: x,
  y: y
 }
 }
})()
</script>

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值