v-drag拖拽插件使用

插件项目文档

v-drag插件文档地址

使用场景

遇到电子盖章位置选取与保存的问题

使用代码

安装
npm install v-drag --save
main.js中导入
import drag from "v-drag"
Vue.use(drag, {
  // 全局配置
});
vue文件中使用
<template>
  <div class="page-container">
    <div class="page-btn" @click="savePosition">
      位置已选定,保存
    </div>
    <div class="page-content">
      <img :src="imgBase64" alt="" />
      <div v-drag @v-drag-end="someFunction($event)" :style="{
        'left': offsetLeft + 'px',
        'top': offsetTop + 'px',
      }" class="zhang-img">
        <img id="img-position" src="@/assets/xianbanzhang.png" alt="" />
      </div>
    </div>
  </div>
</template>
someFunction(res) {
	console.log('左偏移:' + res.target.offsetLeft)
	this.offsetLeft = res.target.offsetLeft
	console.log('上偏移:' + res.target.offsetTop)
	this.offsetTop = res.target.offsetTop
},
<style lang="scss" scoped>
  .page-content {
    position: relative;
    padding: 0px;
    overflow: hidden;
    margin: 0 auto;
    height: 100%;
  }
  .page-container {
    position: relative;
    padding: 0px;
    background-color: #f3f5f7;
    display: flex;
    justify-content: center;
  }
  .page-btn {
    background-color: #0070d2;
    color: white;
    padding: 12px 18px;
    border-radius: 20px 0px 0px 20px;
    position: fixed;
    top: 200px;
    right: 0;
    cursor: pointer;
    z-index: 9;
  }
  .zhang-img {
    display: inline-block; position: absolute; z-index: 9;
  }
</style>
### v-drag Vue Draggable 实现方式及示例 为了在 Vue.js 项目中实现可拖拽元素的功能,`v-drag` 提供了一种简便的方法来增强用户体验。通过安装并配置 `v-drag`,可以轻松创建具有拖拽功能的组件。 #### 安装与引入 首先需要按照官方文档完成项目的下载和安装[^1]: ```bash npm install --save v-drag ``` 接着,在 Vue 组件内部导入该库,并将其挂载至全局对象上以便于后续调用: ```javascript import drag from 'v-drag'; export default { name: 'App', mounted() { this.$root.$use(drag); } } ``` #### 基本使用案例 下面是一段 HTML 结构配合样式定义的例子,展示了如何利用自定义指令 `v-drag` 来使指定区域内的 DOM 元素具备拖动能力: ```html <template> <div class="drag-container"> <!-- 应用了 v-drag 的 div --> <div v-drag>拖拽我</div> </div> </template> <style scoped> .drag-container .drag-draggable { width: 100px; height: 100px; background-color: #42b983; color: white; text-align: center; line-height: 100px; } </style> ``` 上述代码片段中的 `.drag-draggable` 类名并非必需项,而是用于演示目的设置了一些视觉效果以突出显示被操作的对象。 对于更复杂的场景比如表格行之间的顺序调整,则有专门针对 Element UI 表格设计的插件如 `el-table-draggable` 可供选用[^3];而对于对话框窗口的位置变动需求而言,`vue-dialog-drag` 则提供了相应的解决方案[^4]。 值得注意的是,除了基础的位移变换外,还有其他扩展性的交互行为可以通过类似的第三方包来达成,例如 `vue-drag-resize-rotate` 就允许开发者构建更加灵活多变的应用界面[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值