Draggable List
Home
Snippets
Draggable List
HTML
CSS
JS
<div class="wrapper"> <h2>Task Priority List</h2> <ul class="list" id="sortableList"> <li draggable="true"> <span class="item-text">Finalize project report</span> </li> <li draggable="true"> <span class="item-text">Design new logo concept</span> </li> <li draggable="true"> <span class="item-text">Prepare marketing email</span> </li> <li draggable="true"> <span class="item-text">Update pricing page</span> </li> </ul> </div>
body { margin: 0; height: 100vh; background: #f8fafc; font-family: 'Segoe UI', sans-serif; display: flex; justify-content: center; align-items: center; padding: 100px 0; } .wrapper { max-width: 340px; width: 100%; background: #fff; border-radius: 12px; box-shadow: 0 10px 25px rgba(0, 0, 0, 0.06); padding: 0 25px 25px 25px; } h2 { text-align: center; margin-bottom: 20px; color: #1e293b; } ul.list { list-style: none; padding: 0; margin: 0; } .list li { padding: 14px 16px; margin-bottom: 10px; background: #f9fafb; border: 1px solid #e2e8f0; border-radius: 8px; cursor: grab; transition: background 0.2s ease, box-shadow 0.2s ease; position: relative; } .list li:last-child { margin-bottom: 0; } .list li:hover { background: #f1f5f9; } .list li.dragging { opacity: 0.6; box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1); background: #e0f2fe; } .list li.drop-zone { border: 2px dashed #3b82f6; background: #eff6ff; }
const list = document.getElementById("sortableList"); let draggingEl = null; Array.from(list.children).forEach(item => { item.addEventListener("dragstart", () => { draggingEl = item; item.classList.add("dragging"); }); item.addEventListener("dragend", () => { draggingEl = null; item.classList.remove("dragging"); Array.from(list.children).forEach(i => i.classList.remove("drop-zone")); }); item.addEventListener("dragover", e => { e.preventDefault(); if (item !== draggingEl) { const isAbove = e.clientY < item.getBoundingClientRect().top + item.offsetHeight / 2; list.insertBefore(draggingEl, isAbove ? item : item.nextSibling); Array.from(list.children).forEach(i => i.classList.remove("drop-zone")); item.classList.add("drop-zone"); } }); item.addEventListener("dragleave", () => { item.classList.remove("drop-zone"); }); item.addEventListener("drop", e => { e.preventDefault(); item.classList.remove("drop-zone"); }); });
Ad #1
Ad #2
Scroll to Top