js实现拖拽列表排序效果代码
代码语言:html
所属分类:拖放
代码描述:js实现拖拽列表排序效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> * { box-sizing: border-box; } :root { height: 100%; font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; line-height: 1.5; font-weight: 400; background: #87a5b2; font-synthesis: none; text-rendering: optimizeLegibility; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; -webkit-text-size-adjust: 100%; } body { height: 100%; display: flex; align-items: center; justify-content: center; } .list { display: flex; flex-direction: column; max-width: 500px; width: 100%; gap: 10px 0; } .list__item { width: 100%; background: white; padding: 15px; border-radius: 5px; color: #001d29; font-weight: 500; font-size: 18px; box-shadow: 0 4px 6px -1px #001d2910, 0 2px 4px -2px #001d2910; display: flex; align-items: center; position: relative; will-change: transform; } .drag-handle { position: absolute; right: 0; width: 44px; height: 44px; display: flex; justify-content: center; align-items: center; } .drag-handle::after { content: '⠿'; font-size: 25px; color: #00000099; } .list__item.is-idle .drag-handle { cursor: grab; } .list__item.is-idle { transition: 0.25s ease transform; } .list__item.is-draggable, .list__item.is-draggable .drag-handle { cursor: grabbing; } .list__item.is-draggable { z-index: 10; } </style> </head> <body > <div class="list js-list"> <div class="list__item is-idle js-item"> 🍦 Ice cream <div class="drag-handle js-drag-handle"></div> </div> <div class="list__item is-idle js-item"> 🥞 Pancake <div class="drag-handle js-drag-handle"></div> </div> <div class="list__item is-idle js-item"> 🧇 Waffle <div class="drag-handle js-drag-handle"></div> </div> <div class="list__item is-idle js-item"> 🍰 Cake <div class="drag-handle js-drag-handle"></div> </div> </div> <script > /*********************** * Variables * ***********************/ let listContainer let draggableItem let pointerStartX let pointerStartY let itemsGap = 0 let items = [] /*********************** * Helper Functions * ***********************/ function getAllItems() { if (!items?.length) { items = Array.from(listContainer.querySelectorAll('.js-item')) } return items } function getIdleItems() { return getAllItems().filter((item) => item.classList.contains('is-idle')) } function isItemAbove(item) { return item.hasAttribute('data-is-above') } function isItemToggled(item) { return item.hasAttribute('data-is-toggled') } /*********************** * Setup * ***********************/ function setup() { listContainer = document.querySelector('.js-list') if (!listContainer) return listContainer.addEventListener('mousedown', dragStart) listContainer.addEventListener('touchstart', dragStart) document.addEventListener('mouseup', dragEnd) document.addEventListener('touchend', dragEnd) } /*********************** * Drag Start * ***********************/ function dragStart(e) { if (e.target.classList.contains('js-drag-handle')) { draggableItem = e.target.closest('.js-item') } if (!draggableItem) return pointerStartX = e.clientX || e.touches[0].clientX pointerStartY = e.clientY || e.touches[0].clientY setItemsGap() disablePageScroll() initDraggableItem() initItemsState() document.addEventListener('mousemove', drag) document.addEventListener('touchmove', drag, { passive: false }) } function setItemsGap() { if (g.........完整代码请登录后点击上方下载按钮下载查看
网友评论0