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

280
Visualizações
How to make multiple elements drag and drop?

What I need is to make the 3 elements (mouth, left eye, right eye) draggable and droppable on the div with the background (face cut out). After the drop of each element, a text appears.

What I'm trying to achieve enter image description here

The draggable pictures are the same size as the background. I have tried something but I haven't really got anywhere:

<!DOCTYPE HTML>
<html>
   <head>
      <style>
         #div1 {
         width: 1554px;
         height: 874px;
         border: 1px solid #aaaaaa;
         z-index: 1;
         }
         #drag1 {
         width: 1554px;
         height: 874px;
         z-index: 2;
         }
         #drag2 {
         width: 1554px;
         height: 874px;
         z-index: 3;
         }
         #drag3 {
         width: 1554px;
         height: 874px;
         z-index: 4;
         }
      </style>
      <script>
         function allowDrop(ev) {
           ev.preventDefault();
         }
         
         function drag(ev) {
           ev.dataTransfer.setData("text", ev.target.id);
         }
         
         function drop(ev) {
           ev.preventDefault();
           var data = ev.dataTransfer.getData("text");
           ev.target.appendChild(document.getElementById(data));
         }
         
      </script>
   </head>
   <body>
      <div id="div1" style="background-image: url('https://blondtrickster.com/wp-content/uploads/2021/12/BACKGROUND.png');" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
      <br>
      <img id="drag1" src="https://blondtrickster.com/wp-content/uploads/2021/12/SUU.png" draggable="true" ondragstart="drag(event)">
      <img id="drag2" src="https://blondtrickster.com/wp-content/uploads/2021/12/SILM_VASAK.png" draggable="true" ondragstart="drag(event)">
      <img id="drag3" src="https://blondtrickster.com/wp-content/uploads/2021/12/SILM_PAREM.png" draggable="true" ondragstart="drag(event)">
   </body>
</html>      

Help would be much appreciated, since I'm a total beginner.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue is with your CSS rules. Right now when you append child, second IMG is placed under the first so you have to make sure that all IMG elements are stacked over one another. I did this by setting

   "position: absolute; top: 0; left: 0"

on each image.

Complete CSS:

html,
  body {
     margin: 0;
     padding: 0;
  }

  #div1 {
     width: 1554px;
     height: 874px;
     border: 1px solid #aaaaaa;
     z-index: 1;
  }

  #div1 #drag1 {
     position: absolute;
     top: 0;
     left: 0;
     width: 1554px;
     height: 874px;
     z-index: 2;
  }

  #div1 #drag2 {
     position: absolute;
     top: 0;
     left: 0;
     width: 1554px;
     height: 874px;
     z-index: 3;
  }

  #div1 #drag3 {
     position: absolute;
     top: 0;
     left: 0;
     width: 1554px;
     height: 874px;
     z-index: 4;
  }

This is what I got:

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
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