原生javascript制作的拼圖游戲?qū)崿F(xiàn)方法詳解
本文實(shí)例講述了原生javascript制作的拼圖游戲?qū)崿F(xiàn)方法。分享給大家供大家參考,具體如下:
實(shí)現(xiàn)方法
//1、讓所有的li(在ul里)可以拖拽
//2、交換li的位置 計(jì)算背景圖位置
//1、讓所有的li(在ul里)可以拖拽
//根據(jù)鼠標(biāo)的位置,計(jì)算目標(biāo)li的序號(hào)//根據(jù)行號(hào)和列號(hào)計(jì)算下標(biāo)
//行號(hào)*3+列號(hào)
//2、歸位
此處沒(méi)有背景圖 請(qǐng)自行添加 css樣式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> html,body{ margin:0; padding:0; } #box{ list-style:none; position:relative; width:600px; height:600px; box-sizing:border-box; margin:10px auto; } li{ position:absolute; width:200px; height:200px; border:1px solid white; background-image:url(img/b1.jpg); background-size:600px 600px; } #box li:nth-child(1){ left:0px; top:0px; background-position:0px 0px; } #box li:nth-child(2){ left:200px; top:0px; background-position:-200px 0px; } #box li:nth-child(3){ left:400px; top:0px; background-position:-400px 0px; } #box li:nth-child(4){ left:0px; top:200px; background-position:0px -200px; } #box li:nth-child(5){ left:200px; top:200px; background-position:-200px -200px; } #box li:nth-child(6){ left:400px; top:200px; background-position:-400px -200px; } #box li:nth-child(7){ left:0px; top:400px; background-position:0px -400px; } #box li:nth-child(8){ left:200px; top:400px; background-position:-200px -400px; } #box li:nth-child(9){ left:400px; top:400px; background-position:-400px -400px; } </style> </head> <body> <ul id="box"> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> </body> </html>
<script type="text/javascript" src="js/cssTools.js"></script> <script type="text/javascript" src="js/eventTools.js"></script>
這個(gè)是這連個(gè)js連接的代碼
//csstools //功能:獲取某個(gè)DOM元素的樣式屬性的兼容性寫(xiě)法 //參數(shù):dom元素,樣式屬性名 //返回值:樣式屬性的值 function getStyle(domObj,attr){ if(domObj.currentStyle){//domObj.currentStyle如果能夠正確獲取到,那就真 return domObj.currentStyle[attr];//當(dāng)對(duì)象的屬性名是變量時(shí),用方括號(hào)而不是點(diǎn)。 }else{ return window.getComputedStyle(domObj)[attr]; } }
//eventTools //功能:阻止瀏覽器默認(rèn)行為的封裝 //參數(shù):事件對(duì)象 //返回值:無(wú) function preventDefault1809(evt) { if(evt.returnValue){ evt.returnValue = false; }else{ evt.preventDefault(); } } //功能:綁定事件 //參數(shù): //事件源 //事件類(lèi)型名,不帶on //事件處理函數(shù), //是否冒泡 //返回值:無(wú) function addEvent1809(domObj,eventType,func,isBubble){ if(domObj.addEventListener){ domObj.addEventListener(eventType,func,isBubble); }else if(domObj.attachEvent){ domObj.attachEvent('on'+eventType,func); }else{ domObj['on'+eventType] = func; } } //當(dāng)對(duì)象的屬性是變量時(shí),不能用點(diǎn),只能用方括號(hào) /* var obj = { id:'007' } obj.id; var temp = "id"; obj[temp] */
js部分
<script type="text/javascript"> function $(id){ return document.getElementById(id); } window.onload = function(){ drag(); } //1、讓所有的li(在ul里)可以拖拽 function drag(){ var lis = $("box").children; var currIndex = -1;//記錄被按下的那個(gè)li var targetIndex = -1; for(var i=0;i<lis.length;i++){ lis[i].setAttribute("index",i); lis[i].onmousedown = function(event){ currIndex = this.getAttribute("index"); var evt = event || window.event; var offsetX = evt.offsetX; var offsetY = evt.offsetY; this.style.zIndex = 1; var liDom = this; $("box").onmousemove = function(event){ var evt = event || window.event; //1、數(shù)據(jù)距離大盒子左上角的距離 var mouseX = evt.pageX-$("box").offsetLeft; var mouseY = evt.pageY-$("box").offsetTop; //鼠標(biāo)距離頁(yè)面左邊的距離- 大盒子距離頁(yè)面左邊的距離-鼠標(biāo)距離事件源的左邊距離 var left1 = mouseX-offsetX; var top1 = mouseY-offsetY; //li不能拖拽到界外(大盒子外面) if(left1<0 || left1>600-200 || top1<0 || top1>600-200 ){ return; } liDom.style.left = left1+"px"; liDom.style.top = top1+"px"; targetIndex = getTargetIndex(mouseX,mouseY); console.log(targetIndex); } } document.body.onmouseup = function(){ $("box").onmousemove = null; if(currIndex>-1){ lis[currIndex].style.zIndex = 0; exchangeLi(currIndex,targetIndex); } } } } //根據(jù)鼠標(biāo)的位置,計(jì)算目標(biāo)li的序號(hào) function getTargetIndex(x,y){ //計(jì)算行號(hào) var rowIndex = parseInt(y/200);// //計(jì)算列號(hào) var colIndex = parseInt(x/200);// //根據(jù)行號(hào)和列號(hào)計(jì)算下標(biāo) //行號(hào)*3+列號(hào) return rowIndex*3+colIndex; } function exchangeLi(sourceIndex,targetIndex){ // var lis = $("box").children; // if(sourceIndex<-1 || sourceIndex>lis.length-1 || targetIndex<-1 || targetIndex>lis.length-1){ // return; // } if(sourceIndex!=targetIndex){ var lis = $("box").children; //1、交換backgroundPosition var temp =getStyle(lis[sourceIndex],"backgroundPosition"); lis[sourceIndex].style.backgroundPosition = getStyle(lis[targetIndex],"backgroundPosition"); lis[targetIndex].style.backgroundPosition = temp; } //2、歸位 rowIndex = parseInt(sourceIndex/3); colIndex = sourceIndex%3; lis[sourceIndex].style.left = colIndex*200+"px"; lis[sourceIndex].style.top = rowIndex*200+"px"; } </script>
PS:這里給大家推薦一款相似的在線工具供大家參考:
在線美女拼圖游戲:
http://tools.jb51.net/games/pintu
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript數(shù)組操作技巧總結(jié)》、《JavaScript排序算法總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- JS快速實(shí)現(xiàn)移動(dòng)端拼圖游戲
- JS 拼圖游戲 面向?qū)ο?,注釋完整?/a>
- js+html5實(shí)現(xiàn)可在手機(jī)上玩的拼圖游戲
- 基于javascript制作經(jīng)典傳統(tǒng)的拼圖游戲
- jQuery+vue.js實(shí)現(xiàn)的九宮格拼圖游戲完整實(shí)例【附源碼下載】
- 基于Vue.js實(shí)現(xiàn)數(shù)字拼圖游戲
- 利用原生的JavaScript實(shí)現(xiàn)簡(jiǎn)單拼圖游戲
- javascript結(jié)合Flexbox簡(jiǎn)單實(shí)現(xiàn)滑動(dòng)拼圖游戲
- JS實(shí)現(xiàn)拼圖游戲
- 基于JS實(shí)現(xiàn)簡(jiǎn)單滑塊拼圖游戲
相關(guān)文章
JS開(kāi)發(fā)中百度地圖+城市聯(lián)動(dòng)實(shí)現(xiàn)實(shí)時(shí)觸發(fā)查詢地址功能
這篇文章主要介紹了JS開(kāi)發(fā)中百度地圖+城市聯(lián)動(dòng)實(shí)現(xiàn)實(shí)時(shí)觸發(fā)查詢地址功能,需要的朋友可以參考下2017-04-04javascript和jQuery中的AJAX技術(shù)詳解【包含AJAX各種跨域技術(shù)】
這篇文章主要介紹了javascript和jQuery中的AJAX技術(shù),結(jié)合實(shí)例形式分析了javascript與jQuery中ajax的實(shí)現(xiàn)方法以及AJAX各種跨域技術(shù)的原理與操作技巧,需要的朋友可以參考下2016-12-12JS簡(jiǎn)單判斷滾動(dòng)條的滾動(dòng)方向?qū)崿F(xiàn)方法
這篇文章主要介紹了JS簡(jiǎn)單判斷滾動(dòng)條的滾動(dòng)方向?qū)崿F(xiàn)方法,涉及javascript針對(duì)scrollTop事件的相關(guān)操作技巧,需要的朋友可以參考下2017-04-04鼠標(biāo)懸浮停留三秒后自動(dòng)顯示大圖js代碼
這篇文章主要介紹了鼠標(biāo)懸浮停留三秒后顯示大圖,在網(wǎng)頁(yè)中還是比較實(shí)用的,下面是示例代碼2014-09-09javascript輸入CD-KEY自動(dòng)分割的代碼
開(kāi)發(fā)過(guò)程中用寫(xiě)的一個(gè)腳本,記錄下來(lái)以備后用與他用,其中attributes["max"].nodeValue是取HTML自定義的 max屬性(兼容Firefox和IE)2010-10-10