手机版
你好,游客 登录 注册
背景:
阅读新闻

使用HTML5的drag&drop做一个数独游戏

[日期:2012-03-19] 来源:Linux社区  作者:shyleoking [字体:大 中 小]

第三步:我们拖动数字之后,希望可以填数字的区域有明显的颜色变化并给出提示,同时固定区域不可以拖进去,其他区域可以拖进去,并且拖动的时候要send值。

有了前面的知识,我们马上知道去哪里改事件控制了:dragstart事件

  1.  $(  
  2. function() {  
  3.     [{ number: 1, bgcolor: "#C71585" }, { number: 2, bgcolor: "#800080" }, { number: 3, bgcolor: "#B8860B" },  
  4.     { number: 4, bgcolor: "rgb(0,0,128)" }, { number: 5, bgcolor: "rgb(30,144,255)" },  
  5.     { number: 6, bgcolor: "rgb(255,165,0)" },  
  6.      { number: 7, bgcolor: "hsl(0,75%,50%)" }, { number: 8, bgcolor: "hsl(30,50%,50%)" },  
  7.      { number: 9, bgcolor: "hsl(120,75%,38%)"}].forEach(  
  8.      function(key, index) {  
  9.          //$.log(key);   
  10.          var li = $("<li>").html(key.number).css("backgroundColor", key.bgcolor).attr("draggable", "true");  
  11.          //$.log(li);   
  12.          li[0].addEventListener("dragstart", function(e) {  
  13.              e.dataTransfer.effectAllowed = "copyMove";  
  14.              e.dataTransfer.setData("text/plain", this.innerHTML);  
  15.              $.log(this.innerHTML);  
  16.              [ ].forEach.call(document.querySelectorAll("#player .default"),  
  17.          function(item) {  
  18.              //$.log(item);   
  19.              item.classList.remove("default");  
  20.              item.classList.add("ation");  
  21.          });  
  22.          }, false);  
  23.   
  24.          li[0].addEventListener("dragend", function() {  
  25.              [ ].forEach.call(document.querySelectorAll("#player .ation"),  
  26.          function(item) {  
  27.              item.classList.remove("ation");  
  28.              item.classList.add("default");  
  29.          });  
  30.          }, false);  
  31.   
  32.          $("#numberBox").append(li);  
  33.      }  
  34.      );  
  35. }  
  36. );  
现在你可以测试下了,当你拖动数字的时候,有明显的颜色改变,并且不同的区域你的鼠标样式也不同哦。

第四步,我们接受值,并且判断这个值是否存在行列冲突,如果冲突就提示,否则改写

  1.  $(  
  2. function() {  
  3.     "500000300090500400004000700051037289302080604008052137035000900609000823080023006".split("").forEach(  
  4.     function(item, index) {  
  5.         $.log(item);  
  6.         var li = $("<li>")  
  7.         if (item != "0") {  
  8.             li.addClass("fix");  
  9.             li[0].innerHTML = item;  
  10.         }  
  11.         else {  
  12.             li[0].classList.add("default");  
  13.             li[0].addEventListener("dragenter",  
  14.             function(e) {  
  15.                 $.log(e);  
  16.             }, false);  
  17.   
  18.             li[0].addEventListener("dragover",  
  19.             function(e) {  
  20.                 if (e.preventDefault) {  
  21.                     e.preventDefault(); //不要执行与事件关联的默认动作   
  22.                 }  
  23.                 if (e.stopPropagation) {  
  24.                     e.stopPropagation(); //停止事件的传播   
  25.                 }  
  26.                 $.log(e);  
  27.                 return false;  
  28.             }, false);  
  29.   
  30.             li[0].addEventListener("dragleave",  
  31.             function(e) {  
  32.             }, false);  
  33.   
  34.             li[0].addEventListener("drop",  
  35.             function(e) {  
  36.                 if (e.preventDefault) {  
  37.                     e.preventDefault(); //不要执行与事件关联的默认动作   
  38.                 }  
  39.                 if (e.stopPropagation) {  
  40.                     e.stopPropagation(); //停止事件的传播   
  41.                 }  
  42.   
  43.                 var sendData = e.dataTransfer.getData("text/plain");  
  44.                 //获得#player>li矩阵数组   
  45.                 var matrix = Array.prototype.slice.call(document.querySelectorAll("#player>li"));   
  46.                 var currIndex = matrix.indexOf(this); //获得当前元素的位置   
  47.                 var rowIndex = currIndex - currIndex % 9; //行开始的位置   
  48.                 var colIndex = currIndex % 9//列开始的位置   
  49.                 for (var i = rowIndex; i < rowIndex + 9; i++) {  
  50.                     if (i != currIndex && matrix[i].innerHTML == sendData) {  
  51.                         alert("对不起行上有数据重复,请小心哦!亲");  
  52.                         return;  
  53.                     }  
  54.                 }  
  55.                 for (var i = colIndex; i < 81; i = i + 9) {  
  56.                     if (i != currIndex && matrix[i].innerHTML == sendData) {  
  57.                         alert("对不起列上有数据重复,请小心哦!亲");  
  58.                         return;  
  59.                     }  
  60.                 }  
  61.                 this.innerHTML = sendData;  
  62.   
  63.             }, false);  
  64.         }  
  65.         $("#player").append(li);  
  66.     }  
  67.     );  
  68. }  
  69. ); 
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款