• vue自定义指令:v-drag使用 拖动拖拽


    <template>
        <div class="drag">
           <div ref="element" class="content" v-drag draggable="false">
              <p>文字网页</p>
           </div>
           <div style="height:2000px;100%"></div>
        </div>
    </template>
    <script >
       export default {
            data(){
                return {
                    dd:"",
                    inout:""
                }
            },
            directives: {
                drag(el){
                    let oDiv = el; //当前元素
                    let self = this; //上下文
                    //禁止选择网页上的文字
                    document.onselectstart = function() {
                        return false;
                    };
                    oDiv.onmousedown = function(e){
                        //鼠标按下,计算当前元素距离可视区的距离
                        let disX = e.clientX - oDiv.offsetLeft;
                        let disY = e.clientY - oDiv.offsetTop;
                        document.onmousemove = function(e){
                            //通过事件委托,计算移动的距离
                            let l = e.clientX - disX;
                            let t = e.clientY - disY;
                            //移动当前元素
                            oDiv.style.left = l + "px";
                            oDiv.style.top = t + "px";
                        }
                        document.onmouseup = function(e){
                            document.onmousemove = null;
                            document.onmouseup = null;
                        };
                        //return false不加的话可能导致黏连,就是拖到一个地方时div粘在鼠标上不下来,相当于onmouseup失效
                        return false;
                    };
                }
            }
       }
    </script>
    <style>
    .drag{
        position: relative;
    }
    /* position:absolute;这个还是要设的,不然拖动块定不了位置 */
    .content{
        position:absolute;
        height:100px;
        100px;
        background-color: #67C23A;
        cursor: pointer;
    }
    </style>
  • 相关阅读:
    JS,Jquery获取各种屏幕的宽度和高度
    mysql存储html代码之导出后无法导入问题
    php之简单socket编程
    php单点登录SSO(Single Sign On)的解决思路
    php读取邮件
    YII框架的依赖注入容器
    YII框架的行为
    YII框架的事件机制
    YII框架的模块化技术
    mysql的索引
  • 原文地址:https://www.cnblogs.com/shiyiersan/p/13917726.html
Copyright © 2020-2023  润新知