【Vue3】Vue3拖拽组件级封装--可快速上手

【Vue3】Vue3拖拽组件级封装–可快速上手

基于 vue3-draggable-resizable

二次封装Vue3+Ts

1、安装依赖

npm install vue3-draggable-resizable

2、src/components下创建组件V3Draggable.vue

V3Draggable.vue

<template>
    <Vue3DraggableResizable ref="draggable" :initW="initW" :initH="initH" v-model:x="x" v-model:y="y" v-model:w="w"
        v-model:h="h" v-model:active="active" :draggable="draggable" :resizable="resizable"
        :lockAspectRatio="lockAspectRatio" :disabledX="disabledX" :disabledY="disabledY" :disabledW="disabledW"
        :disabledH="disabledH" :parent="parent" :classNameDraggable="classNameDraggable" :classNameResizable="classNameResizable"
        :classNameDragging="classNameDragging" :classNameResizing="classNameResizing" @activated="print('activated')"
        @deactivated="print('deactivated')" @drag-start="dragStart" @resize-start="print('resize-start')"
        @dragging="dragging" @resizing="print('resizing')" @drag-end="dragEnd" @resize-end="print('resize-end')">
        <slot>
            <span>这是一个可操作的盒子</span>
        </slot>
    </Vue3DraggableResizable>
</template>
  
<script setup lang="ts">
import { ref } from 'vue'
import Vue3DraggableResizable from 'vue3-draggable-resizable'
//default styles
import 'vue3-draggable-resizable/dist/Vue3DraggableResizable.css'

/**
 * 
 * @description 组件的props
 * @property {number} initW 初始宽度
 * @property {number} initH 初始高度
 * @property {boolean} draggable 是否可拖动
 * @property {boolean} resizable 是否可缩放
 * @property {boolean} lockAspectRatio 是否保持长宽比
 * @property {boolean} disabledX 是否禁用x轴拖动
 * @property {boolean} disabledY 是否禁用y轴拖动
 * @property {boolean} disabledW 是否禁用宽度拖动
 * @property {boolean} disabledH 是否禁用高度拖动
 * @property {boolean} parent 是否将组件的拖动和缩放限制在其父节点内,即组件不会超出父节点,默认开启
 * @event {Function} activated 激活事件
 * @event {Function} deactivated 失活事件
 * @event {Function} drag-start 拖动开始事件
 * @event {Function} resize-start 缩放开始事件
 * @event {Function} dragging 拖动中事件
 * @event {Function} resizing 缩放中事件
 * @event {Function} drag-end 拖动结束事件
 * @event {Function} resize-end 缩放结束事件
 * @property {string} classNameDraggable 自定义组件的类名,该类名在组件是“可拖动”时显示
 * @property {string} classNameResizable 自定义组件的类名,该类名在组件是“可缩放”时显示
 * @property {string} classNameDragging 自定义组件的类名,该类名在组件是“拖动时”时显示
 * @property {string} classNameResizing 自定义组件的类名,该类名在组件是“缩放时”时显示
 * 
 */
type Props = {
    initW?: number,
    initH?: number,
    draggable?: boolean,
    resizable?: boolean,
    lockAspectRatio?: boolean,
    disabledX?: boolean,
    disabledY?: boolean,
    disabledW?: boolean,
    disabledH?: boolean,
    parent?: boolean,
    classNameDraggable?: string,
    classNameResizable?: string,
    classNameDragging?: string,
    classNameResizing?: string,

}
withDefaults(defineProps<Props>(), {
    initW: 200,
    initH: 200,
    draggable: true,
    resizable: true,
    lockAspectRatio: false,
    disabledX: false,
    disabledY: false,
    disabledW: false,
    disabledH: false,
    parent: true,
    classNameDraggable: 'draggable',
    classNameResizable: 'resizable',
    classNameDragging: 'dragging',
    classNameResizing: 'resizing',
})

const x = ref(100)
const y = ref(100)
const w = ref(100)
const h = ref(100)
const active = ref(false)

const cursor = ref('')
const print = (val: string) => {
    console.log(val)
}

const dragStart = () => {
    cursor.value = 'move'
}
const dragging = () => {
    cursor.value = 'move'
}
const dragEnd = () => {
    cursor.value = ''
}

</script>
<style>
.vdr-container {
    border: 1px solid #333;
    cursor: v-bind(cursor);
}
</style>

3、使用

例如:App.vue

<template>
    <div class="wrapper">
        <V3Draggable></V3Draggable>
    </div>
</template>

<script setup lang="ts">
import V3Draggable from "@/components/V3Draggable.vue";

</script>

<style>
.wrapper{
    width: 600px;
    height: 500px;
    background-color: #eee;
}
</style>

忠言逆耳:靠自己不如靠别人

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值