js拖拽

拖拽

<!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>
        .box{width:100px;height:100px;background:red;position: absolute;left:0;top:0;}

    </style>
</head>
<body>
    <div class="box">hello</div>

    
<script>

    var obox = document.querySelector(".box");
    var clientW = document.documentElement.clientWidth;    // 页面可视区域
    var clientH = document.documentElement.clientHeight;
    var boxW = obox.offsetWidth;        //border+padding+width
    var boxH = obox.offsetHeight;

    obox.onmousedown = function(eve){
        // 按下的一瞬间获取事件对象,准备获取按下时相对于元素的位置
        var downE = eve || window.event;
        document.onmousemove = function(eve){
            var moveE = eve || window.event;
            // 通过计算,得出要设置给元素的left和top
            var l = moveE.pageX - downE.offsetX;
            var t = moveE.pageY - downE.offsetY;
            // 边界限定
            if(l<0) l=0;
            if(t<0) t=0;
            if(l > clientW - boxW){
                l = clientW - boxW;
            }
            if(t > clientH - boxH){
                t = clientH - boxH;
            }
            // 设置给元素
            obox.style.left = l + "px";
            obox.style.top = t + "px";
        }
        document.onmouseup = function(){
            // 删除移动事件,元素不跟随鼠标
            document.onmousemove = null;
            // 删除抬起事件,为了节省性能
            document.onmouseup = null;
        }

        return false;
    }

</script>
</body>
</html>

面向对象拖拽

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box{width: 100px;height: 100px;background: red;position: absolute;left:0;top:0;}
    </style>
</head>
<body>
    <div class="box"></div>
</body>
<script>

    class Drag{
        constructor(){
            this.ele = document.querySelector(".box");

            this.m = this.move.bind(this);
            this.u = this.up.bind(this);

            this.addEvent();
        }
        addEvent(){
            this.ele.addEventListener("mousedown", this.down.bind(this));
        }
        down(eve){
            this.downE = eve || window.event;
            document.addEventListener("mousemove",this.m);
            document.addEventListener("mouseup",this.u);
        }
        move(eve){
            this.moveE = eve || window.event;
            this.ele.style.left = this.moveE.clientX - this.downE.offsetX + "px";
            this.ele.style.top = this.moveE.clientY - this.downE.offsetY + "px";

            // 将位置存储到storage技术内
            var pos = {
                x:this.ele.offsetLeft,
                y:this.ele.offsetTop
            }
            localStorage.setItem("pos",JSON.stringify(pos));
        }
        up(){
            document.removeEventListener("mousemove", this.m);
            document.removeEventListener("mouseup", this.u);
        }
    }

    new Drag;

</script>
</html>

jQuery做的拖拽

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

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="box"></div>
</body>
// 注意需要引入jquery
<script src="../jquery.js"></script>
<script>
    $(".box").mousedown(function(event) {
        var downE = event;
        // console.log(downE)
        $(document).on("mousemove.move", function(event) {
            var moveE = event;
            // console.log(moveE)
            var l = moveE.pageX - downE.offsetX;
            var t = moveE.pageY - downE.offsetY;

            var clientW = $(document).width(); // 页面可视区域
            var clientH = $(document).height();
            var boxW = $(".box").outerWidth(); //border+padding+width
            var boxH = $(".box").outerHeight();

            // 边界限定
            if (l < 0) l = 0;
            if (t < 0) t = 0;

            if (l > clientW - boxW) {
                l = clientW - boxW;
            }
            if (t > clientH - boxH) {
                t = clientH - boxH;
            }
            $(".box").css({
                left: l,
                top: t
            })
        })
        $(document).on("mouseup.up", function() {
            $(document).off("mousemove.move")
            $(document).off("mouseup.up")
        })
    })
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值