亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

原生javascript制作的拼圖游戲?qū)崿F(xiàn)方法詳解

 更新時(shí)間:2020年02月23日 12:12:04   作者:巴啦啦小能量  
這篇文章主要介紹了原生javascript制作的拼圖游戲?qū)崿F(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了JavaScript制作拼圖游戲的相關(guān)步驟、原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

最新評(píng)論