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

拖拉拽效果加点击事件


<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><title>自由拖拽+点击元素</title><style>body {margin: 0;height: 100vh;display: flex;justify-content: center;align-items: center;background: #f0f0f0;}#container {width: 800px;height: 600px;border: 2px dashed #999;position: relative;overflow: hidden;}.draggable {position: absolute;width: 120px;height: 120px;background: #fff;border-radius: 8px;box-shadow: 0 2px 10px rgba(0,0,0,0.1);cursor: grab;display: flex;flex-direction: column;align-items: center;justify-content: center;transition: box-shadow 0.2s;}.draggable.dragging {cursor: grabbing;box-shadow: 0 5px 15px rgba(0,0,0,0.2);}.tag {padding: 6px 12px;background: #4CAF50;color: white;border-radius: 4px;margin-bottom: 8px;cursor: pointer;transition: background 0.2s;}.tag:hover {background: #45a049;}</style>
</head>
<body><div id="container"><div class="draggable" style="left: 100px; top: 100px"><div class="tag">可拖拽标签</div><div class="coordinates">(100, 100)</div></div></div><script>(function() {// 拖拽系统初始化const draggables = document.querySelectorAll('.draggable');let currentDraggable = null;let startX = 0;let startY = 0;let initialX = 0;let initialY = 0;let isClick = true;// 鼠标按下事件document.addEventListener('mousedown', e => {const target = e.target.closest('.draggable');if (!target) return;currentDraggable = target;startX = e.clientX;startY = e.clientY;initialX = parseFloat(target.style.left) || 0;initialY = parseFloat(target.style.top) || 0;isClick = true;target.classList.add('dragging');document.addEventListener('mousemove', onMouseMove);document.addEventListener('mouseup', onMouseUp);});// 鼠标移动事件function onMouseMove(e) {if (!currentDraggable) return;// 判断是否达到移动阈值(3像素)if (Math.abs(e.clientX - startX) > 3 || Math.abs(e.clientY - startY) > 3) {isClick = false;}const container = document.getElementById('container');const containerRect = container.getBoundingClientRect();const elementRect = currentDraggable.getBoundingClientRect();// 计算新位置let newX = initialX + (e.clientX - startX);let newY = initialY + (e.clientY - startY);// 边界限制newX = Math.max(0, Math.min(newX, containerRect.width - elementRect.width));newY = Math.max(0, Math.min(newY,containerRect.height - elementRect.height));// 更新位置currentDraggable.style.left = `${newX}px`;currentDraggable.style.top = `${newY}px`;// 更新坐标显示currentDraggable.querySelector('.coordinates').textContent = `(${Math.round(newX)}, ${Math.round(newY)})`;}// 鼠标松开事件function onMouseUp(e) {if (!currentDraggable) return;currentDraggable.classList.remove('dragging');document.removeEventListener('mousemove', onMouseMove);document.removeEventListener('mouseup', onMouseUp);currentDraggable = null;// 如果移动距离太小视为点击if (isClick) {handleClick(e);}}// 点击处理函数function handleClick(e) {const tag = e.target.closest('.tag');if (tag) {// 标签点击tag.style.background = '#ff5722';setTimeout(() => tag.style.background = '#4CAF50', 200);alert('标签被点击!');} else {// 整个元素点击const element = e.target.closest('.draggable');element.style.background = '#f8f8f8';setTimeout(() => element.style.background = '#fff', 200);alert('元素被点击!');}}})();</script>
</body>
</html>

http://www.dtcms.com/a/138028.html

相关文章:

  • 智慧交通内容及发展趋势概述
  • 第五章 SQLite数据库:6、SQLite 常用语法1
  • 【数据结构】AVL树
  • 主数据管理:企业数字化转型的 “数据基石“ 如何为 AI 筑基?
  • Google Mock(GMock):C++单元测试的高效模拟框架详解
  • D4707同步整流器:提升Flyback转换器效率的关键元件
  • 本地Ubuntu轻松部署高效性能监控平台SigNoz与远程使用教程
  • Django 实现物联网管理系统的详细方案
  • Unity3D 测试驱动开发(TDD)框架设计
  • Immich图库本地部署与远程管理:打造你的专属照片云服务
  • Intel(R) Wi-Fi 6 AX201 160MHz
  • Linux信号三部曲:产生机制、处理方式与内核接口
  • vscode+keil嵌入式软件开发全流程
  • 数码管LED显示屏矩阵驱动技术详解
  • Django REST Framework (DRF)
  • 神经网络优化 - 高维变量的非凸优化
  • 卡尔曼滤波-从公式到理解
  • Flink调优面试题及参考答案20道
  • 搭建用友U9Cloud ERP及UAP IDE环境
  • Ubuntu 安装cuda踩坑记录
  • CNN卷积神经网络
  • PyCharm Flask 使用 Tailwind CSS 配置
  • Centos7.6安装JDK 1.8教程
  • ESP32- 开发笔记- 硬件设计-ESP32-C3 天线设计-利用嘉立创EDA来设计
  • 力扣算法ing(59 / 100)
  • B端网站建设,怎样平衡功能与美观,满足企业多元需求?
  • 【测试工具】JMeter使用小记
  • Dell戴尔服务器 PowerEdge R750xs + window server2012r2 || 2016
  • kafka报错:The Cluster ID doesn‘t match stored clusterId Some in meta.properties
  • 数据结构|排序算法(三)选择排序 堆排序 归并排序