zoukankan      html  css  js  c++  java
  • 拖拽限制范围

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <title>Document</title>
        <style>
            #box{width: 200px; height: 200px; background-color: #ccc;position: absolute;
             left: 400px; top: 200px;}
        </style>
    </head>
    <body>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <p>asdfasdf</p>
        <div id="box"></div>
        <script>
        var oDiv = document.getElementById('box');
    
        oDiv.onmousedown = function(ev){
    
            var oEvent = ev || event;
            //xy - 算鼠标距离元素的位置
            var x = oEvent.clientX - oDiv.getBoundingClientRect().left,
                y = oEvent.clientY - oDiv.getBoundingClientRect().top;
    
            document.onmousemove = function(ev){
                var oEvent = ev || event;
                //物体要移动的位置
                var l = oEvent.clientX - x,
                    t = oEvent.clientY - y;
    
                // console.log(l);
                if(l<=0) l=0;
                if(t<=0) t=0;
                if(l>=document.documentElement.clientWidth - oDiv.offsetWidth) l=document.documentElement.clientWidth - oDiv.offsetWidth;
                if(t>=document.documentElement.clientHeight - oDiv.offsetHeight) t=document.documentElement.clientHeight - oDiv.offsetHeight;
    
    
    
                oDiv.style.left = l + 'px';
                oDiv.style.top = t + 'px';
            }
            oDiv.onmouseup = function(){
                if(oDiv.releaseCapture) oDiv.releaseCapture();
                document.onmousemove = null;
            }
    
            if(oDiv.setCapture) oDiv.setCapture();
    
            return false;
    
        }
    
        </script>
    </body>
    </html>
  • 相关阅读:
    学习记录---KMP算法-部分匹配表理解
    关于GameObject无法禁用问题
    out用法
    关于Dictionary.TryGetValue的个人理解记录
    Transform.parent和Transform.root的区别
    Queue默认容量
    关于Camera Culling Mask
    MSVCP110.DLL没有被指定在WINDOWS上运行
    typeof instanceof 之间的区别总结
    Promise 使用心得
  • 原文地址:https://www.cnblogs.com/jinsuo/p/6846890.html
Copyright © 2011-2022 走看看