-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
107 lines (92 loc) · 3.31 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
var images = ['https://images.srkh.in/wp-content/uploads/2023/02/Happy-Propose-Day-Images-333-I-LOVE-YOU.jpg'];
var currentIndex = 0;
var totalClicks = 0;
function randomizeImage() {
let root = document.documentElement;
root.style.setProperty('--image', 'url(' + images[currentIndex] + ')');
currentIndex++;
if (currentIndex >= images.length) {
currentIndex = 0;
}
var puzzleItems = document.querySelectorAll('#puzz i');
for (var i = 0; i < puzzleItems.length; i++) {
puzzleItems[i].style.left = Math.random() * (window.innerWidth - 100) + 'px';
puzzleItems[i].style.top = Math.random() * (window.innerHeight - 100) + 'px';
}
}
randomizeImage();
function reloadPuzzle() {
var doneItems = document.querySelectorAll('.done');
doneItems.forEach(function (element) {
element.classList.toggle('done');
});
var droppedItems = document.querySelectorAll('.dropped');
droppedItems.forEach(function (element) {
element.classList.toggle('dropped');
});
var allDoneElement = document.querySelector('.allDone');
allDoneElement.style = '';
allDoneElement.classList.toggle('allDone');
}
// mobile functionality
var puzzleItemsMobile = document.querySelectorAll('#puzz i');
puzzleItemsMobile.forEach(function (element) {
element.addEventListener('mousedown', function () {
totalClicks++;
document.querySelector('#clicks').innerHTML = totalClicks;
});
element.addEventListener('click', function () {
if (document.querySelector('.clicked')) {
document.querySelector('.clicked').classList.toggle('clicked');
element.classList.toggle('clicked');
} else {
element.classList.toggle('clicked');
}
});
});
var puzzleItemsDesktop = document.querySelectorAll('#puz i');
puzzleItemsDesktop.forEach(function (element) {
element.addEventListener('click', function () {
if (document.querySelector('.clicked')) {
var clickedElement = document.querySelector('.clicked');
if (clickedElement.classList.contains(element.classList)) {
element.classList.add('dropped');
clickedElement.classList.add('done');
clickedElement.classList.toggle('clicked');
if (document.querySelectorAll('.dropped').length == 9) {
document.querySelector('#puz').classList.add('allDone');
document.querySelector('#puz').style.border = 'none';
document.querySelector('#puz').style.animation = 'allDone 1s linear forwards';
setTimeout(function () {
reloadPuzzle();
randomizeImage();
}, 1500);
}
}
}
});
});
// desktop drag and drop
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.className);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
if (ev.target.className == data) {
ev.target.classList.add('dropped');
document.querySelector('.' + data + "[draggable='true']").classList.add('done');
if (document.querySelectorAll('.dropped').length == 9) {
document.querySelector('#puz').classList.add('allDone');
document.querySelector('#puz').style.border = 'none';
document.querySelector('#puz').style.animation = 'allDone 1s linear forwards';
setTimeout(function () {
reloadPuzzle();
randomizeImage();
}, 1500);
}
}
}