zoukankan      html  css  js  c++  java
  • 使用Vue实现一个简单地自定义拖拽功能

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
        <style>
            *{margin:0;padding: 0}
            .box{
                 100px;
                height: 100px;
                background: red;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <div class="box" v-drag="flag"></div>
        </div>
        <div data-id="0"  id="box"></div>
    </body>
    </html>
    <script src="./vue.js"></script>
    <script>
        Vue.directive("drag",(el,{modifiers,value})=>{
            el.style.position = "absolute";
            let {x,y} = modifiers;
           
    
            el.addEventListener("mousedown",donw);
            
            var disX,disY;
            function donw(e){
                disX = e.offsetX;
                disY = e.offsetY;
                document.addEventListener("mousemove",move);
                document.addEventListener("mouseup",up)
    
    
            }
    
            function move(e){
               var l = e.clientX - disX;
               var t = e.clientY - disY;
    
    
                if(value){
    
                    if(x){
                        el.style.left = l + 'px';
                    }
                    if(y){
                        el.style.top = t + 'px';
                    }
                    
                    if(x&&y || (!x&&!y)){
                        el.style.left = l + 'px';
                        el.style.top = t + 'px';
                    }
                     
                }
               
            }
    
            function up(e){
                document.removeEventListener("mousemove",move);
                document.removeEventListener("mouseup",up);
            }
        })
    
    
    
        new Vue({
            el:"#app",
            data:{
                flag:true,
                msg:"123"
            }
        })
    </script>
  • 相关阅读:
    BZOJ1070[SCOI2007]修车
    BZOJ1061[Noi2008] 志愿者招募
    BZOJ 3511 土地划分
    BZOJ3130 [Sdoi2013]费用流
    POJ1797 Heavy Transportation
    P2866 糟糕的一天
    P1155 双栈排序
    P1027 car的旅行路线
    POJ3037 Skiing
    POJ1125 Stockbroker Grapevine
  • 原文地址:https://www.cnblogs.com/r-mp/p/11224156.html
Copyright © 2011-2022 走看看