实现完美拖拽

  1. 完美拖拽点击记录运动轨迹返回原位置
<!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>Document</title>
    <style>
        #box{
            width: 300px;
            height: 200px;
            background: #999;
            position: absolute;
            left: 0;
            top: 0;
        }
        .title{
            width: 100%;
            height: 20px;
            background: black;
            position: absolute;
            left: 0;
            top: 0;
        }
        .title span{
            width: 30px;
            height: 20px;
            background: red;
            color: yellow;
            position: absolute;
            top: 0;
            right: 0;
            font-size: 12px;
        }
        .msg{
            width: 100%;
            height: 180px;
            background: yellowgreen;
            position: absolute;
            left: 0;
            top: 20px;
            color: blue;
        }
    </style>
</head>
<body>
    <div id="box">
        <div class="title">
            <span>返回</span>
        </div>
        <div class="msg"></div>
    </div>
    <script>
        var o_box = document.querySelector('#box');
        var o_back = document.querySelector('.title span');

        var arr = []; //放置运动轨迹的数组
        
        o_box.onmousedown = function(evt){
            var e = evt || window.event;
            //获取事件源
            var target = e.target || e.srcElement;
            //过滤出标题栏
            if(target.className === 'title'){
                //鼠标的相对坐标值
                var dis_x = e.offsetX;
                var dis_y = e.offsetY;
                document.onmousemove = function(evt){
                    var e = evt || window.event;
                    //先求出left和top值
                    var left = e.pageX - dis_x;
                    var top = e.pageY - dis_y;
                    //设置边界
                    //左边界
                    if(left <= 0){
                        left = 0;
                    }else if(left >= document.documentElement.clientWidth - o_box.offsetWidth){ //右边界
                        left = document.documentElement.clientWidth - o_box.offsetWidth;
                    }
                    if(top <= 0){ //上边界
                        top = 0;
                    }else if(top >= document.documentElement.clientHeight - o_box.offsetHeight){ //下边界
                        top = document.documentElement.clientHeight - o_box.offsetHeight;
                    }
                    //设置盒子的位置
                    o_box.style.left = left + 'px';
                    o_box.style.top = top + 'px';

                    //记录轨迹
                    arr.push({left : o_box.offsetLeft,top : o_box.offsetTop});
                    
                    console.log(arr);
                }
                document.onmouseup = function(){
                    document.onmousemove = null;
                }
                document.ondragstart = function(){
                    return false;
                }
            }
        }
        o_back.onclick = function(){
            var index = arr.length - 1; //最大下标
            //利用计时器逆序遍历数组中的坐标值
            var timer = setInterval(function(){
                o_box.style.left = arr[index].left + 'px';
                o_box.style.top = arr[index --].top + 'px';
                if(index === -1){
                    clearInterval(timer);
                }
            }, 30);
        }
    </script>
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值