JavaScript拖拽

让一个长宽100px的div 在一个长宽为600px的div中拖拽 并且判断边界值

HTML部分

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box1 {
            width: 600px;
            height: 600px;
            border: 1px solid red;
            margin: 50px auto;
            position: relative;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background-color: aqua;
            position: absolute;
            cursor: move;
        }
    </style>
</head>

<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
 </body>

</html>

JavaScript部分

    <script>
       // 获取元素  
        var box2 = document.querySelector('.box2')
        //初始化
        init()
        function init() {
            handleDown()
            handleUp()
        }
  
        // 绑定按下事件
        function handleDown() {
            box2.onmousedown = function (e) {
            //兼容写法
            //作用:记录当次事件触发的时候的所有信息 保存在一个对象里面的
                var e = e || window.event
                //offsetX,offsetY
                //以当前触发的目标元素的内边框的左上角为坐标原点
                var w = e.offsetX
                var h = e.offsetY
                handleMove(w, h)
            }
        }
        // 绑定移动事件
        function handleMove(w, h) {
            document.onmousemove = function (e) {
                var e = e || window.event
                //pageX,pageY
                //以当前document文档的左上角为坐标原点 
                var x = e.pageX - w - box2.offsetParent.offsetLeft
                var y = e.pageY - h - box2.offsetParent.offsetTop
//box2.offsetParent.offsetLeft,box2.offsetParent.offsetTop
//求的是box1的相对位置

                // 判断边界值
                //让box2不能脱离box1边界
                if (x < 50) {
                    x = 0
                }else if(x >box2.offsetParent.offsetWidth -  box2.offsetWidth){
                //box1的宽度-box2的宽度,剩下的宽度就是边界
                //box2.offsetParent.offsetWidth=box1.offsetWidth
                       x = box2.offsetParent.offsetWidth -  box2.offsetWidth
               //如果触碰到边界,那么就让box2的左距离等于box1的宽度-box2的宽度
                }
                if (y < 50) {
                    y = 0
                }else if(y>box2.offsetParent.offsetHeight - box2.offsetHeight){
                      y  =  box2.offsetParent.offsetHeight - box2.offsetHeight
                }

                box2.style.left = x + 'px'
                box2.style.top = y + 'px'
            }
        }
        
		// 绑定抬起事件
		//鼠标抬起时,鼠标移动事件为空取消
      function handleUp() {
            document.onmouseup = function () {
                document.onmousemove = null
            }
        }
    </script>

在这里插入图片描述
边界值
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值