Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!