jquery插件实现扫雷游戏(1)

本文实例为大家分享了jquery插件实现扫雷游戏第一篇的具体代码,供大家参考,具体内容如下

做一个扫雷

第一部分完成绘制和点击动作

效果如下

jquery插件实现扫雷游戏(1)_第1张图片

代码部分

* {
 margin: 0px;
 padding: 0px;
 font-size: 12px;
}

#div {
 position: fixed;
 top: 10px;
 bottom: 10px;
 left: 10px;
 right: 10px;
 border: 1px solid lightgray;
 border-radius: 5px;
 display: flex;
 justify-content: center;
 align-items: center;
 overflow: hidden;
}

#box {
 border: 1px solid lightgray;
 border-radius: 5px;
}

.row {
 white-space: nowrap;
 height: 30px;
}

.item {
 display: inline-flex;
 justify-content: center;
 align-items: center;
 height: 30px;
 width: 30px;
 border-right: 1px solid lightgray;
 border-bottom: 1px solid lightgray;
 cursor: pointer;
 position: relative;
}
.item::before{
 position: absolute;
 content: '';
 top: 0.5px;
 left:0.5px;
 bottom: 0.5px;
 right: 0.5px;
 background-color: gray;
}
.item.click::before{
 content: none;
}
.item:hover{
 outline: 1px solid #2c3e50;
}

#menu {
 border-bottom: 1px solid lightgray;
 position: absolute;
 top: 0;
 left: 0;
 right: 0;
 height: 30px;
 display: flex;
 background-color: white;
}
.mitem{
 flex: 1;
 display: flex;
 justify-content: center;
 align-items: center;
}
.sl{
 border: none;
 border-bottom: 1px solid lightgray;
 outline: none;
 width: 60%;
 height: 80%;
}
.btn{
 border: none;
 border: 1px solid lightgray;
 outline: none;
 width: 60%;
 height: 80%;
 background-color: transparent;
 cursor: pointer;
}
.mitem *:hover{
 background-color: lightgray;
}


 
  
  做一个扫雷
  
  
  
 
 
  
$(document).ready(function() {
 var x = 10; //x轴
 var y = 10; //y轴
 var c = 10; //雷数
 var boom = []; //产生炸弹的坐标
 var $menu = $("#menu");
 var $box = $("#box");



 //同步参数
 $("#x").change(function() {
  x = parseInt($(this).val());
  console.log(x);
 })
 $("#y").change(function() {
  y = parseInt($(this).val());
 })
 $("#c").change(function() {
  c = parseInt($(this).val());
 })
 $(document).on('click', '#box .item', function() {
  $(this).addClass("click");
 })
 $("#pro").click(function() {
  console.log(x,y,c)
  draw();
 })
 draw();
 function draw() { //绘制图片
  $box.html('');
  for (var a = 0; a < x; a++) {
   var $row = $("
"); for (var b = 0; b < y; b++) { var $item = $("
"); $item.appendTo($row); } $row.appendTo($box); } } })

思路解释

  • 首先就是参数的产生和内容的绘制,这些很容易做到
  • 然后要做好准备的就是给每一个块状标记上坐标,方便后续的计算,直接操作
  • 然后点击的效果通过伪类来实现,没点的时候伪类生成覆盖的遮罩,点完之后去掉就行了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(jquery插件实现扫雷游戏(1))