HTML5 draggable实现元素拖放 Posted on 2019-09-03 | 个浏览 draggable 属性是 HTML5 中的新属性,规定元素是否可拖动。 draggable=true 配合 ondrag & ondrop 事件可实现拖拽上传、拖拽删除、图片拖拽排序等等 在线demo 🌵 拖拽效果简单示例12345678910111213141516171819202122232425262728293031323334353637383940414243<div> <div class="box"><span draggable="true">0</span></div> <div class="box"><span draggable="true">1</span></div> <div class="box"><span draggable="true">2</span></div> <div class="box"><span draggable="true">3</span></div> <div class="box"><span draggable="true">4</span></div> <div class="box"><span draggable="true">5</span></div> <div class="box"><span draggable="true">6</span></div> <div class="box"><span draggable="true">7</span></div> <div class="box"><span draggable="true">8</span></div></div><script type="text/javascript"> var item = document.getElementsByTagName("span"); var box = document.getElementById("box"); var nowItem, nowItemBox, thenItem, s = 0; for (let i = 0; i < item.length; i++) { item[i].onmousedown = function () { nowItem = this; nowItemBox = this.parentNode; } box[i].ondragover = function (event) { event.preventDefault(); } box[i].ondrop = function (event) { thenItem = box[i].childNodes[0]; box[i].appendChild(nowItem); nowItemBox.appendChild(thenItem); if (item[i].outerText == i) { s += 1; } if (s >= item.length -2 && Number(item[item.length - 1].outerText) == item.length - 1) { setTimeout(function () { alert("Success!"); }, 200) } } }</script> 📜 参考文章: 自制html5拖拽功能实现的拼图游戏