zoukankan      html  css  js  c++  java
  • vue中拖拽API的用法

    1、通过鼠标按下触发移动事件

    //简写如下
    <body>
        <div id="app" @mousedown="move">       <!--绑定按下事件-->
        </div>
    </body>
    var app = new Vue({
        el:'#app',
        data:{
            positionX:0,
            positionY:0,
        },
        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';
                };
                document.onmouseup = (e) => {
                    document.onmousemove = null;
                    document.onmouseup = null;
                };
            }    
        },
    });
    

    2、通过自定义指令

    //简写如下:
    <body>
        <div id="app" v-drag>       <!--实现用指令形式实现拖拽效果-->       
        </div>
    </body>
    var app = new Vue({
        el:'#app',
        data:{},
        methods:{},
        directives: {
            drag: {
                // 指令的定义
                bind: function (el) {
                    let odiv = el;   //获取当前元素
                    odiv.onmousedown = (e) => {
                        //算出鼠标相对元素的位置
                        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';
                        };
                        document.onmouseup = (e) => {
                            document.onmousemove = null;
                            document.onmouseup = null;
                        };
                    };
                }
            }
        }
    });
    
  • 相关阅读:
    MySQL存储写入性能严重抖动分析
    关于MySQL的commit非规律性失败案例的深入分析
    MySQL存储写入速度慢分析
    MySQL缓存之Qcache与buffer pool对比
    SQL执行过程中的性能负载点
    关于MySQL用户会话及连接线程
    如何查询、修改参数状态值
    genymotion 前端调试
    name是个特殊的变量名吗
    background-size 导致的背景不居中问题
  • 原文地址:https://www.cnblogs.com/wangchangli/p/11386441.html
Copyright © 2011-2022 走看看