3

I am working on a project in which there is an image when user clicks on image the image zoom in and when click again the image zoom out. How can I make a zoom out feature in my code here

function resizeImg (img) {
    var scale = 150/100;
    var pos = $(img).offset();
    var clickX = event.pageX - pos.left;
    var clickY = event.pageY - pos.top;
    var container = $(img).parent().get(0);

    $(img).css({
        width: img.width*scale, 
        height: img.height*scale
    });

    container.scrollLeft = ($(container).width() / -2 ) + clickX * scale;
    container.scrollTop = ($(container).height() / -2 ) + clickY * scale;
}
2
  • you mean first click should zoom in and next should zoom out? Commented Aug 17, 2013 at 6:49
  • yes that is exactly what I want. Commented Aug 17, 2013 at 6:52

4 Answers 4

2

You dont need a lot of javascript here..just some good CSS as shown here: http://jsfiddle.net/95wqh/10/

JS:

$('.image').on('click', function () {
    $(this).toggleClass('zoom');
});

css:

#Container {
    position:relative;
    overflow:auto;
}

.zoom{
    -webkit-transform : scale(1.5);
    transform: scale(1.5);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try and see if the below code helps you. Demo

EDIT: Just in-case you want to do this on multiple images in same page.

var zoom = "in";

$('.image').on('click', function () {
    this.zoom = (this.zoom == "in") ? "out" : "in";
    console.log(this.zoom);
    resizeImg(this);
});

function resizeImg (img) {
    var scale = 150/100;
    var pos = $(img).offset();
    var clickX = event.pageX - pos.left;
    var clickY = event.pageY - pos.top;
    var container = $(img).parent().get(0);

    if(img.zoom == "in"){
         $(img).css({
            width: img.width*scale, 
            height: img.height*scale
         });
    }
    else if (img.zoom == "out"){
        $(img).css({
            width: img.width/scale, 
            height: img.height/scale
        });
    }  
    container.scrollLeft = ($(container).width() / -2 ) + clickX * scale;
    container.scrollTop = ($(container).height() / -2 ) + clickY * scale;
}

Comments

1

If you have multiple images this might also help you

JSfiddle: http://jsfiddle.net/95wqh/14/

JS code:

$('.image').on('click', function () {
    if (typeof this.toggleZoom === "undefined") this.toggleZoom = false;
    this.toggleZoom = !this.toggleZoom;
    resizeImg(this);
});

function resizeImg(img) {
    var scale = (img.toggleZoom) ? 150 / 100 : 100 / 150;
    console.log(scale);
    var pos = $(img).offset();
    var clickX = event.pageX - pos.left;
    var clickY = event.pageY - pos.top;
    var container = $(img).parent().get(0);

    $(img).css({
        width: img.width * scale,
        height: img.height * scale
    });

    container.scrollLeft = ($(container).width() / -2) + clickX * scale;
    container.scrollTop = ($(container).height() / -2) + clickY * scale;
}

What I added

    if (typeof this.toggleZoom === "undefined") this.toggleZoom = false;
    this.toggleZoom = !this.toggleZoom;

and

    var scale = (img.toggleZoom) ? 150 / 100 : 100 / 150;

Comments

1

DEMO

DEMO Works with multiple images

$('img').attr('zoom', 0);
$('.image').on('click', function () {
    resizeImg(this);
});

function resizeImg(img) {
    var zoom = $(img).attr('zoom');
    if (zoom == 0) {
        var scale = 150 / 100;
        var pos = $(img).offset();
        var clickX = event.pageX - pos.left;
        var clickY = event.pageY - pos.top;
        var container = $(img).parent().get(0);
        $(img).css({
            width: img.width * scale,
            height: img.height * scale
        }).attr('zoom', 1);
        container.scrollLeft = ($(container).width() / -2) + clickX * scale;
        container.scrollTop = ($(container).height() / -2) + clickY * scale;
    }else{
        $(img).attr('zoom',0).attr('style','');
    }
}

setting zoom attribute = 0 for all the images --> $('img').attr('zoom', 0);

if zoom attribute is set to 0 we will make zoom effect and set attribute zoom=1

else zoom attribute is set to 1 that is already zoomed we set the attribute zoom=0 and style attribute is removed to get the image back to normal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.