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正则表达式在页面验证url网址输入是否正确
Apr 04 jQuery
jquery实现图片放大点击切换
Jun 06 jQuery
jQuery中extend函数简单用法示例
Oct 11 jQuery
如何快速解决JS或Jquery ajax异步跨域的问题
Jan 08 jQuery
jQuery实现表单动态添加与删除数据操作示例
Jul 03 jQuery
基于jquery实现九宫格拼图小游戏
Nov 30 jQuery
jQuery实现的五星点评功能【案例】
Feb 18 jQuery
jQuery分组选择器简单用法示例
Apr 04 jQuery
jQuery实现的分页插件完整示例
May 26 jQuery
jQuery实现简单日历效果
Jul 05 jQuery
jquery简易手风琴插件的封装
Oct 13 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简单系统数据添加以及数据删除模块源文件下载
2008/06/07 PHP
ecshop 批量上传(加入自定义属性)
2012/03/20 PHP
PHP环境中Memcache的安装和使用
2015/11/05 PHP
PHP面向对象程序设计中的self、static、parent关键字用法分析
2019/08/14 PHP
CL vs ForZe BO5 第一场 2.13
2021/03/10 DOTA
利用jQuery实现可输入搜索文字的下拉框
2013/10/23 Javascript
jQuery-ui引入后Vs2008的无智能提示问题解决方法
2014/02/10 Javascript
浅析javascript中function 的 length 属性
2014/05/27 Javascript
动态创建按钮的JavaScript代码
2016/01/29 Javascript
js 求时间差的实现代码
2016/04/26 Javascript
js获取腾讯视频ID的方法
2016/10/03 Javascript
关于Vue Webpack2单元测试示例详解
2017/08/14 Javascript
Bootstrap table使用方法记录
2017/08/23 Javascript
webpack引入eslint配置详解
2018/01/22 Javascript
vue-cli3自动消除console.log()的调试信息方式
2020/10/21 Javascript
python操作xml文件示例
2014/04/07 Python
Python实现LRU算法的2种方法
2015/06/24 Python
Python简单网络编程示例【客户端与服务端】
2017/05/26 Python
python中文乱码不着急,先看懂字节和字符
2017/12/20 Python
Numpy中的mask的使用
2018/07/21 Python
python实现网页自动签到功能
2019/01/21 Python
使用matplotlib中scatter方法画散点图
2019/03/19 Python
利用python-pypcap抓取带VLAN标签的数据包方法
2019/07/23 Python
django项目用higcharts统计最近七天文章点击量
2019/08/17 Python
HTML5 Canvas实现玫瑰曲线和心形图案的代码实例
2014/04/10 HTML / CSS
自荐信格式范文
2013/10/07 职场文书
安全检查验收制度
2014/01/12 职场文书
行政执法作风整顿剖析材料
2014/10/11 职场文书
党员三严三实对照检查材料
2014/10/13 职场文书
民主评议党员登记表自我评价
2014/10/20 职场文书
离职信范本
2015/06/23 职场文书
如何解决.cuda()加载用时很长的问题
2021/05/24 Python
Mysql数据库值的添加、修改、删除及清空操作实例
2021/06/20 MySQL
Java 在线考试云平台的实现
2021/11/23 Java/Android
海贼王十大逆天果实 魂魂果实上榜,岩浆果实攻击力最强
2022/03/18 日漫
vue项目proxyTable配置和部署服务器
2022/04/14 Vue.js