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

Title

* {

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;

}

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}}

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('失败')

}

}

});

})

相关推荐:

html九宫格实现人像拼图游戏,实例分享jQuery+vue.js实现的九宫格拼图游戏相关推荐

  1. Java多线程游戏实例分享2-雷火(手把手教你做个超炫酷的星际争霸)

    观前提示: 本文涉及的小成果特别多,即使你不需要写一个和我完全相同的游戏,也可以按照需要查看某些特定功能的实现过程,说不定能够给您的程序开发带来一点小小的启发!PS:结合源代码阅读此博客更加高效. 本 ...

  2. 使用Unity3D 自主实战开发的赛车游戏实例,关键点记录 (一)之赛车游戏总体介绍

    我之前一直在做C# 的Winform多媒体软件开发.从去年十一月份开始转型自学Unity3D.发现对这个领域.这个方向更为喜爱.在经过两个多月的跟书自学.跟游戏实例教程练习之后,决定自己完成一款游戏, ...

  3. 有什么好用的拼图方法?分享几种简单好用的拼图工具

    有什么好用的拼图方法呢?当我们在旅行时,会拍摄很多照片,我们可以将旅行照片拼接在一起,展示旅行中的种种经历和风景:或者将家庭成员的照片拼在一起,创造一个美好的家庭回忆.不仅可以增加照片的趣味性和吸引力 ...

  4. 分享:Vue.js新手入门指南-0518-v1.0张雅慧

    最近在逛各大网站,论坛,以及像SegmentFault等编程问答社区,发现Vue.js异常火爆,重复性的提问和内容也很多,楼主自己也趁着这个大前端的热潮,着手学习了一段时间的Vue.js,目前用它正在 ...

  5. [Python教程入门教程]Python五子棋小游戏实例分享

    本文实例为大家分享了Python实现五子棋小游戏的具体代码,供大家参考,具体内容如下 使用的库 pygame.pyautogui 流程简述 1.画棋盘 设置网格间隔40px ,留白 80 px ,与网 ...

  6. vue.js:利用vue.js做一个抽奖小游戏

    MVVM模式是什么:M=Model(模型),V=View(视图),VM= ViewModel(简写成MVVM) . 代码如下: 运行代码结果: 1.你没有中奖: 2.恭喜你,你中奖了: 转载于:htt ...

  7. 分享:Vue.js新手入门指南-0518-v1.0张雅慧(续)

    11.我在学习Vue.js的时候老是听到Webpack,这是啥? Webpack是一个前端打包和构建工具.如果你之前一直是手写HTML,CSS,JavaScript,并且通过link标签将CSS引入你 ...

  8. html5 3d游戏引擎演示,Top 10:HTML5、JavaScript 3D游戏引擎和框架

    由于很多人都在用JavaScript.HTML5和WebGL技术创建基于浏览器的3D游戏,所有JavaScript 3D游戏引擎是一个人们主题.基于浏览器的游戏最棒的地方是平台独立,它们能在iOS.A ...

  9. iPhone游戏编程实例:分享成功游戏开发人员的锦囊妙计

    iPhone游戏编程实例:分享成功游戏开发人员的锦囊妙计 基本信息 原书名: iPhone Games Projects 原出版社: Apress 作者: (美)Dave Mark    PJ Cab ...

  10. Java多线程游戏仿真实例分享

    这是一篇学习分享博客,这篇博客将会介绍以下几项内容: 1.如何让一个程序同时做多件事?(多线程的创建.多线程的应用) 2.如何让小球在画面中真实地动起来?(赋予小球匀速直线.自由落体.上抛等向量运动) ...

最新文章

  1. for each .. in ,for ... in , for ... of的用法
  2. 20162304 实验三
  3. mybatis一套完整入门教程
  4. python tkinter怎么读_Tkinter GUI与阅读系列
  5. 小龙多功能工具箱,邮件群发微信多开
  6. htmlentities在mysql_PHP和mySQL:何时确切使用htmlentities?
  7. 基于JAVA+SpringMVC+Mybatis+MYSQL的bbs论坛管理系统
  8. java中的step_Java中finalize()
  9. linux c++ 函数效率,Linux C++程序进行性能分析工具gprof使用入门
  10. idea 新建spring clound 项目_手把手教你spring源码搭建
  11. 计算机,通信职称考试,2017年通信工程师考试科目介绍
  12. 计算机结构系统的发展趋势,计算机体系结构的现状及发展趋势.docx
  13. mysql 速度评测_[评测]低配环境下,PostgresQL和Mysql读写性能简单对比(欢迎大家提出Mysql优化意见)...
  14. 微信最新版下载 8.0.6
  15. Android按键之Menu详解
  16. 淘宝R2去模糊化+聚石塔+奇门
  17. 飓风桑迪:曼哈顿数据中心的灾难应急方案
  18. 窗口根据屏幕分辨率自动调整大小
  19. 产品生命周期用户生命周期?
  20. 获取局域网远程主机ipv6地址

热门文章

  1. javaweb小说阅读网站源码
  2. 哈哈哈哈,这个勒索软件笑死我了!
  3. html5的ajax上传图片,html5标准Ajax上传图片
  4. 计算机管理及维护培训考试题,计算机考试题库和答案_浅析高校公共计算机机房管理与维护...
  5. 高德 设置marker zoom_ZOOM云会议下载安装-ZOOM云视频会议最新版v5.3.53292
  6. 基于物品的协同过滤算法实现图书推荐系统
  7. 1-docker安装
  8. JS获取Audio音频的实时时间
  9. 新版百度网盘公测,真不限速了
  10. 高斯光束的简单matlab仿真