基于Vue实现拖拽效果及阻止拖拽

摘要:
总体代码如下:˂!

参考地址:基于Vue实现拖拽效果

参考链接中讲的比较详细,我只使用了其中自定义指令的方法。整体代码如下:

<template>
    <!-- 卡片 -->
    <div   v-drag id="card">

    </div>
</template>

<script>
export default {
    data() {
        return {
        }
    },
    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;

                        oDiv.style.left = left + 'px';
                        oDiv.style.top = top + 'px';
                    };

                    document.onmouseup = (e) => {
                        document.onmousemove = null;
                        document.onmouseup = null;
                    }
                }
            }
        }
    }
}
</script>

<style lang="scss">
    .card {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
         870px;
        height: 510px;
        background-color: #3ab5a0;
    }
</style>

使用方法:在需拖拽功能的元素上添加v-drag启用: 

基于Vue实现拖拽效果及阻止拖拽第1张

补充:阻止拖拽

上述方法利用自定义指令实现了弹窗的拖拽,补充部分是阻止拖拽,例如:弹窗中有input框,如果想要选中input中的内容就需要阻止弹窗的拖拽

参考地址:vue实现弹窗拖拽

export default {
    directives: {
        /*自定义拖拽*/
        drag: {
            inserted: function(el, binding, vnode) {
                var odiv = el.parentNode;
                odiv.onmousedown = function(eve) {
                    eve = eve || window.event;
                    var clientX = eve.clientX;
                    var clientY = eve.clientY;
                    var odivX = odiv.offsetLeft;
                    var odivY = odiv.offsetTop;
                    var odivLeft = clientX - odivX;
                    var odivTop = clientY - odivY;
                    var clientWidth = document.documentElement.clientWidth;
                    var oWidth = odiv.clientWidth;
                    var odivRight = clientWidth - oWidth;
                    var clientHeight = document.documentElement.clientHeight;
                    var oHeight = odiv.clientHeight;
                    var odivBottom = clientHeight - oHeight;
                    document.onmousemove = function(e) {
                        e.preventDefault();
                        var left = e.clientX - odivLeft;
                        if (left < 0) {
                            left = 0
                        }
                        if (left > odivRight) {
                            left = odivRight
                        }
                        var Top = e.clientY - odivTop;
                        if (Top < 0) {
                            Top = 0
                        }
                        if (Top > odivBottom) {
                            Top = odivBottom
                        }
                        odiv.style.left = left + "px";
                        odiv.style.top = Top + "px";
                    }
                    document.onmouseup = function() {
                        document.onmouseup = "";
                        document.onmousemove = "";
                    }
                }
            }
        },
        /*阻止拖拽*/
        stopdrag: {
            inserted: function(el, binding, vnode) {
                let element = el;
                element.onmousedown = function(e) {
                    e.stopPropagation()
                }
            }
        }
    }
}

 使用方法:在不需拖拽的元素上添加v-stopdrag阻止:

基于Vue实现拖拽效果及阻止拖拽第2张

 基于Vue实现拖拽效果及阻止拖拽第3张

 

免责声明:文章转载自《基于Vue实现拖拽效果及阻止拖拽》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SQL Server 事务、异常和游标vue单页面项目返回上一页无效,链接变化了,但是页面没有变化下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Vue搭建脚手架2

Vue2.0搭建Vue脚手架(vue-cli) 在网上找了很多的搭建脚手架教程,但都不求甚解。终于找到2个比较好的教程,读者可对比阅读1和2,在这里分享给大家,希望对初学者有所帮助。ps:高手请绕道。 1.使用npm全局安装vue-cli(前提是你已经安装了nodejs,否则你连npm都用不了),在cmd中输入一下命令 npm install --glo...

VueJS/Vuex/vue-router笔记- 开发/Error错误处理及优化相关记录

 开发记录备查笔记.....  Q.Vuejs(2.6.x):TS下使用Vuex例子:   记一个ts下的vuex store,备查   可以用以前的ES写法,但是想用强类型约束的话,就得改成TS的写法.   (吐槽:vue虽然已经全部用TS重构了,但还是有大量的any变量,希望随着以后的迭代,能完善成更出色的泛型类吧,现在的vuex真是不太好用,还不如自...

vue 公用组件开发 确认框confirm

文件目录: github地址:https://github.com/xingkongwuyu/vue-spa-experience/tree/master/src/components 最终的效果:  组件的源码解析: confirm :  confirm的框架 ./index.js import confirmBox from './src/inde...

vue安装及升级

先装好note.js 安装过程很简单,一直点下一步就ok了。1.1我们通过打开命令行工具(win+R),输入node -v查看node的版本,若出现相应的版本号说明你安装成功了  1.2.npm包管理器,是集成在node中的,所以安装了node也就有了npm,直接输入 npm -v 命令,显示npm的版本信息 现在node环境已经安装完成了,npm包管...

VUE JS 闹钟函数

心跳函数:秒表 setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。 setInterval() 方法会不停地调用函数,直到clearInterval()被调用或窗口被关闭。 由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数。 clearInterval() 方法可取消由 s...

Vue 技能进阶:使用设计模式写出优雅的前端代码

为什么提出这个复杂的问题? 在我们的应用程序中有一个顶栏,其中包含各种按钮、一个搜索栏和其他一些控件。 它显示的内容根据你所在的页面略有差异,因此我们需要一种按页配置它的方法。 为此,我们希望每个页面都能配置顶栏。 看起来很简单,但这里有一个问题:这个顶栏(我们称之为 ActionBar)实际上是主布局骨架的一部分,它长成这样: <templat...