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

166
Views
function to use mouse as touch navigation

I was creating a website and my client wanted the navigation to be by touch on the desktop, I looked and didn't find anything that helped me so I'm posting it here in case anyone needs it.

         const mouse = {
                x: 0,
                y: 0,
                mouseMove: false,
                screenPosition:{
                 x: 0,
                 y: 0
                }
        
         };
    
         window.addEventListener('mousedown', (e) => {
                //to take the mouse position and screen;
                mouse.screenPosition.x = window.scrollX;
                mouse.screenPosition.y = window.scrollY;
                mouse.x = e.clientX;
                mouse.y = e.clientY;
                mouse.mouseMove = true;
        })

        window.addEventListener('mouseup', () => {
                mouse.mouseMove = false;
        })
        window.addEventListener('mousemove', (e) => {
            //to take the mouse position
            //prevent mouse comportament 
            e.preventDefault();
            if(mouse.mouseMove){
                const x = e.clientX - mouse.x;
                const y = e.clientY - mouse.y;
                window.scrollTo(
                mouse.screenPosition.x - x,
                mouse.screenPosition.y - y,
                );
            }
        })

I created a website that on desktop you use touch navigation!

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