常见的拖拽效果,以及最新的h5拖拽效果及其原理解析

news/2025/3/14 18:19:22/

首先是使用h5的api实现拖拽排序效果
需要实现的效果
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>body {display: flex;flex-direction: row;align-content: center;justify-content: center;align-items: center;}.list {/* 去除ul小点 */list-style: none;}.list-item {user-select: none;width: 400px;height: 35px;line-height: 35px;text-align: center;border: 1px solid #ccc;/* 渐变背景 */background: linear-gradient(to right, #015d69, #51b9ff);cursor: move;color: #fff;border-radius: 10px;}.list-item.moving {background: transparent;color: transparent;border: 1px dashed #ccc;}</style>
</head><body><ul class="list"><li draggable="true" class="list-item">1</li><li draggable="true" class="list-item">2</li><li draggable="true" class="list-item">3</li><li draggable="true" class="list-item">4</li><li draggable="true" class="list-item">5</li><li draggable="true" class="list-item">6</li><li draggable="true" class="list-item">7</li><li draggable="true" class="list-item">8</li></ul><script>const list = document.querySelector('.list');let sourceNode; //当前在拖动的节点list.ondragstart = e => {setTimeout(() => {e.target.classList.add('moving')}, 0);sourceNode = e.targete.dataTransfer.effectAllowed = 'move';}list.ondragover = e => {e.preventDefault()}list.ondragenter = e => {e.preventDefault();if (e.target === list || e.target === sourceNode) {return}const children = Array.from(list.children)const sourceIndex = children.indexOf(sourceNode)const targetIndex = children.indexOf(e.target)if (sourceIndex < targetIndex) {list.insertBefore(sourceNode, e.target.nextElementSibling)} else {list.insertBefore(sourceNode, e.target)}}list.ondragend = e => {e.target.classList.remove('moving')}</script>
</body>
</html>

原理解析
这是因为使用了h5的 draggable属性,页面中设置了 draggable=“true” 属性的元素可以进行拖拽
示例如下
代码示例:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>body {display: flex;flex-direction: row;align-content: center;justify-content: space-evenly;align-items: center;}.box {width: 200px;height: 200px;background-color: red;line-height: 200px;text-align: center;}</style>
</head><body><div class="box">未添加draggable</div><div draggable="true" class="box">添加了draggable</div>
</body></html>

效果示例
在这里插入图片描述
给 box1 增加了draggable=“true” 属性之后, box1 就变成可以拖拽的了。但是拖拽之后要做什么事情?就涉及到事件监听。

拖拽元素的事件监听:(应用于拖拽元素)

  • ondragstart当拖拽开始时调用
  • ondragleave 当鼠标离开拖拽元素时调用
  • ondragend 当拖拽结束时调用
  • ondrag 整个拖拽过程都会调用

演示一下

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>body {display: flex;flex-direction: row;align-content: center;justify-content: space-evenly;align-items: center;}.box {width: 200px;height: 200px;background-color: red;line-height: 200px;text-align: center;}</style>
</head><body><div class="box">未添加draggable</div><div id="box" draggable="true" class="box">添加了draggable</div><script>var box = document.querySelector('#box');//  绑定拖拽事件//  拖拽开始box.ondragstart = function () {console.log('拖拽开始.');}//  拖拽离开:鼠标拖拽时离开被拖拽的元素时触发box.ondragleave = function () {console.log('拖拽离开..');}//  拖拽结束box.ondragend = function () {console.log('拖拽结束...');console.log("---------------");}box.ondrag = function () {console.log('拖拽');}</script>
</body></html>

效果展示

请忽略那些报错,那是浏览器插件的报错
请添加图片描述

目标元素

拖动元素侦听完成了,剩下的就是实现如何的把元素放到目标元素那里去了

比如说,你想把元素A拖拽到元素B里,那么元素B就是目标元素。

页面中任何一个元素都可以成为目标元素。

目标元素的事件监听:(应用于目标元素)

  • ondragenter 当拖拽元素进入时调用
  • ondragover 当拖拽元素停留在目标元素上时,就会连续一直触发(不管拖拽元素此时是移动还是不动的状态)
  • ondrop 当在目标元素上松开鼠标时调用
  • ondragleave 当鼠标离开目标元素时调用
    效果如下

请添加图片描述

代码示例

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>body {display: flex;flex-direction: row;align-content: center;justify-content: space-evenly;align-items: center;}.box {width: 200px;height: 200px;background-color: red;line-height: 200px;text-align: center;}.target-box {width: 500px;height: 500px;background-color: green;line-height: 300px;text-align: center;}</style>
</head><body><div class="box">未添加draggable</div><div id="box" draggable="true" class="box">添加了draggable</div><div class="target-box"></div><script>// var box = document.querySelector('#box');// //  绑定拖拽事件// //  拖拽开始ondragstart 触发// box.ondragstart = function () {//   console.log('拖拽开始.');// }// //  拖拽离开:鼠标拖拽时离开被拖拽的元素时ondragleave 触发// box.ondragleave = function () {//   console.log('拖拽离开..');// }// //  拖拽结束ondragend 事件触发// box.ondragend = function () {//   console.log('拖拽结束...');//   console.log("---------------");// }// box.ondrag = function () {//   console.log('拖拽');// }var two = document.querySelector('.target-box');//目标元素的拖拽事件// 当被拖拽元素进入时ondragenter 触发two.ondragenter = function () {console.log("移入目标盒子.");}// 当被拖拽元素离开时ondragleave 触发two.ondragleave = function () {console.log("移出目标盒子");}// 当拖拽元素在 目标元素上时,ondragover 会连续触发two.ondragover = function (e) {//阻止拖拽事件的默认行为e.preventDefault(); console.log("在目标盒子上");}// 当在目标元素上松开鼠标时ondrop 触发two.ondrop = function () {console.log("在目标盒子上松开了鼠标");}</script>
</body></html>

http://www.ppmy.cn/news/1106421.html

相关文章

手工测试项目实战

功能测试实战 项目介绍及说明 项目部署 开发语言 web服务器 asp IIS php apache java comcat 将其放在网站上&#xff0c;映射地址 查看文件中是否有相对路径…/文件表示方式&#xff0c;有的话需要启用父路径&#xff0c;并点击应用 项目为32位&#xff0c;但win7虚拟机…

Java注解与python函数修饰器区别是啥

区别 Python的函数修饰器&#xff08;Decorators&#xff09;和Java的注解&#xff08;Annotations&#xff09;都是两种用于修改或增强代码功能的工具&#xff0c;但它们的用法和功能上有一些显著的区别。 语法和功能&#xff1a;Python的修饰器是一种函数&#xff0c;它可以…

哪些因素影响App Store上的应用排名

高的转化率会对应用排名产生积极影响。这在很大程度上取决于&#xff0c;在应用元数据的基础上优化应用程序的视觉资产&#xff1a;图标&#xff0c;屏幕截图和视频&#xff0c;这能够有效地传达应用程序的价值并且吸引目标受众。 1、在应用元数据中包含相关关键词。 提高应用…

荐片怕不是写病-毒出身的吧?

事情的经过是这样的&#xff0c;我发现我的电脑上一直有Jp_Update.exe在运行&#xff0c;之前安装过荐片客户端。既然运行了肯定有出处嘛&#xff0c;那就找呗 首先排查启动项、服务、计划任务这3块常规启动&#xff0c;发现并没有&#xff08;安装完就禁用了服务)。 后面想着…

Android逆向——脱壳解析

“壳”是一种对程序进行加密的程序&#xff0c;“壳”形象地表现了这个功能。我们可以把被加壳的程序当成食物&#xff0c;而加壳程序就是在外面加上一层坚硬的外壳&#xff0c;防止别人去窃取其中的程序。加壳后的程序依然可以被直接运行。在程序运行时壳的代码先运行&#xf…

为什么 Elasticsearch 中高基数字段上的聚合是一个坏主意以及如何优化它

Elasticsearch 是分布式搜索和分析引擎&#xff0c;是满足搜索和聚合需求的最受欢迎的选择。 Elasticsearch 提供了 2 种数据类型来存储字符串值&#xff1a; Text&#xff1a;- 在存储到倒排索引之前对这些内容进行分析&#xff0c;并针对全文搜索进行优化。 文本字段不允许…

【LeetCode刷题笔记】动态规划 — 70.爬楼梯

创作不易&#xff0c;本篇文章如果帮助到了你&#xff0c;还请点赞 关注支持一下♡>&#x16966;<)!! 主页专栏有更多知识&#xff0c;如有疑问欢迎大家指正讨论&#xff0c;共同进步&#xff01; 更多算法知识专栏&#xff1a;算法分析&#x1f525; 给大家跳段街舞感谢…

Android GUI系统之SurfaceFlinger(16)MessageBase解读

该系列文章总纲链接&#xff1a;Android GUI系统之SurfaceFlinger 系列文章目录 说明&#xff1a;以下代码分析均在android5.1.1_r3分支上 目录frameworks/native/services/surfaceflinger为root目录 1 MessageBase解读 1.1 源码实现分析 MessageBase源码实现如下&#xff…