zoukankan      html  css  js  c++  java
  • 完美拖拽(点击回放运动轨迹)

    <!DOCTYPE html>

    <html>
    <head>
        <meta charset="utf-8">
        <title>完美拖拽</title>
        <style type="text/css">
            html,body{overflow:hidden;}
            body,div,h2,p{margin:0;padding:0;}
            body{color:#fff;background:#000;font:12px/2 Arial;}
            p{padding:0 10px;margin-top:10px;}
            span{color:#ff0;padding-left:5px;}
            #box{position:absolute;300px;height:150px;background:#333;border:2px solid #ccc;top:50%;left:50%;margin:-75px 0 0 -150px;}
            #box h2{height:25px;cursor:move;background:#222;border-bottom:2px solid #ccc;text-align:right;padding:0 10px;}
            #box h2 a{color:#fff;font:12px/25px Arial;text-decoration:none;outline:none;}
        </style>
    </head>
    <body>
        <div id="box" style="margin-left: 0px; margin-top: 0px; left: 533px; top: 231px;">
         <h2><a href="javascript:;" id="a1">点击回放拖动轨迹</a></h2>
         <p><strong>Drag:</strong><span>false</span></p>
         <p><strong>offsetTop:</strong><span>231</span></p>
         <p><strong>offsetLeft:</strong><span>533</span></p>
        </div>
    </body>
    </html>
    <script>
        window.onload = function(){
         var oDiv = document.querySelector("#box");
         var oA1 = document.getElementById("a1");
         var arr = [];
        
            oDiv.onmousedown = function(e){
                var e = e || event;
                var disx = e.offsetX;
                var disy = e.offsetY;
                document.onmousemove = function(e){
                    var e = e || event;
                    var x = e.pageX - disx;
                    var y = e.pageY - disy;
                    var maxL = window.innerWidth - 300;//最大的left
                    var maxT = window.innerHeight- 150;//最大的top
                    if( x<0 ){ //左边界
                        x = 0;
                    }else if( x > maxL ){ //右边界
                        x = maxL;
                    }
                    if( y<0 ){ //上边界
                        y = 0;
                    }else if( y > maxT ){ // 下边界
                        y = maxT;
                    }
                    oDiv.style.left = x + "px";
                    oDiv.style.top = y + "px";
                    var pos = {
                        aleft : x,
                        atop : y
                    }
                    arr.unshift( pos );
                    
                }   
                document.onmouseup = function(){
                    document.onmousemove = null;
                    oA1.onclick = function(){
                        var num=0;
                        var timer = null;                                       
                        timer = setInterval(function(){
                            oDiv.style.left = arr[num].aleft + "px";
                            oDiv.style.top = arr[num].atop + "px";
                            num++;
                            if(num == arr.length){
                                clearInterval(timer);
                                arr = [];
                                return;
                            }   
                        },100); 
                    };
                }   
            }   
        }
    </script>
  • 相关阅读:
    MyBatis执行sql的整个流程
    Ftp传输:向linux服务器上传文件时“550 Permission denied.”错误问题解决
    SpringBoot框架:两个方法同时调用时父方法使内部方法的DataSource注解失效的解决办法
    SpringBoot框架:通过AOP和自定义注解完成druid连接池的动态数据源切换(三)
    SpringBoot框架:配置文件application.properties和application.yml的区别
    SpringBoot框架:'url' attribute is not specified and no embedded datasource could be configured问题处理
    bash脚本打印字符串一个空格的内容
    gethostbyname的线程安全
    算法工程师的职业规划
    理解Deep Link & URI Schemes & Universal Link & App Link
  • 原文地址:https://www.cnblogs.com/tis100204/p/10319216.html
Copyright © 2011-2022 走看看