Make puzzle games with jQuery_jquery
Source code idea analysis:
【1】How to generate a picture grid, I think of two methods:
(1) Cut this large picture into 16 small pictures, and then use the src of the img tag
(2) There is only one large image, and then the background image of each element is cut and positioned using the background-position of CSS, which requires 16 arrays [0,0], [-150,0], [-300 ,0]..........(I adopt this)
【2】Picture background positioning array and layout positioning array
After choosing to use CSS to position and cut the image, you need to generate data.
The required css background positioning array is: [0,0],[-150,0],[-300,0],[-450,0],
[0,-150],[-150,-150],[-300,-150],[-450,-150],
[0,-300],[-150,-300],[-300,-300],[-450,-300],
[0,-450],[-150,-450],[-300,-450],[-450,-450]
The values in [0,-150,-300,-450] are used among them (that is, I define the height and width of the image as multiples of 150), so I use this value to automatically generate it through for(){} Array
//this.nCol here is 4 --- because my puzzle is 4*4
// this.nArea is 150, which is the width and height of each picture (600px/4) - the large picture is 600*600
var l = [],
p = [];
for(var n=0;n
p.push(-n*this.nArea); }
for(var i=0;i
k = i - this.nCol*t,
aP = [],
aL = [];
aP.push(p[k],p[t],i); //Here I added an additional i to the css background positioning array, which is used for judgment in step 3. It does not need to be used to set css attributes. I Set it as the attribute of the tag [bg-i]
aL.push(l[k],l[t]);
This.aBgp[i] = aP;
This.aLayout[i] = aL;
}
[3] Determine whether it is completed
The second element (div) applies css background positioning this.aBgp[1] (value is [-150,0,1]), and the randomly assigned layout positioning is this.aLayout[3] (here 3 is randomly generated) (the value is [453,0]), then left:453px,top:0;
By moving this element, what changes is its letf and top values, not the order of its own structure. Get the left and top values of this element (if it is moved to left:151px, top:0), and then use Judging from the value [151,0] of this.aLayout[1] (the 1 index is the [bg-i]=1 of the own label attribute and the index of this.aBgp[1]), equality means that the element has moved The rear position is correct.
Detailed code:
/*
Version:2.0
*/
Function GyPuzzleGame(option){
This.target = $(option.target);
This.data = option.data; //Image data
This.opt = option;
This.nLen = option.count; //How many puzzle pieces
This.aColLayout = option.aColLayout || [0,151,302,453];//Layout horizontal array
This.aRowLayout = option.aRowLayout || [0,151];//Layout vertical array
This.aColBgp = option.aColBgp || [0,-150,-300,-450];//Layout horizontal array
This.aRowBgp = option.aRowBgp || [0,-150];//Layout vertical array
This.nCol = this.aColLayout.length;
This.nRow = this.aRowLayout.length;
This.aLayout = []; //Layout array
This.aBgp = []; //css background positioning array
This.init();
}
GyPuzzleGame.prototype = {
getRand:function(a,r){
var arry = a.slice(0),
newArry = [];
for(var n=0;n
newArry.push(arry[nR]);
arry.splice(nR,1);
}
return newArry;
},
setPos:function(){
for(var i=0;i
aP = [],
aL = [];
aP.push(this.aColBgp[l],this.aRowBgp[t],i);
aL.push(this.aColLayout[l],this.aRowLayout[t]);
This.aBgp[i] = aP;
This.aLayout[i] = aL; }
},
isPass:function(item){
var _that = this,
is = 0;
Item.each(function(){
var l = parseInt($(this).css('left')),
t = parseInt($(this).css('top')),
i = parseInt($(this).attr('data-bgi'));
If(l==_that.aLayout[i][0]&&t==_that.aLayout[i][1]){
is ++;
}
});
return is;
},
createDom:function(){
var layout = this.getRand(this.aLayout,this.nLen);
// console.log(layout);
for(var n=0;n
l = n - this.nCol*t;
var html = $('').
css({'left':layout[n][0] 'px',
'top':layout[n][1] 'px',
'background-image':'url(' this.data ')',
'background-position':this.aBgp[n][0] 'px' ' ' this.aBgp[n][1] 'px'
});
this.target.append(html);
}
},
move:function(){
var $div = this.target.find('.puzzle_list'),
_that = this;
var hasElem = function(){
var t = false;
$div.each(function(){
if($(this).hasClass("on")){
t = true;
}
});
return t;
};
// click
$div.click(function(){
var $this = $(this);
if(hasElem()&&!$this.hasClass("on")){
var index = $('.on').index();
if($div.eq(index).is(':animated')||$this.is(':animated')){
return false;
}
var l = $div.eq(index).position().left,
t = $div.eq(index).position().top,
myl = $this.position().left,
myt = $this.position().top;
$(this).animate({'left':l,'top':t});
$div.eq(index).css({'z-index':'1'}).animate({'left':myl,'top':myt},function(){
$(this).removeClass("on");
$(this).find('span').remove();
$(this).css({'z-index':'0'});
if(_that.isPass($div) == _that.nLen){
if(typeof _that.opt.success == 'function'){
_that.opt.success({target:_that.target});
}
}
});
}
else {
if($this.hasClass("on")){
$this.removeClass("on");
$this.find('span').remove();
}
else {
$this.addClass("on").append("");
}
}
});
},
init:function(){
// 设置CSS背景定位与元素布局数组
this.setPos();
// 创建元素
this.createDom();
// 挪动图片
this.move();
}
}
//实例调用
new GyPuzzleGame({
'data':'images/03.jpg',
'target':'#pA',
'count':8,
'success':function(opt){
opt.target.append('
}
});

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

Download and install the latest version of Yitian Camera 2023. It is a must-have camera tool for everyone. The camera function is very advanced. It is very convenient to take pictures anytime and anywhere. Various styles of theme backgrounds, exquisite stickers, etc. can be downloaded for free. Use, the way to take photos is very simple. One-click beautification will make your skin more delicate. There are many popular and popular photo styles that you can try online every day, allowing you to become a beautiful goddess in minutes, and you can also shoot videos. Continuing to record the most beautiful moments in life, the editor will provide details on how to set up puzzles for Yitian camera partners online. 1. First enter the software interface and click [Template] to enter. 2. Enter the interface and click [Puzzle] to enter. 3. Finally enter the interface and click [Puzzle].
