Javascript方法实现拖曳和用HTML实现拖曳

Javascript方法实现拖曳和用HTML实现拖曳

1.js实现拖曳

<!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>拖曳</title>
    <style>
        #box {
            width: 100px;
            height: 100px;
            background-color: pink;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div id="box">

    </div>
    <script>
        let box = document.querySelector('#box');

        //鼠标按下
        box.onmousedown = function (e) {
            //鼠标按下的位置
            let x = e.x - parseInt(box.style.left || 0);
            let y = e.y - parseInt(box.style.top || 0);
            //鼠标移动
            document.onmousemove = function (e) {
                //鼠标移动的距离减去按下的位置
                let left = e.x - x;
                let top = e.y - y;
                box.style.left = left + "px";
                box.style.top = top + "px";
                console.log(e.x, e.y);
            }
            //鼠标离开
            document.onmouseup = function () {
                document.onmousemove = null;
                document.onmouseup = null;
            }
        }

    </script>
</body>

</html>

2.html实现拖曳

<!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>拖曳</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background-color: aquamarine;
            position: absolute;
            left: 0;
            top: 0;
        }
       html,body{
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>
    <div class="box" draggable="true"></div>
    <script>
        let box = document.querySelector(".box");
        let x=0;
        let y=0;
        //ondragstart:开始移动
        box.ondragstart = function(e){
            x = e.x - parseInt(box.style.left || 0);
            y = e.y - parseInt(box.style.top || 0);
            console.log(e.x);
            console.log(x,y);

        }
        //想要是drop实现效果,必须将投放在对象上的dragover事件的默认行为阻止才能起作用
        document.body.ondragover = function(e){
            e.preventDefault();
        }
        //落下
       //只可视页面移动:document.body.ondrop = function (e)
        //滚动页面也可移动:box.ondragend = function (e)
        box.ondragend = function (e) {
            console.log("drop");
            let left = e.x - x;
            let top = e.y - y;
            box.style.left = left + "px";
            box.style.top = top + "px";
            console.log(box.style.left, box.style.top);
        }
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值