具體程式碼如下所示:
<!--程式碼如下,最下面給了我測試用的9張250*250的圖片切片--><!DOCTYPE html><html><head> <meta charset=utf-8> <title>drag拖曳</title> <style> .box{ float: left; } img{ width: 150px; height:150px; } #puzzle{ font-size: 0; margin:80px auto; padding: 5px; width: 460px; } </style></head><body> <div id=puzzle> <div class=box><img <div class=box ><img <div class=box><img <div class=box><img <div class=box><img <div class=box><img <div class=box><img <div class=box><img <div class=box><img </div> <script> var image = document.getElementsByTagName(img); var box = document.getElementsByClassName(box); image.draggable = true; var source = ; var nowImage; var nowImageBox; var thenImage; for(let i=0;i<image.length;i++){ source = picture+i+.jpg; image[i].setAttribute(src,source); image[i].onmousedown = function(Attribute(src,source); image[i].onmousedown = function( ){ nowImage = this; nowImageBox = this.parentNode; } box[i].ondragover = function(event){ event.preventDefault(); //去除ondragover事件的預設行為,該行為預設無法將資料或元素放置到其他元素} box[i].ondrop = function(event){ thenImage = box[ i].childNodes[0]; box[i].appendChild(nowImage); nowImageBox.appendChild(thenImage); } } </script></body></html>總結
以上所述是小編給大家介紹的HTML5拖曳功能實現的拼圖遊戲,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對VeVb武林網站的支持!