• 鼠标拖拽


    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    <style>
    #div1 {
    200px;
    height: 200px;
    background: red;
    position: absolute;
    }
    </style>
    <script>
    window.onload = function () {
    var oDiv = document.getElementById('div1');
    var disX = 0;
    var disY = 0;
    oDiv.onmousedown = function (ev) {
    var oEvent = ev || event;
    disX = oEvent.clientX - oDiv.offsetLeft;
    disY = oEvent.clientY - oDiv.offsetTop;
    document.onmousemove = function (ev) {
    var oEvent = ev || event;
    var l = oEvent.clientX - disX;
    var t = oEvent.clientY - disY;
    if (l < 0) {
    l = 0;
    }
    else if (l > document.documentElement.clientWidth - oDiv.offsetWidth) {//可视区的宽度——div宽度
    l = document.documentElement.clientWidth - oDiv.offsetWidth;
    }
    if (t < 0) {
    t = 0;
    }
    else if (t > document.documentElement.clientHeight - oDiv.offsetHeight) {
    t = document.documentElement.clientHeight - oDiv.offsetHeight;
    }
    oDiv.style.left = l + 'px';
    oDiv.style.top = t + 'px';
    };
    document.onmouseup = function () {
    document.onmousemove = null;
    document.onmouseup = null;
    };
    return false;
    };
    };
    </script>
    </head>

    <body>
    <div id="div1"></div>
    </body>
    </html>

    div拖拽带框:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    <style>
    #div1 {
    100px;
    height: 100px;
    background: yellow;
    position: absolute;
    }

    .box {
    border: 1px dashed black;
    position: absolute;
    }
    </style>
    <script>
    window.onload = function () {
    var oDiv = document.getElementById('div1');

    var disX = 0;
    var disY = 0;

    oDiv.onmousedown = function (ev) {
    var oEvent = ev || event;

    disX = oEvent.clientX - oDiv.offsetLeft;
    disY = oEvent.clientY - oDiv.offsetTop;

    var oBox = document.createElement('div');

    oBox.className = 'box';
    oBox.style.width = oDiv.offsetWidth - 2 + 'px';
    oBox.style.height = oDiv.offsetHeight - 2 + 'px';

    oBox.style.left = oDiv.offsetLeft + 'px';
    oBox.style.top = oDiv.offsetTop + 'px';

    document.body.appendChild(oBox);

    document.onmousemove = function (ev) {
    var oEvent = ev || event;
    var l = oEvent.clientX - disX;
    var t = oEvent.clientY - disY;

    oBox.style.left = l + 'px';
    oBox.style.top = t + 'px';
    };

    document.onmouseup = function () {
    document.onmousemove = null;
    document.onmouseup = null;

    oDiv.style.left = oBox.offsetLeft + 'px';
    oDiv.style.top = oBox.offsetTop + 'px';

    document.body.removeChild(oBox);
    };

    return false; //chrome、ff、IE9
    };
    };
    </script>
    </head>

    <body>
    <div id="div1"></div>
    </body>
    </html>
    div拖拽吸附:

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="utf-8">
    <title>无标题文档</title>
    <style>
    #div1 {
    100px;
    height: 100px;
    background: red;
    position: absolute;
    }

    #div2 {
    700px;
    height: 500px;
    background: #CCC;
    position: relative;
    }
    </style>
    <script>
    window.onload = function () {
    var oDiv = document.getElementById('div1');
    var oDiv2 = document.getElementById('div2');

    var disX = 0;
    var disY = 0;

    oDiv.onmousedown = function (ev) {
    var oEvent = ev || event;

    disX = oEvent.clientX - oDiv.offsetLeft;
    disY = oEvent.clientY - oDiv.offsetTop;

    document.onmousemove = function (ev) {
    var oEvent = ev || event;
    var l = oEvent.clientX - disX;
    var t = oEvent.clientY - disY;

    if (l < 50) {
    l = 0;
    } else if (l > 550) {
    l = 600;
    }
    else if (l > oDiv2.offsetWidth - oDiv.offsetWidth) {
    l = oDiv2.offsetWidth - oDiv.offsetWidth;
    }

    if (t < 50) {
    t = 0;
    } else if (t > 350) {
    t = 400;
    }
    else if (t > oDiv2.offsetHeight - oDiv.offsetHeight) {
    t = oDiv2.offsetHeight - oDiv.offsetHeight;
    }

    oDiv.style.left = l + 'px';
    oDiv.style.top = t + 'px';
    };

    document.onmouseup = function () {
    document.onmousemove = null;
    document.onmouseup = null;
    };

    return false;
    };
    };
    </script>
    </head>

    <body>
    <div id="div2">
    <div id="div1"></div>
    </div>
    </body>
    </html>
     
  • 相关阅读:
    python加载csv数据
    Android项目依赖库管理方式简介
    Android PhotoView基本功能实现
    Android ListView的header footer设置visibility gone不起作用
    [干货][EMIT]千行代码实现代理式AOP+属性的自动装配
    Emit实现DataRow转化成强类型的T
    有关docker新版的icc、iptables的一个巨坑
    Mac神器Iterm2的Shell Integration的用法和注意事项
    生成ssl证书文件
    python3 module中__init__.py的需要注意的地方
  • 原文地址:https://www.cnblogs.com/theWayToAce/p/5305406.html
Copyright © 2020-2023  润新知