• 拖拽按钮 + 获取定位(源码)


    先看一下实现的效果:

    拖拽按钮

    ok,源码奉上:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>信息系统</title>
        <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
        <!-- <link rel='icon'  href='../img/找培训.png '  type=‘image/x-ico’  /> -->
     
        <style>
            #drag-test{
            position: absolute;     /*定位*/
            top: 10px;
            left: 10px;
            width: 200px;
            height: 30px;
            background: greenyellow;
            }
        </style>
     
        <script>
        //写Cookie
        function addCookie(objName, objValue, objHours) {
            var str = objName + "=" + escape(objValue); //编码
            if (objHours > 0) {//为0时不设定过期时间,浏览器关闭时cookie自动消失
                var date = new Date();
                var ms = objHours * 3600 * 1000;
                date.setTime(date.getTime() + ms);
                str += "; expires=" + date.toGMTString();
            }
            document.cookie = str;
        }
     
        //读Cookie
        function getCookie(objName) {//获取指定名称的cookie的值
            var arrStr = document.cookie.split("; ");
            for (var i = 0; i < arrStr.length; i++) {
                var temp = arrStr[i].split("=");
                if (temp[0] == objName) return unescape(temp[1]);  //解码
            }
            return "";
        }
        </script>
    </head>
    <body>
        <!-- vue事件详解 -->
        <button id="save-all" onclick="saveAll()">保存</button>
        <!-- 鼠标事件 -->
        <button id="drag-test"  @mousedown="move">
            {{message}}
            {{positionX}}
            {{positionY}}
        </button>
        <script>
            pX=getCookie("xV")
            pY=getCookie("yV")
     
            document.getElementById("drag-test").style.left=pX
            document.getElementById("drag-test").style.top=pY
     
           var b1= new Vue({
                el:"#drag-test",
                data:{
                    message:"点我即可拖拽",
                    positionX:pX,
                    positionY:pY
                },
                methods:{
                    move(e){
                let odiv = e.target;        //获取目标元素
                
                //算出鼠标相对元素的位置
                let disX = e.clientX - odiv.offsetLeft;
                let disY = e.clientY - odiv.offsetTop;
                document.onmousemove = (e)=>{       //鼠标按下并移动的事件
                    //用鼠标的位置减去鼠标相对元素的位置,得到元素的位置
                    let left = e.clientX - disX;    
                    let top = e.clientY - disY;
                    
                    //绑定元素位置到positionX和positionY上面
                    this.positionX = top;
                    this.positionY = left;
                    
     
                    //移动当前元素
                    odiv.style.left = left + 'px';
                    odiv.style.top = top + 'px';
     
                    
     
                    pX=odiv.style.left;
                    pY=odiv.style.top;
                };
                document.onmouseup = (e) => {
                    document.onmousemove = null;
                    document.onmouseup = null;
                };
                }
     
            } 
                
            })
     
     
     
            function saveAll(){
                addCookie("xV",pX,1)
                addCookie("yV",pY,1)
                alert("保存 成功"+pX+','+pY)
            }
            
     
        </script>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
  • 相关阅读:
    RocketMQ消息存储机制
    【TensorRT】Win10系统python/c++安装tensorrt库
    飞常准查航班小程序采集
    星戈瑞Sulfo-CY5 maleimide应用于流式细胞术
    网站分类seo怎么优化(如何调整有利于SEO排名)
    Web3中文|以太坊创始人V神:关注技术,而不是价格
    OceanBase 4.0 发布!看世界上最小的鱼如何游出一大步
    JavaScript知识点(根据例子来学习)
    我试图扯掉这条 SQL 的底裤。只能扯一点点,不能扯多了
    一文get到SOLID原则的重点
  • 原文地址:https://blog.csdn.net/weixin_55846296/article/details/118763560