1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
| <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <title>js拖拽自动排序</title> </head> <style> * { padding: 0; margin: 0; } html, body { width: 100%; height: 100%; user-select: none; } h1 { text-align: center; } .wrap { position: relative; width: 600px; height: 400px; margin: 100px auto; border: solid 1px black; } .wrap div { position: absolute; z-index: 1; width: 100px; height: 100px; background: rgb(243, 243, 243); transition: all 0.5s; } .wrap p { background: gray; color: #fff; padding-left: 5px; } </style> <body> <h1>js拖拽自动排序</h1> <div class="wrap" id="elWrap"> <div><p>1</p></div> <div><p>2</p></div> <div><p>3</p></div> <div><p>4</p></div> <div><p>5</p></div> <div><p>6</p></div> <div><p>7</p></div> <div><p>8</p></div> <div><p>9</p></div> <div><p>10</p></div> <div><p>11</p></div> <div><p>12</p></div> <div><p>13</p></div> <div><p>14</p></div> <div><p>15</p></div> </div> <script> let index = 0 let elArr = document.getElementById('elWrap').children let elList = [] for (let i = 0; i < elArr.length; i++) { elList.push({ el: elArr[i], sort: i, index: i }) elList[i].onClick = addEvent(elList[i]) } moveItem(elList, 125)
function addEvent(item) { item.el.addEventListener('mousedown', (e) => { item.el.style.zIndex = 2 item.el.style.transition = 'all 0s' let startX = e.pageX let startY = e.pageY let left = item.el.offsetLeft let top = item.el.offsetTop
let moveFun = (e) => { let X = left + e.pageX - startX let Y = top + e.pageY - startY item.el.style.left = X + 'px' item.el.style.top = Y + 'px' reRange(item, X, Y, 125) } document.addEventListener('mousemove', moveFun) document.addEventListener('mouseup', () => { document.removeEventListener('mousemove', moveFun) item.el.style.zIndex = 1 item.el.style.transition = 'all 0.5s' moveItem(elList, 125) }) }) } function reRange(item, x, y, width) {
let moveIndex = Math.round(x / width) + Math.round(y / width) * 5 moveIndex = moveIndex < 0 ? 0 : moveIndex moveIndex = moveIndex > elList.length - 1 ? elList.length - 1 : moveIndex if (moveIndex !== index) { index = moveIndex let currentSort = item.sort for (let i = 0; i < elList.length; i++) { if (currentSort < moveIndex) { if (elList[i].sort >= currentSort && elList[i].sort <= moveIndex) { elList[i].sort -= 1 } } else if (currentSort > moveIndex) { if (elList[i].sort <= currentSort && elList[i].sort >= moveIndex) { elList[i].sort += 1 } } } elList[item.index].sort = moveIndex moveItem(elList, 125) } } function moveItem(elList, width) { for (let i = 0; i < elList.length; i++) { elList[i].el.style.left = elList[i].sort % 5 * width + 'px' elList[i].el.style.top = parseInt(elList[i].sort / 5) * width + 'px' } } </script> </body> </html>
|