Yet another lightweight jQuery code snippet to create an image gallery with zoom hover effect. It arranges the images in a grid layout and zooms the image when it clicked.
How to Create jQuery Gallery with Image Zoom Effect
Create the HTML structure for the image gallery as follows:
<div class="gallery"> <div class="img-w"> <img src="https://images.unsplash.com/photo-1485766410122-1b403edb53db?dpr=1&auto=format&fit=crop&w=1500&h=846&q=80&cs=tinysrgb&crop=" alt="" /></div> <div class="img-w"><img src="https://images.unsplash.com/photo-1485793997698-baba81bf21ab?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=" alt="" /></div> <div class="img-w"><img src="https://images.unsplash.com/photo-1485871800663-71856dc09ec4?dpr=1&auto=format&fit=crop&w=1500&h=2250&q=80&cs=tinysrgb&crop=" alt="" /></div> <div class="img-w"><img src="https://images.unsplash.com/photo-1485871882310-4ecdab8a6f94?dpr=1&auto=format&fit=crop&w=1500&h=2250&q=80&cs=tinysrgb&crop=" alt="" /></div> <div class="img-w"><img src="https://images.unsplash.com/photo-1485872304698-0537e003288d?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=" alt="" /></div> <div class="img-w"><img src="https://images.unsplash.com/photo-1485872325464-50f17b82075a?dpr=1&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=" alt="" /></div> <div class="img-w"><img src="https://images.unsplash.com/photo-1470171119584-533105644520?dpr=1&auto=format&fit=crop&w=1500&h=886&q=80&cs=tinysrgb&crop=" alt="" /></div> <div class="img-w"><img src="https://images.unsplash.com/photo-1485881787686-9314a2bc8f9b?dpr=1&auto=format&fit=crop&w=1500&h=969&q=80&cs=tinysrgb&crop=" alt="" /></div> <div class="img-w"><img src="https://images.unsplash.com/photo-1485889397316-8393dd065127?dpr=1&auto=format&fit=crop&w=1500&h=843&q=80&cs=tinysrgb&crop=" alt="" /></div> </div>
Style the gallery using the following CSS styles:
body { padding: 30px 0; position: relative; } .gallery { width: 600px; margin: auto; border-radius: 3px; overflow: hidden; } .img-c { width: 200px; height: 200px; float: left; position: relative; overflow: hidden; } .img-w { position: absolute; width: 100%; height: 100%; background-size: cover; background-position: center; cursor: pointer; transition: transform ease-in-out 300ms; } .img-w img { display: none; } .img-c { transition: width ease 400ms, height ease 350ms, left cubic-bezier(0.4, 0, 0.2, 1) 420ms, top cubic-bezier(0.4, 0, 0.2, 1) 420ms; } .img-c:hover .img-w { transform: scale(1.08); transition: transform cubic-bezier(0.4, 0, 0.2, 1) 450ms; } .img-c.active { width: 100% !important; height: 100% !important; position: absolute; z-index: 2; } .img-c.postactive { position: absolute; z-index: 2; pointer-events: none; } .img-c.active.positioned { left: 0 !important; top: 0 !important; transition-delay: 50ms; }
Load the jQuery by adding the following CDN link before closing the body tag:
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>
Finally, add the following jQuery gallery function to activate the zoom hover effect.
$(function() { $(".img-w").each(function() { $(this).wrap("<div class='img-c'></div>") let imgSrc = $(this).find("img").attr("src"); $(this).css('background-image', 'url(' + imgSrc + ')'); }) $(".img-c").click(function() { let w = $(this).outerWidth() let h = $(this).outerHeight() let x = $(this).offset().left let y = $(this).offset().top $(".active").not($(this)).remove() let copy = $(this).clone(); copy.insertAfter($(this)).height(h).width(w).delay(500).addClass("active") $(".active").css('top', y - 8); $(".active").css('left', x - 8); setTimeout(function() { copy.addClass("positioned") }, 0) }) }) $(document).on("click", ".img-c.active", function() { let copy = $(this) copy.removeClass("positioned active").addClass("postactive") setTimeout(function() { copy.remove(); }, 500) })
Code Explanation
First, the code waits for the page to load by wrapping the code in $(function() { ... })
. Then, for each element with the class “img-w”, the code wraps it in a div with the class “img-c” and sets the background image of the new div to be the same as the source of the image within the “img-w” element.
Next, when an “img-c” element is clicked, the code creates a copy of the element, removes any other elements with the class “active”, resizes and positions the copy of the element, and sets a delay of 500 milliseconds before adding the class “active”. The positioned class is added with a zero-millisecond delay, so it is added immediately after the active class.
Finally, when an “img-c active” element is clicked, the code removes the element after a delay of 500 milliseconds by first removing the “positioned” and “active” classes and adding the “postactive” class. The element is then removed from the DOM with a delay of 500 milliseconds.
That’s all! hopefully, you have successfully created jQuery Gallery with Image Zoom Effect. If you have any questions or suggestions, feel free to comment below.