Vue 实现弹框自由拖拽(不出可视范围、解决快速拖动问题)

Vue 实现弹框自由拖拽(不出可视范围、解决快速拖动问题)

由于页面中弹框很多,往往会挡住想要查看的内容。从而,有了自由移动拖拽弹框的需求;但在使用的过程中发现,一开始编写的drag.js文件会移出可视范围,会影响项目的整体布局;并且在拖动过程中发现,快速拖动弹框时,可能会失效或者略微延后。

1.在项目中新建drag.js文件,文件中代码如下(直接复制使用)(以下代码已解决移出可视范围和快速拖动延后问题,并为拖动时设置对应的鼠标样式)
import Vue from 'vue';
//使用Vue.directive()定义一个全局指令
//1.参数一:指令的名称,定义时指令前面不需要写v-
//2.参数二:是一个对象,该对象中有相关的操作函数
//3.在调用的时候必须写v-
const drag = Vue.directive('drag', {
  //1.指令绑定到元素上回立刻执行bind函数,只执行一次
  //2.每个函数中第一个参数永远是el,表示绑定指令的元素,el参数是原生js对象
  //3.通过el.focus()是无法获取焦点的,因为只有插入DOM后才生效
  bind: function (el) {
    el.style.cursor = "move"; //鼠标样式变move样式
  },
  //inserted表示一个元素,插入到DOM中会执行inserted函数,只触发一次
  inserted: function (el) {
    el.onmousedown = function (e) {
      var distX = e.pageX - el.offsetLeft;
      var distY = e.pageY - el.offsetTop;
      console.log("元素本身的高:" + el.clientHeight + ",元素本身的宽:" + el.clientWidth)
      
      if (e.preventDefault) {
        e.preventDefault();
       } else{
        e.returnValue=false;
       };//解决快速拖动滞后问题

      document.onmousemove = function (e) {

        // 用鼠标的位置减去鼠标相对元素的位置,得到元素的位置
        let left = e.clientX - distX;
        let top = e.clientY - distY;

        if (left <= 0) {
          left = 5; //设置成5是为了不离边缘太近
        } else if (left > document.documentElement.clientWidth - el.clientWidth) {
          //document.documentElement.clientWidth 屏幕的可视宽度
          left = document.documentElement.clientWidth - el.clientWidth - 5
        }
        if (top <= 0) {
          top = 5;
        } else if (top > document.documentElement.clientHeight - el.clientHeight) {
          top = document.documentElement.clientHeight - el.clientHeight - 5
        }
        el.style.left = left + 'px';
        el.style.top = top + 'px';
      }
      document.onmouseup = function () {
        document.onmousemove = document.onmouseup = null;
      }
    }
  },
  //当VNode更新的时候会执行updated,可以触发多次
  updated: function (el) {}
})
export default drag;
在main.js 中引入import drag from “…/src/api/drag.js”

在需要拖拽的弹框中添加v-drag指令
在这里插入图片描述

实现效果(实现拖动)

快速拖动效果
在这里插入图片描述
参考博文: https://blog.csdn.net/hong521520/article/details/106804209
https://blog.csdn.net/a_tzluy/article/details/111030896
https://www.jb51.net/article/146240.htm

实现可视化可拖放的自定义表单可以使用Vue.js和一些第三方库来帮助我们完成。下面是一个简单的实现步骤: 1. 安装并引入vue-draggable插件 可以使用npm安装vue-draggable插件:`npm install vuedraggable --save`,然后在Vue组件中引入该插件。 ```javascript import draggable from 'vuedraggable' export default { components: { draggable } } ``` 2. 创建表单设计器组件并添加表单元素 在Vue组件中,可以使用`draggable`组件来实现拖放功能。我们可以使用`v-for`指令渲染表单元素列表,并将元素添加到`draggable`组件中。 ```html <draggable v-model="formElements"> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> </draggable> ``` 3. 添加表单元素到表单设计器 我们可以添加按钮来添加新的表单元素。在Vue组件中,我们可以使用`@click`事件添加新的元素到表单元素列表。 ```html <button @click="addInput">Add Input</button> <button @click="addSelect">Add Select</button> ``` ```javascript export default { data() { return { formElements: [] } }, methods: { addInput() { this.formElements.push({ type: 'input', inputType: 'text', placeholder: 'Enter text' }) }, addSelect() { this.formElements.push({ type: 'select', options: ['Option 1', 'Option 2', 'Option 3'] }) } } } ``` 4. 渲染表单 最后,我们可以使用`v-for`指令再次渲染表单元素列表,并将表单元素添加到表单中。 ```html <form> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> <button type="submit">Submit</button> </form> ``` 完整代码如下: ```html <template> <div> <draggable v-model="formElements"> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> </draggable> <button @click="addInput">Add Input</button> <button @click="addSelect">Add Select</button> <form> <div v-for="(element, index) in formElements" :key="index"> <input v-if="element.type === 'input'" :type="element.inputType" :placeholder="element.placeholder"> <select v-else-if="element.type === 'select'"> <option v-for="(option, optionIndex) in element.options" :key="optionIndex">{{ option }}</option> </select> </div> <button type="submit">Submit</button> </form> </div> </template> <script> import draggable from 'vuedraggable' export default { components: { draggable }, data() { return { formElements: [] } }, methods: { addInput() { this.formElements.push({ type: 'input', inputType: 'text', placeholder: 'Enter text' }) }, addSelect() { this.formElements.push({ type: 'select', options: ['Option 1', 'Option 2', 'Option 3'] }) } } } </script> ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值