前端实现鼠标可拖动弹框

目录

一、使用原生JS实现

1.HTML结构

2.CSS样式

3.使用JavaScript实现弹框的可拖动功能

二、使用Vue实现


分享一下前端常见功能"可拖动弹框"的实现方式,分别用原生JS和Vue实现。

一、使用原生JS实现

1.HTML结构

首先创建一个弹框的HTML结构,例如:

html 复制代码
<div id="draggableDialog">
  <div class="dialog-header">可拖动弹框标题</div>
  <div class="dialog-content">弹框内容</div>
</div>

2.CSS样式

设置弹框样式,使其具有一定的外观和布局:

css 复制代码
#draggableDialog {
  position: absolute;
  width: 300px;
  height: 200px;
  background-color: #fff;
  border: 1px solid #ccc;
  padding: 10px;
}

.dialog-header {
  cursor: move;
  background-color: #f0f0f0;
  padding: 5px;
}

3.使用JavaScript实现弹框的可拖动功能

javascript 复制代码
const dialog = document.getElementById('draggableDialog');
const dialogHeader = dialog.querySelector('.dialog-header');

let isDragging = false;
let offsetX, offsetY;

dialogHeader.addEventListener('mousedown', (e) => {
  isDragging = true;
  offsetX = e.clientX - dialog.offsetLeft;
  offsetY = e.clientY - dialog.offsetTop;
});

document.addEventListener('mousemove', (e) => {
  if (isDragging) {
    dialog.style.left = e.clientX - offsetX + 'px';
    dialog.style.top = e.clientY - offsetY + 'px';
  }
});

document.addEventListener('mouseup', () => {
  isDragging = false;
});

在上述代码中,首先获取弹框元素和弹框头部元素。当在弹框头部按下鼠标时,记录鼠标位置与弹框位置的偏移量,并设置拖动状态为true。当鼠标移动时,如果处于拖动状态,根据鼠标位置和偏移量更新弹框的位置。当鼠标松开时,设置拖动状态为false。

二、使用Vue实现

javascript 复制代码
<template>
  <div id="draggableDialog" ref="dialog">
    <div class="dialog-header" @mousedown="startDrag($event)">可拖动弹框标题</div>
    <div class="dialog-content">弹框内容</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDragging: false,
      offsetX: 0,
      offsetY: 0
    };
  },
  methods: {
    startDrag(event) {
      this.isDragging = true;
      this.offsetX = event.clientX - this.$refs.dialog.offsetLeft;
      this.offsetY = event.clientY - this.$refs.dialog.offsetTop;
      document.addEventListener('mousemove', this.drag);
      document.addEventListener('mouseup', this.stopDrag);
    },
    drag(event) {
      if (this.isDragging) {
        this.$refs.dialog.style.left = event.clientX - this.offsetX + 'px';
        this.$refs.dialog.style.top = event.clientY - this.offsetY + 'px';
      }
    },
    stopDrag() {
      this.isDragging = false;
      document.removeEventListener('mousemove', this.drag);
      document.removeEventListener('mouseup', this.stopDrag);
    }
  }
};
</script>

<style>
#draggableDialog {
  position: absolute;
  width: 300px;
  height: 200px;
  background-color: #fff;
  border: 1px solid #ccc;
  padding: 10px;
}

.dialog-header {
  cursor: move;
  background-color: #f0f0f0;
  padding: 5px;
}
</style>

在 Vue 示例中,通过在模板中定义弹框结构,并在方法中实现拖动的逻辑。使用ref属性获取弹框元素,在事件处理函数中更新弹框位置,并在鼠标松开时停止拖动

相关推荐
福兮说11 小时前
设计稿是 #4A7C6F,页面量出来是 #4B7C6F:HEX、HSL、透明度、canvas 来回转的七个坑
前端·javascript·css·canvas
郑州光合科技余经理11 小时前
海外版外卖加盟:总站与分站配送规则怎么分开管
java·开发语言·前端·后端·uni-app·php·ai编程
小呆呆66612 小时前
副业搞起来,小说,漫画,漫剧的成本优化思路
前端·后端·面试
凤城老人12 小时前
从 PyQt6 到 Electron:给 Edge TTS 做一个“多角色配音机“的踩坑手记
javascript·typescript·electron
Dovis(誓平步青云)12 小时前
浇水提醒刚弹出又消失,植物状态别只存一个百分比
开发语言·前端·javascript·pdf·ecmascript·电脑
码艺-Alimjan13 小时前
Vben Admin 新增维吾尔语 Vben-Modal的关键坑之一
前端·javascript·vue.js
可乐鸡翅yeah_13 小时前
hls.js 手动自定义 http 请求 loader,修改请求头实战
开发语言·前端·javascript·网络协议·http·ecmascript·m3u8在线
IT_陈寒14 小时前
Vite静态资源导入这个坑我帮你们踩过了
前端·人工智能·后端
广州华水科技14 小时前
大坝安全监测解决方案:单北斗GNSS形变监测系统应用与维护
前端
yivifu14 小时前
中文古籍电子书注释集成
前端·javascript·python·beautifulsoup·epub