zoukankan      html  css  js  c++  java
  • 关于移动div的具体实现(js)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>移动框练习</title>
        <style type = "text/css">
            #big{width: 300px;height: 200px;position: absolute;top: 200px;left: 300px;background: #ccc;}
            h1{width: 200px;height: 50px;line-height: 50px;text-align: center;background: orange;margin: 0 auto; cursor: move;}
        </style>
    </head>
    <body>
        <div id="big">
            <h1 class="titl"></h1>
        </div>
        <script type="text/javascript">
            //通过父元素获取到指定想要获取的元素。
            function getClass(clsName,parent){
                var oParent = parent?document.getElementById(parent):document;
                var oH1 = oParent.getElementsByClassName(clsName)[0];
                return oH1;
            }
            window.onload = drag;
            //给h1添加按住的事件。
            function drag(){
                var h1Node = getClass("titl","big");
                h1Node.onmousedown = fnDown;
            }
            //通过函数来获取指针的位置。
            function fnDown(event) {
                event = event || window.event;
                var oDiv = document.getElementById("big"),
                    //光标按下时光标和面板之间的距离。
                    disX = event.clientX-oDiv.offsetLeft;
                    disY = event.clientY-oDiv.offsetTop;
                document.onmousemove = function(event) {
                    event = event || window.event;
                    fnMove(event,disX,disY);
                }
                document.onmouseup = function(){
                    document.onmousemove = null;
                }
            }
            function fnMove(e,posX,posY) {
                var oDiv = document.getElementById("big"),
                    l = e.clientX-posX,
                    t = e.clientY-posY,
                    winW = document.documentElement.clientWidth || document.body.clientWidth,
                    winH = document.documentElement.clientHeight || document.body.clientHeight,
                    maxW = winW-oDiv.offsetWidth,
                    maxH = winH-oDiv.offsetHeight;
                if (l<0) {
                    l=10;
                } else if(l>maxW){
                    l=maxW;
                }
                if (t<0) {
                    t=10;
                } else if(t>maxH){
                    t=maxH;
                }
                oDiv.style.left = l+"px";
                oDiv.style.top = t+"px";
            }
        </script>
    </body>
    </html>
  • 相关阅读:
    golang 相关
    ES root用户启动失败can not run elasticsearch as root
    基于 Flink CDC + Hudi 湖仓一体方案实践
    数据平台上云
    多云趋势
    数果实时数仓探索
    宽表的设计
    数仓指标体系
    Hudi在医疗大数据的应用
    Hudi on Flink上手使用总结
  • 原文地址:https://www.cnblogs.com/Arther-J/p/5396549.html
Copyright © 2011-2022 走看看