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>
  • 相关阅读:
    hdu 5015
    hdu 2276
    hdu 6197 array array array LIS
    poj 3070
    codefroce 854 A.Fraction
    sql数据库发布、订阅同步方式操作
    python排序(冒泡、直接选择、直接插入等)
    忘记mysql数据库密码时进行修改方法
    Word中图片自动编号且与文中引用的编号对应
    解决电脑本地网络连接显示红叉又可上网问题
  • 原文地址:https://www.cnblogs.com/jinsuo/p/6846890.html
Copyright © 2011-2022 走看看