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

拖拽排序的实现示例demo

拖拽排序的实现示例demo

    • 文章说明
    • 核心代码
    • 示例效果展示

文章说明

文章主要为了学习拖拽排序的实现思路,并且采用此示例效果来进一步理解Flip动画的使用

参考渡一前端袁老师的讲解视频

核心代码

页面源码,拖拽排序的实现代码并不复杂,但是可以很好的帮助学习该示例的实现思路和拖拽API的使用

<!DOCTYPE html>
<html lang="zh-cn"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>拖拽排序</title><style>* {box-sizing: border-box;}.container {margin: 100px auto auto;width: 200px;}.container div {height: 30px;line-height: 30px;margin: 10px 0;background-color: chocolate;border-radius: 10px;color: #ffffff;text-align: center;}.container .moving {border: dashed 1px black;background-color: #ffffff;}</style></head><body><div class="container"><div draggable="true" class="item">1</div><div draggable="true" class="item">2</div><div draggable="true" class="item">3</div><div draggable="true" class="item">4</div><div draggable="true" class="item">5</div><div draggable="true" class="item">6</div></div><script src="flip.js"></script><script>const container = document.getElementsByClassName("container")[0];let dragElem = null;let flip;container.ondragstart = function (e) {flip = new Flip(container.children, 0.5);dragElem = e.target;setTimeout(() => {e.target.classList.add("moving");}, 0);}container.ondragover = function (e) {e.preventDefault();}container.ondragend = function (e) {e.target.classList.remove("moving");}container.ondragenter = function (e) {e.preventDefault();if (e.target === container || dragElem === e.target) {return;}const children = [...container.children];const sourceIndex = children.indexOf(dragElem);const targetIndex = children.indexOf(e.target);if (sourceIndex > targetIndex) {container.insertBefore(dragElem, e.target);} else {container.insertBefore(dragElem, e.target.nextElementSibling);}flip.play();}</script></body>
</html>

Flip工具类

class Flip {children = null;delay = 0;constructor(children, delay = 1) {this.children = children;this.calculatePos();this.delay = delay;}calculatePos(name = "first") {const children = this.children;for (let i = 0; i < children.length; i++) {children[i][name] = children[i].getBoundingClientRect();}}play() {this.calculatePos("last");const children = this.children;for (let i = 0; i < children.length; i++) {const first = children[i]["first"];const last = children[i]["last"];if (first.x !== last.x || first.y !== last.y) {children[i].style.transform = `translateY(${first.y - last.y}px) translateX(${first.x - last.x}px)`;setTimeout(() => {children[i].style.transition = `transform ${this.delay}s`;children[i].style.removeProperty("transform");setTimeout(() => {children[i].style.removeProperty("transition");this.calculatePos();}, this.delay * 1000);}, 0);}}}
}

示例效果展示

拖拽功能展示
在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 物品识别——基于python语言
  • 人工智能GPT____豆包使用的一些初步探索步骤 体验不一样的工作
  • [NSSRound#4 SWPU]hide_and_seek-用gdb调试
  • 【2024】前端学习笔记6-容器标签div
  • 人工智能开发实战matplotlib库应用基础
  • 性能测试-jmeter的控制器(十六)
  • 腾讯云升级多个云存储解决方案 以智能化存储助力企业增长
  • 物联网——DMA+AD多通道
  • Flutter - Win32程序是如何执行main函数
  • 易语言源码用键盘按键代替小键盘写法教程
  • esp32-C2 对接火山引擎实现智能语音(一)
  • 只有C盘的windows系统通过磁盘分区分出D盘
  • 计算机毕业设计 基于协同过滤算法的个性化音乐推荐系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试
  • electron react离线使用monaco-editor
  • 算法练习题27——疫情下的电影院(模拟)
  • (三)从jvm层面了解线程的启动和停止
  • [NodeJS] 关于Buffer
  • 「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
  • 【162天】黑马程序员27天视频学习笔记【Day02-上】
  • HTTP传输编码增加了传输量,只为解决这一个问题 | 实用 HTTP
  • JAVA 学习IO流
  • JavaScript服务器推送技术之 WebSocket
  • log4j2输出到kafka
  • Lsb图片隐写
  • Object.assign方法不能实现深复制
  • uni-app项目数字滚动
  • vue2.0项目引入element-ui
  • Vue2.x学习三:事件处理生命周期钩子
  • webpack项目中使用grunt监听文件变动自动打包编译
  • 从PHP迁移至Golang - 基础篇
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 前端_面试
  • 手机端车牌号码键盘的vue组件
  • 问题之ssh中Host key verification failed的解决
  • 再次简单明了总结flex布局,一看就懂...
  • 栈实现走出迷宫(C++)
  • ​Python 3 新特性:类型注解
  • !$boo在php中什么意思,php前戏
  • #QT(QCharts绘制曲线)
  • (1)(1.13) SiK无线电高级配置(六)
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (附源码)ssm考试题库管理系统 毕业设计 069043
  • (个人笔记质量不佳)SQL 左连接、右连接、内连接的区别
  • (力扣题库)跳跃游戏II(c++)
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • ***原理与防范
  • *Django中的Ajax 纯js的书写样式1
  • *setTimeout实现text输入在用户停顿时才调用事件!*
  • .gitignore
  • .NET CORE 3.1 集成JWT鉴权和授权2
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .Net Redis的秒杀Dome和异步执行
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉