基于Vue3 和 Ant Design Vue实现Modal弹窗拖拽组件

首先,我们需要安装并引入核心工具:

javascript 复制代码
npm install @vueuse/core

实现:

javascript 复制代码
<script setup>
import { useDraggable } from '@vueuse/core'
import { computed, ref, useTemplateRef, watch, watchEffect } from 'vue'

defineOptions({
  name: 'DraggableModal',
})

defineProps({
  title: {
    type: String,
  },
})

const modalTitleRef = useTemplateRef('modalTitleRef')
const { x, y, isDragging } = useDraggable(modalTitleRef)
const startX = ref(0)
const startY = ref(0)
const startedDrag = ref(false)
const transformX = ref(0)
const transformY = ref(0)
const preTransformX = ref(0)
const preTransformY = ref(0)
const dragRect = ref({
  left: 0,
  right: 0,
  top: 0,
  bottom: 0,
})
watch([x, y], () => {
  if (!startedDrag.value) {
    startX.value = x.value
    startY.value = y.value
    const bodyRect = document.body.getBoundingClientRect()
    const titleRect = modalTitleRef.value.getBoundingClientRect()
    dragRect.value.right = bodyRect.width - titleRect.width
    dragRect.value.bottom = bodyRect.height - titleRect.height
    preTransformX.value = transformX.value
    preTransformY.value = transformY.value
  }
  startedDrag.value = true
})
watch(isDragging, () => {
  if (!isDragging.value) startedDrag.value = false
})
watchEffect(() => {
  if (startedDrag.value) {
    transformX.value =
      preTransformX.value +
      Math.min(Math.max(dragRect.value.left, x.value), dragRect.value.right) -
      startX.value
    transformY.value =
      preTransformY.value +
      Math.min(Math.max(dragRect.value.top, y.value), dragRect.value.bottom) -
      startY.value
  }
})
const transformStyle = computed(() => {
  return {
    transform: `translate(${transformX.value}px, ${transformY.value}px)`,
  }
})
</script>

<template>
  <a-modal :wrap-style="{ overflow: 'hidden' }">
    <template #title>
      <div ref="modalTitleRef" style="width: 100%; cursor: move; user-select: none">
        <slot name="title">
          {{ title }}
        </slot>
      </div>
    </template>
    <template #modalRender="{ originVNode }">
      <div :style="transformStyle">
        <component :is="originVNode" />
      </div>
    </template>
    <slot />
    <template v-if="$slots.footer" #footer>
      <slot name="footer" />
    </template>
    <template v-if="$slots.closeIcon" #closeIcon>
      <slot name="closeIcon" />
    </template>
    <template v-if="$slots.cancelText" #cancelText>
      <slot name="cancelText" />
    </template>
    <template v-if="$slots.okText" #okText>
      <slot name="okText" />
    </template>
  </a-modal>
</template>

<style scoped></style>
相关推荐
神奇的程序员1 天前
开发了一个管理本地开发环境的软件
前端·flutter
天若有情6731 天前
程序员原创|借鉴JS事件冒泡,根治电脑文件混乱的“冒泡整理法”
开发语言·javascript·windows·ecmascript·电脑·办公·日常
XiYang-DING1 天前
HTML 核心标签
前端·html
Csvn1 天前
技术选型方法论
前端
Csvn1 天前
前端架构演进:从页面到平台的十年变革
前端
李伟_Li慢慢1 天前
ShaderToy-山峦+蓝天+白云
前端·webgl
小码哥_常1 天前
Android字体字重设置全攻略:XML黑科技+Kotlin动态实现,告别.ttf臃肿
前端
FYKJ_20101 天前
springboot校园兼职平台--附源码02041
java·javascript·spring boot·python·eclipse·django·php
言萧凡_CookieBoty1 天前
AI 编程省 Token 实战:从 Spec、上下文工程到模型分层的降本策略
前端·ai编程
DFT计算杂谈1 天前
wannier90 参数详解大全
java·前端·css·html·css3