Basic Zoom In Effect for Images using jQuery!

PraveenKumar Purushothaman

PraveenKumar Purushothaman

@praveenkumar-66Ze92 Oct 21, 2024
CEan #-Link-Snipped-# asked how can we make images zoom in after a click. We can use .animate() function and change the properties of an image. Lets have a container and place all the images.
​<div class="zoom"​​​​​​​​>
    [img]https://lorempixel.com/640/480/nightlife/1/[/img]
    [img]https://lorempixel.com/640/480/nightlife/2/[/img]
</div>​
And now, lets define a thumbnail size of the images. The original size is 640px x 480px. Lets take the half of it. So, we will keep it as 320px x 240px. We need to define that in the style as:
​.zoom img {
    width: 320px;
    height: 240px;
}​
Now comes the JavaScript part! 😀 We bind the click function of the image to animate its CSS Width and Height properties. That can be done by:
​$(".zoom img")​.click(function(){
    $(this).animate({
        width: '640px',
        height: '480px'
    }, 1000);
});​
Now, you can see the live demo here: <a href="https://jsfiddle.net/Vn9Gx/" target="_blank" rel="nofollow noopener noreferrer">Edit fiddle - JSFiddle - Code Playground</a>

If #-Link-Snipped-# was asking about having a set of images and upon clicking on it, making them show in a modal window, then dude, you are asking how to make something like a <a href="https://lokeshdhakar.com/projects/lightbox2/" target="_blank" rel="nofollow noopener noreferrer">Lightbox2</a>! 😀 Check it out too! Try this out and let me know your comments.

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Aug 14, 2012

    Thank you praveen for such a quick response

    Actually I was looking for something like this

    #-Link-Snipped-#

    on product image, this is one of my project and i want zoom plugin to work only on click
  • PraveenKumar Purushothaman

    PraveenKumar Purushothaman

    @praveenkumar-66Ze92 Aug 14, 2012

    goyal420
    Thank you praveen for such a quick response

    Actually I was looking for something like this

    #-Link-Snipped-#

    on product image, this is one of my project and i want zoom plugin to work only on click
    You mentioned that it shouldn't come on hover right?
  • PraveenKumar Purushothaman

    PraveenKumar Purushothaman

    @praveenkumar-66Ze92 Aug 14, 2012

    In case, you wanna build something like that, it is kind of Image Preview on Hover. You can see a detailed tutorial on how to do that here: #-Link-Snipped-#