I have created a puzzle starting from one with 25 pieces, there is a free one to be able to move the rest and get a photograph. The problem is that I modified it to use 16 pieces and it doesn't work. I show the code, and the changes I made are made with comments to override that code, if they are removed it works with 25 pieces.
// crear una matriz de diapositivas
var slide = document.getElementsByClassName("box");
var blank = slide[Math.sqrt(slide.length) - 1];//para identificar la diapositiva en blanco
var root = Math.sqrt(slide.length);
for (var i = 0; i < slide.length; i++) {
slide[i].frameNumber = i + 1;
slide[i].coOrdinate = i + 1;// esto cambia con la posición de la diapositiva. ¡nunca es constante!
}
var slideTraced = function (number) {
for (var a = 0; a < slide.length; a++) {
if (slide[a].coOrdinate == number) {
return slide[a];
}
}
};
// esta función dispersa las diapositivas aleatoriamente cuando la ventana se carga .....
var randomization = function () {
var movementOrder = [1, root, -1, -root];
for (var i = 0; i <= 100; i++) {
var guestPosition = blank.coOrdinate + (movementOrder[Math.floor(Math.random() * movementOrder.length)]);
if (guestPosition > 0 && guestPosition <= slide.length) {
var guest = slideTraced(guestPosition);
var left = blank.coOrdinate;
var right = guest.coOrdinate;
if (left % root == 0 && right % root == 1) {
i--;
} else if (left % root == 1 && right % root == 0) {
i--;
} else {
var swPos = guest.getAttribute("style");
var swPosBl = blank.getAttribute("style");
guest.coOrdinate = left;
blank.coOrdinate = right;
guest.setAttribute("style", swPosBl);
blank.setAttribute("style", swPos);
}
} else {
i--;
}
}
};
// llama a la función de aleatorización en la ventana de carga y dispersa las diapositivas ..
window.addEventListener("load", randomization);
// esta es una función para cambiar las posiciones de diapositivas en el clic del usuario ....
var changePosition = function () {
var left = this.coOrdinate;
var right = blank.coOrdinate;
var possibility = left - right;
if (possibility == 1 || possibility == -1 || possibility == root || possibility == -root) {
if (left % root == 1 && right % root == 0) {
return false;
} else if (left % root == 0 && right % root == 1) {
return false;
} else {
var swapped = this.coOrdinate;
var swPos = this.getAttribute("style");
var swPosBl = blank.getAttribute("style");
this.coOrdinate = blank.coOrdinate;
blank.coOrdinate = swapped;
this.setAttribute("style", swPosBl);
blank.setAttribute("style", swPos);
}
}
};
// aplica la función changePosition en todas las diapositivas ...
for (var i = 0; i < slide.length; i++) {
slide[i].addEventListener("click", changePosition);
}
* {
box-sizing: border-box;
}
.main {
margin:10px auto;
width:400px;
height:400px;
margin-bottom:5px;
}
body {
background: grey;
}
h1, p {
font-family:open sans;
margin:5px auto;
text-align:center;
font-size: 95%;
}
.board {
background: rgba(0, 0, 0, 0.15);
position: relative;
width: 90%;
padding-bottom: 90%;
margin: 50px auto;
border: 1px solid black;
z-index:0;
box-shadow: 0px 0px 7px black inset;
overflow: hidden;
}
.box {
position: absolute;
height: 25%;
width: 25%;
float: left;
transition: 0.5s;
background-image: url("http://i2.esmas.com/2015/10/09/808630/bob-esponja-y-mad-men...-juntos-330x330.jpg");
box-shadow: 0px 0px 7px black;
background-size: 500%;
}
.border {
width: 100%;
height: 100%;
border: 1px solid black;
}
div.one {
background-position: 0% 0%;
}
div.two {
background-position: -100% 0%;
}
div.three {
background-position: -200% 0%;
}
div.four {
background-position: -300% 0%;
}
div.five {
background: none;
box-shadow: 0px 0px 0px black;
}
.five .border {
border: none;
}
div.six {
background-position: 0% -100%;
}
div.seven {
background-position: -100% -100%;
}
div.eight {
background-position: -200% -100%;
}
div.nine {
background-position: -300% -100%;
}
div.ten {
background-position: -400% -100%;
}
div.eleven {
background-position: 0% -200%;
}
div.twelve {
background-position: -100% -200%;
}
div.thirteen {
background-position: -200% -200%;
}
div.fourteen {
background-position: -300% -200%;
}
div.fifteen {
background-position: -400% -200%;
}
div.sixteen {
background-position: 0% -300%;
}
/*div.seventeen {
background-position: -100% -300%;
}
div.eighteen {
background-position: -200% -300%;
}
div.nineteen {
background-position: -300% -300%;
}
div.twenty {
background-position: -400% -300%;
}
div.twenty_one {
background-position: 0% -400%;
}
div.twenty_two {
background-position: -100% -400%;
}
div.twenty_three {
background-position: -200% -400%;
}
div.twenty_four {
background-position: -300% -400%;
}
div.twenty_five {
background-position: -400% -400%;
}
#music{
display: none;
autoplay: true;
}
<!DOCTYPE html>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<html>
<head>
<!--
Customize this policy to fit your own app's needs. For more guidance, see:
https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
Some notes:
* gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
* https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
* Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
* Enable inline JS: add 'unsafe-inline' to default-src
-->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Rompecabezas</title>
</head>
<body>
<h1>Puzzle</h1>
<p>Haz clic en las piezas para hacer la foto</p>
<div class="main">
<div class="board" id="board">
<div class="box one" style="top:0%;left:0%;"><div class="border"> </div></div>
<div class="box two" style="top:0%;left:25%;"><div class="border"> </div></div>
<div class="box three" style="top:0%;left:50%;"><div class="border"> </div></div>
<div class="box four" style="top:0%;left:75%;"><div class="border"> </div> </div>
<div class="box five" style="top:25%;left:0%;"> <div class="border"> </div> </div>
<div class="box six" style="top:25%;left:25%;"><div class="border"> </div> </div>
<div class="box seven" style="top:25%;left:50%;"><div class="border"> </div></div>
<div class="box eight" style="top:25%;left:75%;"><div class="border"> </div></div>
<div class="box nine" style="top:50%;left:0%;"><div class="border"> </div></div>
<div class="box ten" style="top:50%;left:25%;"> <div class="border"> </div> </div>
<div class="box eleven" style="top:50%;left:50%;"><div class="border"> </div></div>
<div class="box twelve" style="top:50%;left:75%;"><div class="border"> </div></div>
<div class="box thirteen" style="top:75%;left:0%;"><div class="border"> </div></div>
<div class="box fourteen" style="top:75%;left:25%;"><div class="border"> </div></div>
<div class="box fifteen" style="top:75%;left:50%;"> <div class="border"> </div> </div>
<div class="box sixteen" style="top:75%;left:75%;"><div class="border"> </div> </div>
<!--<div class="box seventeen" style="top:60%;left:20%;"><div class="border"> </div></div>
<div class="box eighteen" style="top:60%;left:40%;"><div class="border"> </div></div>
<div class="box nineteen" style="top:60%;left:60%;"><div class="border"> </div> </div>
<div class="box twenty" style="top:60%;left:80%;"> <div class="border"> </div> </div>
<div class="box twenty_one" style="top:80%;left:0%;"> <div class="border"> </div> </div>
<div class="box twenty_two" style="top:80%;left:20%;"> <div class="border"> </div> </div>
<div class="box twenty_three" style="top:80%;left:40%;"> <div class="border"> </div> </div>
<div class="box twenty_four" style="top:80%;left:60%;"> <div class="border"> </div> </div>
<div class="box twenty_five" style="top:80%;left:80%;"> <div class="border"> </div> </div>-->
</div>
</div>
<!-- <iframe src="audio\entredosaguas.mp3" id="music"></iframe>-->
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
If someone did something similar and can help me
The problem is the CSS: They are set to crop the image into 25 chunks, ignoring the fifth (the top right corner). You must modify them so that it is the fourth div that has
background: none
, in addition to repositioning the displacement (the offset ) of the image in the background of the rest.Bonus: how your puzzle cells work:
background-position: -300% 0%;