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

178
Views
Implementing a zoom function but can't get it to work properly
    let scale = 1;
    let translateX = 0;
    let translateY = 0;
    let element = document.querySelector('img');

    let zoom = ( e ) => {

        e.preventDefault();

        let formerScale = scale;
        scale -= (e.deltaY / 5000);
        let elemRect = element.getBoundingClientRect();
        let mX = e.clientX - elemRect.x;
        let mY = e.clientY - elemRect.y;

        let newMX = mX - translateX ;
        let newMY = mY - translateY;
        
        let x = mX - (newMX * (scale / formerScale));
        let y = mY - (newMY * (scale / formerScale));
        translateX = x;
        translateY = y;

        element.style.transform = `translate(${x}px, ${y}px) scale(${scale})`;

    }
    element.addEventListener('mousewheel', wheelZoom);

I am having trouble getting this zoom based on mouse position to work. I got this image with a 3:2 aspect ratio as the elem and the zoom isn't behaving the way its supposed to. It is zooming, but it is traveling away from the area where the mouse is rather than adjusting to it. Would really appreciate some input on the implementation of the zoom here. Thank you!!

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!