基于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>
相关推荐
FungLeo11 小时前
成为全栈·React 管理后台篇·总复盘:从“接口能调通”到“后台值得使用”
前端·react.js·前端框架·项目复盘·成为全栈
Csvn13 小时前
并发模式:让渲染学会排队、插队和让路
前端
可乐鸡翅yeah_13 小时前
新手梳理:M3U8 线上问题,哪些是前端锅,哪些是后端锅
前端·ios·音视频·实时音视频·m3u8·音视频在线播放
Flynt13 小时前
Linear 用 1000 个 PR 换掉 styled-components,我写了 200 个按钮,把这笔账复现了一遍
前端·css·preact
JavaGuide14 小时前
NVIDIA 又开源了!这次给 AI Agent 加上权限管控
前端·后端
excel14 小时前
prisma 如何处理数据库竞态
前端·数据库·后端
莪_幻尘16 小时前
Skill 体检:30 个 Skill 全凭感觉?体检器先自曝了 8 个“假 0 分
前端·人工智能·llm
风骏时光牛马16 小时前
AI模型综合能力评测:性能、指令遵循与多场景实测对比
前端
Frag0ut16 小时前
Chrome与Chromium内核浏览器在Windows 11上的新特性全景解析
前端·chrome·windows·web安全·chromium·gemini ai·playready drm
hiahiahia12316 小时前
实现完整 Tool Dispatcher
开发语言·前端