admin管理员组

文章数量:1315025

I want to zoom-in and zoom-out image on mouse scroll in HTML. There are multiple img tag without ID. So how can I do it using JavaScript or Ajax?

I want to zoom-in and zoom-out image on mouse scroll in HTML. There are multiple img tag without ID. So how can I do it using JavaScript or Ajax?

Share Improve this question edited Apr 10, 2014 at 7:54 Rajat 1,04312 silver badges23 bronze badges asked Apr 10, 2014 at 7:24 VickyVicky 531 gold badge1 silver badge6 bronze badges 4
  • 2 Hey check this link out. Does this do what you want? – Déjà vu Commented Apr 10, 2014 at 7:31
  • Short answer: yes. Long answer: check google, other questions on SO, try to write down some code and e back with a specific question or problem. – giorgio Commented Apr 10, 2014 at 7:41
  • Yes I want this type of functionality. I checked that earlier but I don't know why that functionality not work in my side. – Vicky Commented Apr 10, 2014 at 7:43
  • @Vicky if you mean what i linked. It's because it's not originally within jquery. You have to download it and then include it in the header. – Déjà vu Commented Apr 10, 2014 at 8:28
Add a ment  | 

1 Answer 1

Reset to default 4

Just throwing the answer for the ones that will search for an answer to this question.

First, you will need to find a system to detect the mouse scroll. If you are courageous, you can develop it yourself. If you're not, you can find some pretty good libraries (ex : MouseWheel with JQuery).

Next, you will find another two ways to zoom in and out.

Easy way

First, let's cheat a bit. When you will have to zoom, just multiply the height and width of your image by a factor you will decide. To have height and width into a variable (JQuery)

var height = $('#image').height();
var width = $('#image').width();

For each scroll you will receive, you will only have 2 choices. Once you are able to know if the mousewheel goes up or down, you will just have to do something like this (JQuery)

height *= 2;
width *= 2;

This way, by doubling the size of your image, you will have the impression to zoom in.

Less easy way

If you want to zoom in as you would do in a GMap object, you can do something like that.

var firstHeight = $('#image').height();

height *= 2;
width *= 2;

scalechange = (actualHeight / firstHeight) - 1;
offsetX = -(coordX * scalechange);
offsetY = -(coordY * scalechange);

$("#image").css('top', offsetY + 'px');
$("#image").css('left', offsetX + 'px');

First, you have to have the first height of your image. Next, you will double the size of your image (zoom effect). Next step is to calculate the scalechange. You will be able to find multiple explanations and many way to calculate it, my method is as good as another. The two offsets presented are the new positions that your image will adopt (simple factor calculation, it's like making x percent on a price). Last part is to set the new values of your image.

In the end, you will be able to zoom and unzoom with ou without centering the image at your mouse position.

Be careful : The calculation above in only to zoom-in. You will have to do some maths to get the zoom-out!

Go further ?

Another way to go further would be to place your image in a div.

<div id="imageContainer" style="overflow:hidden;">
    <img id="image" src="YourImage">
</div>

By setting

"overflow:hidden;" 

to your div, your image will zoom. But everything that will overflow your div will be hidden. If you set your div to the original size of your image, like this (JQuery)

$("#imageContainer").css('height', $('#image').height());
$("#imageContainer").css('width', $('#image').width());

Then you will have an image displayed that will always be at the same size, but your zoom will be effective. If you bine this to a drag'n'drop method, you have a GMap object-like (zoom in-out, moove the zoomed image, ...)

Hope it will help someone!

本文标签: javascriptZoomin and Zoomout image when Mouse ScrollStack Overflow