admin管理员组

文章数量:1309988

I'm trying to create a zoom able image upon click of a button, however the image should be zoomed centered on the view able area, as the image may be bigger than the container.

I've created a fiddle here to illustrate what I want, I'd obviously usually hide the image outside the container, I'm not bothered about that part just now, also put in a border overlay to show the bounds of the container.

I've done the zoom part based on the image ratio, I just need to work out the new top and left css values and apply it to the image after the zoom. Also the image is draggable, so once you move the image it must take into account the position of the image.

Basically, the centrol point of the image within the red container must remain the same after the zoom, so you are effectly zooming in on whatever is at the middle of the container.

/

why do you we need code to link to jsfiddle?

Thanks

edit:

/

getting close with the above fiddle, but still not zooming pletely on central point

I'm trying to create a zoom able image upon click of a button, however the image should be zoomed centered on the view able area, as the image may be bigger than the container.

I've created a fiddle here to illustrate what I want, I'd obviously usually hide the image outside the container, I'm not bothered about that part just now, also put in a border overlay to show the bounds of the container.

I've done the zoom part based on the image ratio, I just need to work out the new top and left css values and apply it to the image after the zoom. Also the image is draggable, so once you move the image it must take into account the position of the image.

Basically, the centrol point of the image within the red container must remain the same after the zoom, so you are effectly zooming in on whatever is at the middle of the container.

http://jsfiddle/wFaFg/1/

why do you we need code to link to jsfiddle?

Thanks

edit:

http://jsfiddle/FU55w/

getting close with the above fiddle, but still not zooming pletely on central point

Share Improve this question edited May 15, 2013 at 16:32 Ant asked May 15, 2013 at 15:20 AntAnt 3681 gold badge3 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

So I found the solution,

It is slightly more plex than just finding out how much the image increased in size.

I work out the x & y value of the center point of the image within the container, by taking the left and top value, turning it positive then adding half the container width and height.

var x = Math.abs(image.position().left) + container.width() / 2
var y = Math.abs(image.position().top) + container.height() / 2

I work out the ratio of the image scale by dividing the new width by the old width, then I can multiply the x & y value by the ratio.

Then take the difference between the new x and y away from the current left and top.

image.position().left - (newX - x)
image.position().top - (newY - y)

Complete fiddle: http://jsfiddle/fbd2mk5q/

Try the new fiddle based on your ment:

http://jsfiddle/wFaFg/6/

$("#zoom").on("click", function(e)
{
    var container = $("#container");
    var image = $("#container img");
    var curLeft = image.position().left;
    var curTop = image.position().top;
    var ratio = image.height() / image.width();

    image.css({
        height: image.height() + (20 * ratio),
        width: image.width() + (20 * ratio),
        left: curLeft - ((20 * ratio)/2),
        top: curTop - ((20 * ratio)/2)
    });
});

本文标签: jqueryJavaScript zoom image and center viewable areaStack Overflow