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插件FusionWidgets实现的Bulb图效果示例【附demo源码下载】
Mar 23 jQuery
基于jQuery实现文字打印动态效果
Apr 21 jQuery
jQuery扇形定时器插件pietimer使用方法详解
Jul 18 jQuery
使用jQuery实现页面定时弹出广告效果
Aug 24 jQuery
jquery中done和then的区别(详解)
Dec 19 jQuery
jQuery简单判断值是否存在于数组中的方法示例
Apr 17 jQuery
jQuery获取随机颜色的实例代码
May 21 jQuery
jQuery插件jsonview展示json数据
May 26 jQuery
详解如何使用webpack打包多页jquery项目
Feb 01 jQuery
JQuery获取可视区尺寸和文档尺寸及制作悬浮菜单示例
May 14 jQuery
jQuery 选择方法及$(this)用法实例分析
May 19 jQuery
jQuery实现评论模块
Aug 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&amp;mysql(二)
2006/10/09 PHP
PHP 字符串分割和比较
2009/10/06 PHP
Yii2实现自定义独立验证器的方法
2017/05/05 PHP
使用PHP json_decode可能遇到的坑与解决方法
2017/08/03 PHP
PHP实现用session来实现记录用户登陆信息
2018/10/15 PHP
用JAVASCRIPT如何给&amp;lt;textarea&amp;gt;&amp;lt;/textarea&amp;gt;赋值
2007/04/20 Javascript
jQuery+canvas实现简单的球体斜抛及颜色动态变换效果
2016/01/28 Javascript
三种AngularJS中获取数据源的方式
2016/02/02 Javascript
Vue父子模版传值及组件传值的三种方法
2017/11/27 Javascript
除Console.log()外更多的Javascript调试命令
2018/01/24 Javascript
vue多页面开发和打包正确处理方法
2018/04/20 Javascript
layer弹出层 iframe层去掉滚动条的实例代码
2018/08/17 Javascript
小程序登录态管理的方法示例
2018/11/13 Javascript
layer弹出子iframe层父子页面传值的实现方法
2018/11/22 Javascript
vue路由 遍历生成复数router-link的例子
2019/10/30 Javascript
使用nodeJS中的fs模块对文件及目录进行读写,删除,追加,等操作详解
2020/02/06 NodeJs
在vscode 中设置 vue模板内容的方法
2020/09/02 Javascript
微信小程序实现点赞业务
2021/02/10 Javascript
Python图片裁剪实例代码(如头像裁剪)
2017/06/21 Python
Python使用itertools模块实现排列组合功能示例
2018/07/02 Python
Selenium(Python web测试工具)基本用法详解
2018/08/10 Python
django项目搭建与Session使用详解
2018/10/10 Python
Python hashlib加密模块常用方法解析
2019/12/18 Python
Python如何转换字符串大小写
2020/06/04 Python
matplotlib grid()设置网格线外观的实现
2021/02/22 Python
莫斯科制造商的廉价皮大衣:Fursk
2020/06/09 全球购物
一些网络技术方面的面试题
2014/05/01 面试题
《灯光》教学反思
2014/02/08 职场文书
运动会广播稿20字
2014/02/18 职场文书
教师纪念9.18事件演讲稿范文
2014/09/14 职场文书
《普罗米修斯》教学反思
2016/02/22 职场文书
python实现自定义日志的具体方法
2021/05/28 Python
springboot @ConfigurationProperties和@PropertySource的区别
2021/06/11 Java/Android
Python pyecharts绘制条形图详解
2022/04/02 Python
JS函数式编程实现XDM一
2022/06/16 Javascript
CSS使用SVG实现动态分布的圆环发散路径动画
2022/12/24 HTML / CSS