前端:拖动悬浮小窗

一、效果展示

二、代码

复制代码
<template>
  <div class="contents">
    <div 
      class="contents--menu" 
      :style="style"
      @mousedown="startDrag"
      @touchstart="startDrag"
      @mousemove="onDrag"
      @touchmove="onDrag"
      @mouseup="stopDrag"
      @touchend="stopDrag"
    >
      <m-button v-for="(item, index) in list" :key="index" @click="changeComponent(item)">{{item.name}}</m-button>
    </div>
    <component :is="activeComponent"></component>
  </div>
</template>
<script setup lang="ts">
  // const itemsButton = defineAsyncComponent(() => import('../items/button/index.vue'))
import itemsButton from '../items/button/index.vue'
import modulesButtons from '../modules/buttons/index.vue'
import modulesForm from '../modules/form/index.vue'
import modulesTable from '../modules/table/index.vue'
import pagesList from '../pages/list/index.vue'
const list = [
  { name: '按钮', component: itemsButton },
  { name: '按钮组', component: modulesButtons },
  { name: '表单', component: modulesForm },
  { name: '表格', component: modulesTable },
  { name: '列表', component: pagesList },
]
let activeComponent = ref(list[0].component)
const changeComponent = (item: any) => {
  activeComponent.value = item.component
}


const isDragging = ref(false);
const initialPosition = ref({ x: 0, y: 0 });
const currentPosition = ref({ x: 100, y: 100 }); // 初始位置

const style = computed(() => ({
  position: 'fixed' as const,
  left: `${currentPosition.value.x}px`,
  top: `${currentPosition.value.y}px`,
  cursor: isDragging.value ? 'grabbing' : 'grab',
  userSelect: 'none' as const,
}));

const startDrag = (e: MouseEvent | TouchEvent) => {
  e.preventDefault();
  isDragging.value = true;
  
  // 获取鼠标或触摸位置
  const clientX = e instanceof MouseEvent ? e.clientX : e.touches[0].clientX;
  const clientY = e instanceof MouseEvent ? e.clientY : e.touches[0].clientY;
  
  // 计算鼠标位置与元素左上角的偏移量
  initialPosition.value = {
    x: clientX - currentPosition.value.x,
    y: clientY - currentPosition.value.y
  };
};

const onDrag = (e: MouseEvent | TouchEvent) => {
  if (!isDragging.value) return;
  
  e.preventDefault();
  
  // 获取当前鼠标或触摸位置
  const clientX = e instanceof MouseEvent ? e.clientX : e.changedTouches[0].clientX;
  const clientY = e instanceof MouseEvent ? e.clientY : e.changedTouches[0].clientY;
  
  // 计算新位置
  currentPosition.value = {
    x: clientX - initialPosition.value.x,
    y: clientY - initialPosition.value.y
  };
};

const stopDrag = () => {
  isDragging.value = false;
};
</script>
<style scoped lang="scss">
.contents{
  &--menu{
    z-index: 20;
    border-radius: 8px;
    width: 200px;
    border: 2px solid #ccc;
    padding: 15px;
    background-color: #f9f9f9;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: box-shadow 0.2s;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    
    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }
    
    &.dragging {
      z-index: 9999;
    }
    .el-button+.el-button{
      margin: 0;
    }
  }
}
</style>
相关推荐
MyBFuture2 小时前
C# 二进制数据读写与BufferStream实战
开发语言·c#·visual studio
川石课堂软件测试2 小时前
软件测试的白盒测试(二)之单元测试环境
开发语言·数据库·redis·功能测试·缓存·单元测试·log4j
2301_796512522 小时前
ModelEngine平台创建知识库体系 ,帮助“前端职业导航师”定制化私域知识累积
前端·modelengine
鹏程十八少2 小时前
Android ANR项目实战:Reason: Broadcast { act=android.intent.action.TIME_TICK}
android·前端·人工智能
温轻舟2 小时前
圣诞节雪人动态效果 | HTML页面
开发语言·前端·javascript·html·css3·温轻舟·圣诞
云技纵横2 小时前
Vue 2 生产构建 CSS 压缩报错修复与深度选择器规范
前端·css·vue.js
加成BUFF2 小时前
C++入门讲解6:数据的共享与保护核心机制解析与实践
开发语言·c++
Codebee2 小时前
打破偏见!企业级AI不是玩具!Ooder全栈框架+程序员=专业业务系统神器
前端·全栈
IT_Octopus2 小时前
Java Protobuf+Zstd 压缩存储Redis实践&问题解决&对比Gzip压缩的大小和性能
java·开发语言·redis