博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[javascript]9宫格拖拽拼图游戏 puzzle
阅读量:4651 次
发布时间:2019-06-09

本文共 2584 字,大约阅读时间需要 8 分钟。

1.跨浏览器,主流浏览器都可以运行

2.纯javascript开发。

3.配置实用方便。

4.二次开发简单,易用。

 

 

成品预览

 

  用一张图片用坐标的方式生成拼图单元格,类似css sprite。大致思路是,每个单元格有对应的两个索引,由数据驱动显示。创建实例时吧0-9乱序排列,每个单元格对应两个索引,一个是真实所在位置的索引,一个是记录现在位置的索引。拖动图片的时调整记录现在位置的索引,去对比原始数组如果值相等,那么就可以确定拼图完成。

1.乱序排列[0-9]的数组生成一个新数组,来产生拼图单元格的排序。为了避免生成不符合需求的数组,比较然后递归。

indicator.fn.unordered = function (array) {  //乱序排列      array=array || [0,1,2,3,4,5,6,7,8,9];      if (({}).toString.call(array).indexOf('Array') === -1) return;      var arr = [], value = array.toString();      arr = array.sort(function () {         return Math.random() > 0.5 ? -1 : 1;     });     (arr.toString() === value) && arguments.callee(array);     return arr;};

 

2.主要难点在于拖拽时实时去检测现在单元格与其他单元格的位置关系,判断拖动的单元格是不是进入其他单元格而不是在自己的占位单元格。如果不符合要求,则把拖拽元素放回原始位置。

indicator.fn.checkPosition=function(e){ //检查单元格位置,符合条件插入 不符合条件放回原处     e=that.getEvent(e);     var i=that.htmls.length,size=null,pointer = [e.clientX,e.clientY];     for(;i--;){	size=that.getClinetRect(that.htmls[i]);	if(size.top < pointer[1] && size.bottom>pointer[1]){	    if(size.left
pointer[0]){ that.htmls[i]===indicator.currentTarget || that.replaceElement(that.htmls[i]); }else{ that.restore(); } } }};

  

3.为了避免拖拽元素时导致单元格产生的重新排列导致的错位,所以要产生一个占位单元格,这占位的单元格克隆自拖拽单元格。

indicator.fn.createPlaceholder=function(T){   var node=T.cloneNode(true);   node.innerHTML='';   node.style.display='none';   node.className='placeholder';   T.parentNode.insertBefore(node,T);   that.placeholder=node;};

4.界定拖拽单元格是否进入其他单元格,左顶点大于当前单元格的top,left但是小于当前单元格的bottom,right 

 

indicator.fn.checkPosition=function(e){ //检查单元格位置,符合条件插入 不符合条件放回原处    e=that.getEvent(e);    var i=that.htmls.length,size=null,pointer = [e.clientX,e.clientY];    for(;i--;){          size=that.getClinetRect(that.htmls[i]);          if(size.top < pointer[1] && size.bottom>pointer[1]){                if(size.left
pointer[0]){ that.htmls[i]===indicator.currentTarget || that.replaceElement(that.htmls[i]); }else{ that.restore(); } } }};

 

5.调整单元格时,要改变拖拽单元个的两个记录位置的属性,还要改变当前单元格的两个位置属性,再调整记录位置的数组

这里的处理我觉得也是最繁复最有挑战的地方,修改了很久

indicator.fn.replaceElement=function(targetNode){ //摆放单元至正确位置		        var current=indicator.currentTarget,k1=that.htmls,k2=that.sn,temp,j=0,k=0,a1=[-1,-1],a2=null,a3=-1,arr=[],parent=that.parent.children[0],li;		        current.style.position='static';		        that.index=[];		        for(;k

6.在释放鼠标时,检查原始数组和记录位置数组的值十分相等,确定是不是拼图完成。

that.index.join('')===that.sn.sort().join('')

  

 

转载于:https://www.cnblogs.com/enix/archive/2012/04/28/2475287.html

你可能感兴趣的文章
C/C++心得-结构体
查看>>
函数名作为参数传递
查看>>
apt-get for ubuntu 工具简介
查看>>
数值计算算法-多项式插值算法的实现与分析
查看>>
day8-异常处理与网络编程
查看>>
Python基础-time and datetime
查看>>
Linux epoll 笔记(高并发事件处理机制)
查看>>
shell脚本练习01
查看>>
WPF图标拾取器
查看>>
通过取父级for循环的i来理解闭包,iife,匿名函数
查看>>
HDU 3374 String Problem
查看>>
数据集
查看>>
[Leetcode] unique paths ii 独特路径
查看>>
HDU 1217 Arbitrage (Floyd + SPFA判环)
查看>>
IntelliJ idea学习资源
查看>>
Django Rest Framework -解析器
查看>>
ExtJs 分组表格控件----监听
查看>>
Hibernate二级缓存配置
查看>>
LoadRunner常用术语
查看>>
关于jedis2.4以上版本的连接池配置,及工具类
查看>>