zoukankan      html  css  js  c++  java
  • 拖拽克隆。

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    body {
    margin: 0;
    }
    #wrap {
    margin: 50px auto;
    position: relative;
    border: 1px solid #000;
    600px;
    height: 500px;
    }
    #div {
    position: absolute;
    left: 0;
    top: 0;
    100px;
    height: 100px;
    background: red;
    cursor: move;
    }
    </style>
    </head>
    <body>
    <div id="wrap">
    <div id="div">文字</div>
    </div>
    <script type="text/javascript">
    (function(){
    var div = document.querySelector('#div');
    var maxX = div.offsetParent.clientWidth - div.offsetWidth;
    var maxY = div.offsetParent.clientHeight - div.offsetHeight;
    div.addEventListener('mousedown', function(e) {
    e.preventDefault();
    var start = {x: e.clientX,y: e.clientY};
    var elOffset = {x: div.offsetLeft,y: div.offsetTop};
    var newDiv = div.cloneNode(true);
    newDiv.style.opacity = .5;
    div.parentNode.appendChild(newDiv);
    document.addEventListener('mousemove', move);
    document.addEventListener('mouseup', end);
    function move(e){
    var dis = {x:e.clientX - start.x,y:e.clientY - start.y};
    var x = dis.x + elOffset.x;
    var y = dis.y + elOffset.y;
    /* 左侧磁性吸附 */
    x = x<0?0:(x>maxX?maxX:x);

    y = y<0?0:(y>maxY?maxY:y);
    console.log(x);
    newDiv.style.left = x + "px";
    newDiv.style.top = y + "px";
    }
    function end(e){
    document.removeEventListener('mousemove', move);
    document.removeEventListener('mouseup', end);
    div.style.left = newDiv.style.left;
    div.style.top = newDiv.style.top;
    div.parentNode.removeChild(newDiv);
    }
    });
    })();
    </script>
    </body>
    </html>

  • 相关阅读:
    小程序左滑删除之<movable-area/>实现
    小程序省市区县分割
    小程序自定义底部按钮适配Iphone X
    小程序处理图片加载失败的问题
    Notepad++ 使用技巧
    IDEA 在打包项目时遇到的ERROR
    Markdown学习
    Ubuntu操作系统(文件传输)
    数据 恢复----判断Raid盘序及校验方向
    数据恢复----重组raid5解析
  • 原文地址:https://www.cnblogs.com/catEatBird/p/7068207.html
Copyright © 2011-2022 走看看