zoukankan      html  css  js  c++  java
  • JS实现拖拽小案例

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>简单的拖拽</title>
        <link rel="stylesheet" href="../toolkit/reset.min.css">
        <style>
            #box{
                height: 200px;
                width: 200px;
                background-color: #e277ff;
                position: absolute;
                cursor: move;
            }
        </style>
    </head>
    <body>
    <div id="box"></div>
    <script>
        var box=document.getElementById("box");
        function drag(e) {
            e=e||window.event;
            var _this=this;
            var mouseX=e.clientX,
                    mouseY=e.clientY,
                    boxL=this.offsetLeft,
                    boxT=this.offsetTop;
            document.onmousemove=function (e) {
                e=e||window.event;
                var curMouseX=e.clientX,
                        curMouseY=e.clientY,
                        curBoxL=curMouseX-mouseX+boxL,
                        curBoxT=curMouseY-mouseY+boxT;
    
                var minW=0,maxW=((document.documentElement.clientWidth||document.body.clientWidth)-_this.offsetWidth);
                var minH=0,maxH=((document.documentElement.clientHeight||document.body.clientHeight)-_this.offsetHeight);
                if(curBoxL<=minW){
                    curBoxL=minW;
                }else if(curBoxL>=maxW){
                    curBoxL=maxW
                }
                if(curBoxT<=minH){
                    curBoxT=minH;
                }else if(curBoxT>=maxH){
                    curBoxT=maxH;
                }
    
                _this.style.left=curBoxL+"px";
                _this.style.top=curBoxT+"px";
            };
            document.onmouseup=function () {
                document.onmousemove=null;
            }
        }
        box.onmousedown=drag;
    </script>
    </body>
    </html>
  • 相关阅读:
    python_接口基础知识
    python_基础总结
    python_配置文件_yaml
    python_loggin日志处理
    python_数据驱动_ddt
    python_unittest_单元测试_openpyxl
    python_类与对象总结_继承
    python_路径操作及类和对象
    python_导包
    Codeforces Round #655 (Div. 2) B. Omkar and Last Class of Math
  • 原文地址:https://www.cnblogs.com/Scar007/p/7911137.html
Copyright © 2011-2022 走看看