登录  /  注册
首页 > web前端 > js教程 > 正文

实例分享jQuery+vue.js实现的九宫格拼图游戏

小云云
发布: 2017-12-29 09:29:36
原创
2736人浏览过

本文主要介绍了jquery+vue.js实现的九宫格拼图游戏,结合完整实例形式分析了jquery结合vue.js针对图片的相关操作技巧,需要的朋友可以参考下,希望能帮助到大家。


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    /*#piclist {
      width: 600px;
      height: 600px;
      background-color: green;
    }*/
    .nitem {
      /*width: 200px;*/
      /*height: 200px;*/
      float: left;
      background: url(img/meinv.jpg) 0px 0px no-repeat;
      -webkit-background-size: 600px 600px;
      background-size: 600px 600px;
      font-size: 33px;
      color: red;
      font-weight: bold;
      cursor: pointer;
    }
    /*.nitem:nth-child(2){
      background-position: -200px 0;
    }
    .nitem:nth-child(3){
      background-position: -400px 0;
    }
    .nitem:nth-child(4){
      background-position: 0px -200px;
    }
    .nitem:nth-child(5){
      background-position: -200px -200px;
    }
    .nitem:nth-child(6){
      background-position: -400px -200px;
    }
    .nitem:nth-child(7){
      background-position: 0px -400px;
    }
    .nitem:nth-child(8){
      background-position: -200px -400px;
    }
    .nitem:nth-child(9){
      background-position: -400px -400px;
    }*/
    .fn-clear {
      clear: both;
      height: 0;
      line-height: 0px;
      font-size: 0px;
    }
  </style>
</head>
<body>
<p id="appbox" :style="{ width:width+&#39;px&#39;,height:height+&#39;px&#39; }">
  <p id="piclist">
    <p class="nitem"
       v-for="(item,index) in itemlist"
       :class="{remove : index === 0}"
       :index="index "
       v-bind:style="{
        &#39;backgroundPosition&#39;:-px(index)+&#39;px -&#39; + py(index) + &#39;px&#39;,
         width : width / rows + &#39;px&#39;,
         height : height / cols + &#39;px&#39;}">{{index}}
    </p>
  </p>
</p>
</body>
<script src=js/jquery-1.9.1.min.js></script>
<script src=js/vue.min.js></script>
<script>
  var olen = 0, oi = 0, cindex = 0, oa = new Array(), oindex = 0, listarray = new Array();
  var vm = new Vue({
    el: "#appbox",
    data: {
      itemlist: [],
      rows: 3,
      cols: 3,
      width: 600,
      height: 600,
    },
    methods: {
      px(index){
        return (index % this.rows) * (this.width / this.rows)
      },
      py (index){
        return parseInt((index / this.cols)) * (this.height / this.cols);
      }
    }
  });
  for (var i = 0; i < vm.rows * vm.cols; i++) {
    vm.itemlist.push(i);
  }
  function getrow(index) {
    return parseInt(index / vm.cols);
  }
  function getcols(index) {
    return index % vm.rows;
  }
  function getBound(index) {
    var left = index - 1;
    var right = index + 1;
    var top = index - vm.rows;
    var bottom = index + vm.rows;
    var len = vm.itemlist.length; //总长度
    var currentRow = getrow(index);
    var currentCol = getcols(index);
    var roundArr = new Array();
    if (left >= 0 && left < len && getrow(left) == currentRow) {
      roundArr.push(left);
    }
    if (right >= 0 && right < len && getrow(right) == currentRow) {
      roundArr.push(right);
    }
    if (top >= 0 && top < len && getcols(top) == currentCol) {
      roundArr.push(top);
    }
    if (bottom >= 0 && bottom < len && getcols(bottom) == currentCol) {
      roundArr.push(bottom);
    }
    return roundArr;
  }
  function box_switch(i, j) {
    var iobj = $(&#39;#piclist .nitem&#39;).eq(i);
    var jobj = $(&#39;#piclist .nitem&#39;).eq(j);
    var tobj = iobj.clone();
    jobj.after(tobj);
    iobj.replaceWith(jobj);
  }
  vm.$nextTick(function () {
    $(&#39;.remove&#39;).css({
      background: &#39;none&#39;,
      backgroundColor: &#39;green&#39;
    });
  });
  function box_rand(times) {
    for (var i = 0; i < times; i++) {
      oindex = $(&#39;.remove&#39;).index();
      oa = getBound(oindex);
      olen = oa.length;
      oi = Math.floor(Math.random() * olen);
      cindex = oa[oi];
      box_switch(cindex, oindex);
    }
    listarray.length = 0;
    $.each($(&#39;.nitem&#39;), function (i, item) {
      listarray.push($(item).attr(&#39;index&#39;));
    });
    if (listarray.join(&#39;,&#39;) == vm.itemlist.join(&#39;,&#39;)) {
      box_rand(times);
    }
  }
  $(function () {
    //打乱图片
    box_rand(20);
    $(&#39;.nitem&#39;).on(&#39;click  &#39;, function () {
      var cindex = $(this).index();
      var oindex = $(&#39;.remove&#39;).index();
      var oRound = getBound(oindex); //空盒子四周的索引
      if ($.inArray(cindex, oRound) < 0) { //不在
        return false;
      } else {
        box_switch(oindex, cindex);
        var listArray = new Array();
        $.each($(&#39;.nitem&#39;), function (i, item) {
          listArray.push($(item).attr(&#39;index&#39;));
        })
        if (listArray.join(&#39;,&#39;) == vm.itemlist.join(&#39;,&#39;)) {
          alert(&#39;success&#39;)
        } else {
          console.log(&#39;失败&#39;)
        }
      }
    });
  })
</script>
</html>
登录后复制

相关推荐:

实例详解基于vue组件实现猜数字游戏

html5制作吃月饼小游戏教程

如何用js开发实现简单贪吃蛇游戏

以上就是实例分享jQuery+vue.js实现的九宫格拼图游戏的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号