// //globalne zmienne
const squere = document.querySelectorAll(".squere");
const start = document.getElementById("start");
const timeDisplay = document.getElementById("time-left");
const round = document.querySelector(".round");
let time = 3;
//po kliknięciu w przycisk start losuje kolory i odpala licznik
start.addEventListener("click", asignColor);
start.addEventListener("click", interval);
start.addEventListener("click", signColorToCircle);
//funkcja przypisująca kolor
function asignColor(){
const colors = ["yellow", "black", "red", "pink"];
squere.forEach(element => {
const colorIndex = Math.floor(Math.random() * colors.length); //losuje kolor
element.style.backgroundColor = colors[colorIndex]; //przypisuje kolor
element.classList.add(colors[colorIndex]); //dodaje klase danego koloru
colors.splice(colorIndex, 1); //usuwa element z indexu
//wszystkie elementy drag na każdym kwadracie
element.addEventListener("dragstart", dragStart);
element.addEventListener("dragend", dragEnd);
});
}
function signColorToCircle(){
const colors = ["yellow", "black", "red", "pink"];
const colorIndex = Math.floor(Math.random() * colors.length);
round.classList.add(colors[colorIndex]);
}
//odlicza czas do końca rundy
function timer(){
if(time > 0){ //jeżeli czas jest większy od zera
time--; //odejmij o 1
}
timeDisplay.innerHTML = time; //wyświetl czas
}
//ustawia interwał na funckcji timer
function interval(){
setInterval(timer, 1000);
}
round.addEventListener('dragover', dragOver);
round.addEventListener('dragenter', dragEnter);
round.addEventListener('dragleave', dragLeave);
round.addEventListener('drop', dragDrop);
//funkcje związane z przeciąganiem
function dragStart(){
this.classList.add("squere-hold");
}
function dragEnd(){
this.classList.remove("squere-hold");
}
function dragOver(event){
event.preventDefault();
}
function dragEnter(event){
event.preventDefault();
this.classList.add("hover-over-circle-hold");
}
function dragLeave(){
this.classList.remove("hover-over-circle-hold");
}
function dragDrop(){
const colors = ["yellow", "black", "red", "pink"];
for(var i = 0; i < squere.length; i++){
if(squere[i].classList.contains("yellow") && round.classList.contains("yellow")){
alert("dupa");
}
}
}
body{
text-align: center;
}
.squere{
width: 50px;
height: 50px;
border: 2px solid black;
}
.round{
width: 200px;
height: 200px;
border: 2px solid black;
border-radius: 50%;
margin-left: 60%;
position: relative;
bottom: 100px;
}
.squere-hold{
border-color: white;
background-color: white !important;
}
.hover-over-circle-hold{
border: 4px solid black;
box-sizing: border-box;
}
input[type="button"]{
width: 100px;
height: 50px;
}
<h1>Przeciągnij odpowiedni kolor do kwadratu</h1>
<h2>Czas: <span id="time-left">3</span></h2>
<div class="squere" id="red" draggable="true"></div>
<div class="squere" id="blue" draggable="true"></div>
<div class="squere" id="green" draggable="true"></div>
<div class="squere" id="black" draggable="true"></div>
<div class="round"></div>
<input type="button" name="" value="start" id="start">