jQuery+vue.js实现的九宫格拼图游戏完整实例【附源码下载】


Posted in jQuery onSeptember 12, 2017

本文实例讲述了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>
<div id="appbox" :style="{ width:width+'px',height:height+'px' }">
  <div id="piclist">
    <div class="nitem"
       v-for="(item,index) in itemlist"
       :class="{remove : index === 0}"
       :index="index "
       v-bind:style="{
        'backgroundPosition':-px(index)+'px -' + py(index) + 'px',
         width : width / rows + 'px',
         height : height / cols + 'px'}">{{index}}
    </div>
  </div>
</div>
</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 = $('#piclist .nitem').eq(i);
    var jobj = $('#piclist .nitem').eq(j);
    var tobj = iobj.clone();
    jobj.after(tobj);
    iobj.replaceWith(jobj);
  }
  vm.$nextTick(function () {
    $('.remove').css({
      background: 'none',
      backgroundColor: 'green'
    });
  });
  function box_rand(times) {
    for (var i = 0; i < times; i++) {
      oindex = $('.remove').index();
      oa = getBound(oindex);
      olen = oa.length;
      oi = Math.floor(Math.random() * olen);
      cindex = oa[oi];
      box_switch(cindex, oindex);
    }
    listarray.length = 0;
    $.each($('.nitem'), function (i, item) {
      listarray.push($(item).attr('index'));
    });
    if (listarray.join(',') == vm.itemlist.join(',')) {
      box_rand(times);
    }
  }
  $(function () {
    //打乱图片
    box_rand(20);
    $('.nitem').on('click  ', function () {
      var cindex = $(this).index();
      var oindex = $('.remove').index();
      var oRound = getBound(oindex); //空盒子四周的索引
      if ($.inArray(cindex, oRound) < 0) { //不在
        return false;
      } else {
        box_switch(oindex, cindex);
        var listArray = new Array();
        $.each($('.nitem'), function (i, item) {
          listArray.push($(item).attr('index'));
        })
        if (listArray.join(',') == vm.itemlist.join(',')) {
          alert('success')
        } else {
          console.log('失败')
        }
      }
    });
  })
</script>
</html>
jQuery 相关文章推荐
jQuery中的deferred对象和extend方法详解
May 08 jQuery
jQuery常用选择器详解
Jul 17 jQuery
快速掌握jquery分页插件jqPaginator的使用方法
Aug 09 jQuery
jquery ztree实现右键收藏功能
Nov 20 jQuery
jQuery中库的引用方法
Jan 06 jQuery
基于jquery的on和click的区别详解
Jan 15 jQuery
jquery拖拽自动排序插件使用方法详解
Jul 20 jQuery
jquery实现吸顶导航效果
Jan 08 jQuery
jQuery实现手风琴效果(蒙版)
Jan 11 jQuery
Jquery cookie插件实现原理代码解析
Aug 04 jQuery
jquery实现加载更多&quot;转圈圈&quot;效果(示例代码)
Nov 09 jQuery
jQuery实现穿梭框效果
Jan 19 jQuery
jQuery实现用户信息表格的添加和删除功能
Sep 12 #jQuery
解决jquery appaend元素中id绑定事件失效的问题
Sep 12 #jQuery
JS和jQuery通过this获取html标签中的属性值(实例代码)
Sep 11 #jQuery
利用JQuery操作iframe父页面、子页面的元素和方法汇总
Sep 10 #jQuery
jQuery事件对象的属性和方法详解
Sep 09 #jQuery
详解jquery插件jquery.viewport.js学习使用方法
Sep 08 #jQuery
jQuery选择器中的特殊符号处理方法
Sep 08 #jQuery
You might like
php 实现进制转换(二进制、八进制、十六进制)互相转换实现代码
2010/10/22 PHP
一些php项目中比较通用的php自建函数的详解
2013/06/06 PHP
php实现爬取和分析知乎用户数据
2016/01/26 PHP
JavaScript中为元素加上name属性的方法
2011/05/09 Javascript
JavaScript 用Node.js写Shell脚本[译]
2012/09/20 Javascript
JQuery拖拽元素改变大小尺寸实现代码
2012/12/10 Javascript
JavaScript中对循环语句的优化技巧深入探讨
2014/06/06 Javascript
jQuery常用操作方法及常用函数总结
2014/06/19 Javascript
Node.js实现在目录中查找某个字符串及所在文件
2014/09/03 Javascript
微信小程序与php 实现微信支付的简单实例
2017/06/23 Javascript
基于jQuery的左滑出现删除按钮的示例
2017/08/29 jQuery
利用Javascript实现一套自定义事件机制
2017/12/14 Javascript
js实现图片上传并预览功能
2018/08/06 Javascript
node.js中 mysql 增删改查操作及async,await处理实例分析
2020/02/11 Javascript
在antd4.0中Form使用initialValue操作
2020/11/02 Javascript
解决vue-cli输入命令vue ui没效果的问题
2020/11/17 Javascript
实例讲解Python的函数闭包使用中应注意的问题
2016/06/20 Python
使用PyV8在Python爬虫中执行js代码
2017/02/16 Python
Python 获取当前所在目录的方法详解
2017/08/02 Python
Python中的pygal安装和绘制直方图代码分享
2017/12/08 Python
Python基于聚类算法实现密度聚类(DBSCAN)计算【测试可用】
2018/12/26 Python
Pyinstaller打包.py生成.exe的方法和报错总结
2019/04/02 Python
python issubclass 和 isinstance函数
2019/07/25 Python
详解python中的数据类型和控制流
2019/08/08 Python
python数据类型强制转换实例详解
2020/06/22 Python
python如何查看安装了的模块
2020/06/23 Python
CSS3 transforms应用于背景图像的解决方法
2019/04/16 HTML / CSS
白俄罗斯女装和针织品网上商店:Presli.by
2019/10/13 全球购物
探矿工程师自荐信
2014/01/24 职场文书
工程催款通知书
2015/04/17 职场文书
2015年小学二年级班主任工作总结
2015/05/21 职场文书
欢送会主持词
2015/07/01 职场文书
公司致全体员工的感谢信
2019/06/24 职场文书
制定企业培训计划的五大要点!
2019/07/10 职场文书
anaconda python3.8安装后降级
2021/06/11 Python
CSS 鼠标选中文字后改变背景色的实现代码
2023/05/21 HTML / CSS