当前位置: 首页 > article >正文

前端实现鼠标可拖动弹框

       

目录

一、使用原生JS实现

1.HTML结构

2.CSS样式

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

二、使用Vue实现


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

一、使用原生JS实现

1.HTML结构

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

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

2.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实现弹框的可拖动功能

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实现

<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属性获取弹框元素,在事件处理函数中更新弹框位置,并在鼠标松开时停止拖动


http://www.kler.cn/news/365599.html

相关文章:

  • Python实现微博舆情分析的设计与实现
  • 麒麟v10 arm64 部署 kubesphere 3.4 修改记录
  • 智创 AI 新视界 -- 探秘 AIGC 中的生成对抗网络(GAN)应用
  • ESlint代码规范
  • 量子纠错--shor‘s 码
  • traceroute或tracepath区别
  • 推荐一些关于计算机网络和 TCP/IP 协议的书籍
  • 【001】调用kimi实现AI对话_#py
  • 隨筆 20241024 Kafka中的ISR列表:分区副本的族谱
  • 利用客户端导入有关联的业务数据(DBeaver+sql)
  • Linux中安装配置SQLite3,并实现C语言与SQLite3的交互。
  • Python基础之循环语句
  • 近似推断 - 学习的近似推理篇
  • go语言结构体与json数据相互转换
  • 如何动态改变本地的ip
  • 【华为路由】OSPF多区域配置
  • LINUX1.2
  • 主机电子邮件访问:实现高效通信的关键
  • Node.js 23 发布了!不再支持 32 位 windows 系统
  • Ceph入门到精通-Osd db扩容
  • 【赵渝强老师】Hive的内部表与外部表
  • C语言运算符——[]下标运算符
  • 【开源免费】基于SpringBoot+Vue.JS在线视频教育平台(JAVA毕业设计)
  • 离offer更进一步的JVM详细面试题(含答案)
  • ⭐ Unity 序列帧播放脚本
  • NVIDIA发布Nemotron-70B-Instruct,超越GPT-4o和Claude 3.5的AI模型