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

134
Visualizações
Drag & Drop with Vanilla JavaScript in small screen & mobile devices

I am trying to make a drag & drop events (to move text) in my webpage. It is working fine in the big screens, but when I try it on small screens (less than 480) the event fires, but the text either disappears or the touch grabs the screen behind the div (the body of the page itself)

JS

let x = 0;
let y = 0;

const ele = document.getElementById("textOverImage");

if (innerWidth <= 480) {
    ele.addEventListener('touchstart', handleStart, false);
    ele.addEventListener('touchend', handleEnd, false);
    ele.addEventListener('touchmove', handleMove, false);

    function handleStart(s) {
        console.log('touch started');

        x = s.clientX;
        y = s.clientY;

        document.querySelector("#lock_image").addEventListener('touchmove', handleMove);
        document.querySelector("#lock_image").addEventListener('touchend', handleEnd);
    };

    function handleMove(s) {
        console.log('touch moved');

        const dx = s.clientX - x;
        const dy = s.clientY - y;

        ele.style.top = `${ele.offsetTop + dy}px`;
        ele.style.left = `${ele.offsetLeft + dx}px`;

        ele.classList.remove('centered');
        ele.classList.remove('customClass');
        ele.classList.add('customMovement');

        x = s.clientX;
        y = s.clientY;
    };

    function handleEnd() {
        console.log('touch ended');
        document.querySelector("#lock_image").removeEventListener('touchmove', handleMove);
        document.querySelector("#lock_image").removeEventListener('touchend', handleEnd);
    };
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

if (window.innerWidth <= 480){
        ele.addEventListener('touchstart', handleStart, false);
        ele.addEventListener('touchend', handleEnd, false);
        ele.addEventListener('touchmove', handleMove, false);

        function handleStart(s){
          s.preventDefault();

          x = s.touches[0].clientX;
          y = s.touches[0].clientY;

          console.table(x, y);

          document.querySelector("#lock_image").addEventListener('touchmove', handleMove);
          document.querySelector("#lock_image").addEventListener('touchend', handleEnd);
        };

        
        
        function handleMove(s){
          s.preventDefault();

          const dx = s.touches[0].clientX - x;
          const dy = s.touches[0].clientY - y;
          
          ele.style.top = `${ele.offsetTop + dy}px`;
          ele.style.left = `${ele.offsetLeft + dx}px`;
          
          
          ele.classList.remove('centered');
          ele.classList.remove('customClass');
          ele.classList.add('customMovement');
          
          x = s.touches[0].clientX;
          y = s.touches[0].clientY;
        };
        
        function handleEnd(s){
          s.preventDefault();
          document.querySelector("#lock_image").removeEventListener('touchmove', handleMove);
          document.querySelector("#lock_image").removeEventListener('touchend', handleEnd);
        };


      }
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