最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Zooming an image aimed at the mouse cursor - Stack Overflow

programmeradmin2浏览0评论

Use case:

User clicks a link, a modal window is displayed containing one big image, scaled down to fit this window. When the user scrolls their mousewheel up or down, the image zooms in or out. The image is zoomed at whatever location the mouse cursor is pointing.

Problem:

I can't wrap my brain around the how-to part of doing this.

Current workings:

When a link is clicked, a jQuery plugin creates a "viewer" collection of html elements that are styled with CSS. It's an absolute positioned DIV that contains an IMG-element. The image is scaled to 100% width. When scrolling, the image is increased by 5% per scroll action.

Because the image is scaled up whilst zooming in, the containing DIV (overflow: hidden) will eventually clip the image. The image is draggable so that you can always see all parts of it.

Variables I keep and know:

The image's scaled down ratio. The image's dimensions pre our most recent zoom action. The image's new dimensions after our most recent zoom action. The difference in these two dimensions. The X and Y position of the image. The X and Y position of the mouse cursor, relative to the positioned container DIV.

What I probably need:

I know the variables I need, I just can't grasp in what order to put them and what operators to use when. Math never was my strong suit, neither was grammar. So any help is appreciated.

Research done:

I Googled a lot for existing solutions. Most were implementations of OpenGL or Flash/ActionScript, none of which make any sense to me whatsoever. The few javascript-based examples I found were for mercial purposes, and even then their source code was obfuscated or minified, and I couldn't quite extract the inner workings for this little feature properly.

Also, I probably don't have the correct search words to figure it out, either.

Use case:

User clicks a link, a modal window is displayed containing one big image, scaled down to fit this window. When the user scrolls their mousewheel up or down, the image zooms in or out. The image is zoomed at whatever location the mouse cursor is pointing.

Problem:

I can't wrap my brain around the how-to part of doing this.

Current workings:

When a link is clicked, a jQuery plugin creates a "viewer" collection of html elements that are styled with CSS. It's an absolute positioned DIV that contains an IMG-element. The image is scaled to 100% width. When scrolling, the image is increased by 5% per scroll action.

Because the image is scaled up whilst zooming in, the containing DIV (overflow: hidden) will eventually clip the image. The image is draggable so that you can always see all parts of it.

Variables I keep and know:

The image's scaled down ratio. The image's dimensions pre our most recent zoom action. The image's new dimensions after our most recent zoom action. The difference in these two dimensions. The X and Y position of the image. The X and Y position of the mouse cursor, relative to the positioned container DIV.

What I probably need:

I know the variables I need, I just can't grasp in what order to put them and what operators to use when. Math never was my strong suit, neither was grammar. So any help is appreciated.

Research done:

I Googled a lot for existing solutions. Most were implementations of OpenGL or Flash/ActionScript, none of which make any sense to me whatsoever. The few javascript-based examples I found were for mercial purposes, and even then their source code was obfuscated or minified, and I couldn't quite extract the inner workings for this little feature properly.

Also, I probably don't have the correct search words to figure it out, either.

Share Improve this question asked Aug 31, 2011 at 13:11 MHDMHD 1,3602 gold badges13 silver badges18 bronze badges 1
  • Can you show what you've done so far in a JS Fiddle, or similar, demo? – David Thomas Commented Aug 31, 2011 at 13:24
Add a ment  | 

1 Answer 1

Reset to default 4

You could try to use an existing jQuery plugin, such as MapBox or GZoom:

  • http://wayfarerweb./jquery/plugins/mapbox
  • http://lab.gianiaz./jquery/gzoom

Or you could try to roll your own. If you do this, there are a three pieces you'll need to put together:

  1. Use javascript to scale the images in and out http://agilepartners./blog/2005/12/07/iphoto-image-resizing-using-javascript/
  2. Use javascript to allow you to pan the zoomed-in image
  3. Capture the scroll wheel movements in javascript http://www.switchonthecode./tutorials/javascript-tutorial-the-scroll-wheel
发布评论

评论列表(0)

  1. 暂无评论