Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9285671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:13:34+00:00 2026-06-18T19:13:34+00:00

This is a followup question to How to zoom to mouse pointer while using

  • 0

This is a followup question to How to zoom to mouse pointer while using my own mousewheel smoothscroll?

I am using css transforms to zoom an image to the mouse pointer. I am also using my own smooth scroll algorithm to interpolate and provide momentum to the mousewheel.

With Bali Balo’s help in my previous question I have managed to get 90% of the way there.

You can now zoom the image all the way in to the mouse pointer while still having smooth scrolling as the following JSFiddle illustrates:

http://jsfiddle.net/qGGwx/7/

However, the functionality is broken when the mouse pointer is moved.

To further clarify, If I zoom in one notch on the mousewheel the image is zoomed around the correct position. This behavior continues for every notch I zoom in on the mousewheel, completely as intended. If however, after zooming part way in, I move the mouse to a different position, the functionality breaks and I have to zoom out completely in order to change the zoom position.

The intended behavior is for any changes in mouse position during the zooming process to be correctly reflected in the zoomed image.

The two main functions that control the current behavior are as follows:

self.container.on('mousewheel', function (e, delta) {
        var offset = self.image.offset();

        self.mouseLocation.x = (e.pageX - offset.left) / self.currentscale;
        self.mouseLocation.y = (e.pageY - offset.top) / self.currentscale;

        if (!self.running) {
            self.running = true;
            self.animateLoop();
        }
        self.delta = delta
        self.smoothWheel(delta);
        return false;
    });

This function collects the current position of the mouse at the current scale of the zoomed image.

It then starts my smooth scroll algorithm which results in the next function being called for every interpolation:

zoom: function (scale) {

    var self = this;
    self.currentLocation.x += ((self.mouseLocation.x - self.currentLocation.x) / self.currentscale);
    self.currentLocation.y += ((self.mouseLocation.y - self.currentLocation.y) / self.currentscale);

    var compat = ['-moz-', '-webkit-', '-o-', '-ms-', ''];
    var newCss = {};
    for (var i = compat.length - 1; i; i--) {
        newCss[compat[i] + 'transform'] = 'scale(' + scale + ')';
        newCss[compat[i] + 'transform-origin'] = self.currentLocation.x + 'px ' + self.currentLocation.y + 'px';
    }
    self.image.css(newCss);


    self.currentscale = scale;
},

This function takes the scale amount (1-10) and applies the css transforms, repositioning the image using transform-origin.

Although this works perfectly for a stationary mouse position chosen when the image is completely zoomed out; as stated above it breaks when the mouse cursor is moved after a partial zoom.

Huge thanks in advance to anyone who can help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:13:35+00:00Added an answer on June 18, 2026 at 7:13 pm

    Before you check this fiddle out; I should mention:

    First of all, within your .zoom() method; you shouldn’t divide by currentscale:

    self.currentLocation.x += ((self.mouseLocation.x - self.currentLocation.x) / self.currentscale);
    self.currentLocation.y += ((self.mouseLocation.y - self.currentLocation.y) / self.currentscale);
    

    because; you already use that factor when calculating the mouseLocation inside the initmousewheel() method like this:

    self.mouseLocation.x = (e.pageX - offset.left) / self.currentscale;
    self.mouseLocation.y = (e.pageY - offset.top) / self.currentscale;
    

    So instead; (in the .zoom() method), you should:

    self.currentLocation.x += (self.mouseLocation.x - self.currentLocation.x);
    self.currentLocation.y += (self.mouseLocation.y - self.currentLocation.y);
    

    But (for example) a += b - a will always produce b so the code above equals to:

    self.currentLocation.x = self.mouseLocation.x;
    self.currentLocation.y = self.mouseLocation.y;
    

    in short:

    self.currentLocation = self.mouseLocation;
    

    Then, it seems you don’t even need self.currentLocation. (2 variables for the same value). So why not use mouseLocation variable in the line where you set the transform-origin instead and get rid of currentLocation variable?

    newCss[compat[i] + 'transform-origin'] = self.mouseLocation.x + 'px ' + self.mouseLocation.y + 'px';
    

    Secondly, you should include a mousemove event listener within the initmousewheel() method (just like other devs here suggest) but it should update the transform continuously, not just when the user wheels. Otherwise the tip of the pointer will never catch up while you’re zooming out on “any” random point.

    self.container.on('mousemove', function (e) {
        var offset = self.image.offset();
        self.mouseLocation.x = (e.pageX - offset.left) / self.currentscale;
        self.mouseLocation.y = (e.pageY - offset.top) / self.currentscale;
        self.zoom(self.currentscale);
    });
    

    So; you wouldn’t need to calculate this anymore within the mousewheel event handler so, your initmousewheel() method would look like this:

    initmousewheel: function () {
        var self = this;
    
        self.container.on('mousewheel', function (e, delta) {
            if (!self.running) {
                self.running = true;
                self.animateLoop();
            }
            self.delta = delta;
            self.smoothWheel(delta);
            return false;
        });
    
        self.container.on('mousemove', function (e) {
            var offset = self.image.offset();
            self.mouseLocation.x = (e.pageX - offset.left) / self.currentscale;
            self.mouseLocation.y = (e.pageY - offset.top) / self.currentscale;
            self.zoom(self.currentscale); // <--- update transform origin dynamically
        });
    }
    

    One Issue:

    This solution works as expected but with a small issue. When the user moves the mouse in regular or fast speed; the mousemove event seems to miss the final position (tested in Chrome). So the zooming will be a little off the pointer location. Otherwise, when you move the mouse slowly, it gets the exact point. It should be easy to workaround this though.

    Other Notes and Suggestions:

    • You have a duplicate property (prevscale).
    • I suggest you always use JSLint or JSHint (which is available on
      jsFiddle too) to validate your code.
    • I highly suggest you to use closures (often refered to as Immediately Invoked Function Expression (IIFE)) to avoid the global scope when possible; and hide your internal/private properties and methods.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a followup question for this: Scrollpane on the bottom, css is hacky,
This is a followup question to Removing <span> tag while leaving content intact, with
This is followup question from my previous question Toggle button inside accordion using jquery
This is a followup question to my other widget-related question . I'd like to
This is a followup question to my other question : Run bat file in
This is a followup on the question: ASP.NET next/previous buttons to display single row
This is a followup to a question I posted yesterday. I thought everything was
As a followup to this question , is it possible to write a single
NOTE: This is a followup to my question here. I have a program that
This is a followup of a previous question I had. I got the very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.