Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Drag and drop javascript game

Here is what i have so far : https://webte1.fei.stuba.sk/~xkvasnica/skuskove_zadanie/game.html

I want to ask if there is any library which I can use to emulate the dragging and dropping behavior in the game linked above, but for touch screens? When I try to use sortablejs, it just switches the Cards which I do not want. I was not yet able to implement touch events in JavaScript the way I want it. In sortablejs I found options for just cloning the dragged element and disabling sorting which is for most part what I want, but I cant find any drop event and some equivalent to dragenter, dragleave and drop. You can see in my game that the source of the image element changes with the dragged one. When there is no library with such funcionality I would like to implement this behavior in native Javascript using touchstart, toucheneter, touchleave but it seems to by little more complicated.

Here is function where I add event listeners, but please just check the page source code and click on linked JavaScript.

I would be so thankful for any advice.

function playGame(gameLevels, level, score) {
  localStorage.setItem("score", String(score));
  localStorage.setItem("level", String(level));
  localStorage.setItem("game-levels", JSON.stringify(gameLevels));
  currentLevel = [];
  options.innerHTML = "";
  gameLevels[level].options.forEach((e) => {
    const img = document.createElement("img");
    img.src = e.image;
    img.setAttribute("alt", e.name);
    img.classList.add("optionsImages");
    img.width = 200;
    img.height = 200;
    img.style = "object-fit : cover";
    currentLevel.push(img);
    options.appendChild(img);
    nameStatus.innerHTML = `${correctAnswer}`;
    scoreStatus.innerHTML = `Points: ${score}`;
    levelStatus.innerHTML = `Level: ${level + 1}/10`;
  });

  answer.addEventListener("dragover", function (event) {
    event.preventDefault();
  });

  answer.addEventListener("drop", (e) => {
    e.preventDefault();
    e.target.src = currentDrag;
    e.target.alt = currentAnswer;
    prevDrag = currentDrag;
  });


  options.addEventListener("dragstart", (e) => {
    currentDrag = e.target.src;
    currentAnswer = e.target.alt;
  });

  answer.addEventListener("dragenter", (e) => {
    e.target.src = currentDrag;
  });

  answer.addEventListener("dragleave", (e) => {
    e.target.src = prevDrag;
  });
} 

BTW, it is a school project but I am really stuck and we are allowed to use libraries.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda