长按拖拽移动的vue3组件

复制代码
<template>
  <div
    ref="draggableElement"
    class="draggable-element"
    :style="elementStyle"
    @mousedown="startDrag"
    @touchstart="startDrag"
  >
    <slot></slot>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue';

export default {
  name: 'PressAndDrag',
  setup() {
    const draggableElement = ref(null);
    const isDragging = ref(false);
    const startPos = ref({ x: 0, y: 0 });
    const currentPos = ref({ x: 0, y: 0 });
    const pressTimer = ref(null);
    const elementStyle = ref({
      position: 'fixed',
      left: '100px',
      top: '100px',
      cursor: 'pointer',
      userSelect: 'none',
      touchAction: 'none'
    });

    // 长按开始
    const startPress = (event) => {
      // 阻止默认行为,防止触摸设备上的滚动等
      event.preventDefault();
      
      // 设置定时器,500ms后开始拖动
      pressTimer.value = setTimeout(() => {
        isDragging.value = true;
        
        // 获取初始位置
        const clientX = event.clientX || event.touches[0].clientX;
        const clientY = event.clientY || event.touches[0].clientY;
        
        startPos.value = {
          x: clientX - currentPos.value.x,
          y: clientY - currentPos.value.y
        };
      }, 500); // 500ms长按时间
    };

    // 开始拖动(鼠标/触摸按下)
    const startDrag = (event) => {
      startPress(event);
      
      // 添加移动和结束事件的监听
      window.addEventListener('mousemove', handleMove);
      window.addEventListener('touchmove', handleMove, { passive: false });
      window.addEventListener('mouseup', endDrag);
      window.addEventListener('touchend', endDrag);
    };

    // 处理移动
    const handleMove = (event) => {
      if (!isDragging.value) return;
      event.preventDefault();
      
      const clientX = event.clientX || event.touches[0].clientX;
      const clientY = event.clientY || event.touches[0].clientY;
      
      currentPos.value = {
        x: clientX - startPos.value.x,
        y: clientY - startPos.value.y
      };
      
      updatePosition();
    };

    // 更新元素位置
    const updatePosition = () => {
      elementStyle.value.left = `${currentPos.value.x}px`;
      elementStyle.value.top = `${currentPos.value.y}px`;
    };

    // 结束拖动
    const endDrag = () => {
      // 清除长按定时器
      if (pressTimer.value) {
        clearTimeout(pressTimer.value);
        pressTimer.value = null;
      }
      
      isDragging.value = false;
      
      // 移除事件监听
      window.removeEventListener('mousemove', handleMove);
      window.removeEventListener('touchmove', handleMove);
      window.removeEventListener('mouseup', endDrag);
      window.removeEventListener('touchend', endDrag);
    };

    // 组件卸载时清理
    onUnmounted(() => {
      endDrag();
    });

    return {
      draggableElement,
      elementStyle,
      startDrag,
      handleMove,
      endDrag
    };
  }
};
</script>

<style scoped>
.draggable-element {
  background-color: #42b983;
  padding: 20px;
  border-radius: 8px;
  color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
</style>

使用示例

复制代码
<template>
  <div class="container">
    <PressAndDrag>
      长按并拖动我
    </PressAndDrag>
  </div>
</template>

<script>
import PressAndDrag from './PressAndDrag.vue';

export default {
  components: {
    PressAndDrag
  }
};
</script>

<style>
.container {
  height: 100vh;
  position: relative;
}
</style>
相关推荐
风继续吹..1 小时前
后台管理系统权限管理:前端实现详解
前端·vue
yuanmenglxb20041 小时前
前端工程化包管理器:从npm基础到nvm多版本管理实战
前端·前端工程化
新手小新2 小时前
C++游戏开发(2)
开发语言·前端·c++
我不吃饼干2 小时前
【TypeScript】三分钟让 Trae、Cursor 用上你自己的 MCP
前端·typescript·trae
飞翔的佩奇3 小时前
基于SpringBoot+MyBatis+MySQL+VUE实现的经方药食两用服务平台管理系统(附源码+数据库+毕业论文+部署教程+配套软件)
数据库·vue.js·spring boot·mysql·毕业设计·mybatis·经方药食两用平台
小杨同学yx3 小时前
前端三剑客之Css---day3
前端·css
星月心城4 小时前
Promise之什么是promise?(01)
javascript
二川bro4 小时前
第二篇:Three.js核心三要素:场景、相机、渲染器
开发语言·javascript·数码相机
Mintopia5 小时前
🧱 用三维点亮前端宇宙:构建你自己的 Three.js 组件库
前端·javascript·three.js
故事与九5 小时前
vue3使用vue-pdf-embed实现前端PDF在线预览
前端·vue.js·pdf