zoukankan      html  css  js  c++  java
  • 只能在方块内拖拽的案例

    <!DOCTYPE html>
    <html>
      <head>
      <meta charset="UTF-8">
      <title></title>
    <style>

      #aa{
        500px;
        height:500px;
        border:red 1px solid;
        margin:100px auto;
        position:relative;
        /*padding:2px;*/
        }
      #dd{
        100px;
        height:100px;
        background:green;
        /*margin:2px;*/
        position:absolute;
      }
      </style>
    </head>
    <body>
      <div id="aa">
        <div id="dd"></div>
      </div>
    </body>
    </html>
    <script>
      var $=function(id){
      return document.getElementById(id)
      }
      var aa=$("aa");
      var dd=$("dd");
      dd.onmousedown=function(ev){
        var ev=ev||window.event;
        var left=aa.offsetLeft
        var top=aa.offsetTop
        var x=ev.clientX-left-dd.offsetLeft;
        var y=ev.clientY-top-dd.offsetTop;
        document.onmousemove=function(ev){
          var ev=ev||window.event;

          dd.style.left=ev.clientX-left-x+"px";
          dd.style.top=ev.clientY-top-y+"px"
        if(ev.clientX-left-x<=0){
          dd.style.left=0+"px";
        }
        if(ev.clientY-top-y<=0){
          dd.style.top=0+"px";
        }

        if(ev.clientX-left-x>=aa.offsetWidth-dd.offsetWidth){
          dd.style.left=aa.offsetWidth-dd.offsetWidth+"px";
        }
        if(ev.clientY-top-y>=aa.offsetHeight-dd.offsetHeight){
          dd.style.top=aa.offsetHeight-dd.offsetHeight+"px";
        }



      }
      return false;
      }
      document.onmouseup=function(){
      document.onmousemove=null;
      }
    </script>

  • 相关阅读:
    Android 开发技术周报 Issue#276
    手动添加 Git bash 到鼠标右键
    Sublime Text3 3143 注册码
    MySQL主从复制
    Python-MongoDB的驱动安装、升级
    MongoDB数据库的安装、配置和使用
    js循环生成多个easyui datagrid数据网格时,初始化表格
    屏蔽掉Google Chrome 浏览器 textarea 单词拼写检测
    Fiddler屏蔽某些url的抓取方法
    解决VMware下安装Ubuntu 16.04 不支持1920X1080分辨率的问题
  • 原文地址:https://www.cnblogs.com/shangjun6/p/10375234.html
Copyright © 2011-2022 走看看