Creating a Zoom Image Overlay on Hover using CSS and JavaScript Tutorial

In this tutorial, you can learn how to create a Zoomed Image Overlay when Hovering over the Image Container using CSS and JavaScript. The tutorial aims to provide students and beginners with a reference for learning to create a useful component for a website or web application. Here, I will be providing a simple web page script that demonstrates the creation of a simple image-zoomed overlay functionality.

What is Zoomed Image Overlay?

Zoomed Image Overlay is a site component or useful feature that allows the end-users to zoom the image when hovering over the image container. This feature or component displays a floating element that contains the zoomed image and emphasizes the area where the cursor is located on the image. This feature is often used or implemented in eCommerce websites where visitors or users can zoom the product image by hovering only the image element.

How to create a Zoomed Image Overlay?

The Zoomed Image Overlay can be achieved easily using CSS and JavaScript. CSS comes with useful properties that allow us to design the overlay element according to what we desire and float it on the page screen. Using some of the JavaScript built-in methods, properties, functions, and APIs, we can make the Zoomed Image Overlay feature functional. Check out the scripts that I created and provided below to understand it more.

Sample Web Page Scripts

The scripts below result in a simple web application's page which display an image and has a Zoomed Image Overlay feature. The overlay will show when the user hovers over any area of the image container only and automatically hide when the image container area has been hovered out.

HTML

Here's the HTML file script known as index.html. The file contains the page layout, image container, and overlay elements.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>Zoom Image on Hover</title>
  6. <link rel="stylesheet" href="style.css">
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. </head>
  10. <div class="container">
  11. <h1 id="page-title">Zoom Image on Hover using CSS and JS</h1>
  12. <hr id="title_hr">
  13. <div id="wrapper">
  14. <div id="img-container">
  15. <img src="./image-1.jpg" alt="Sample Image">
  16. </div>
  17. </div>
  18. </div>
  19. <div id="img-zoom-container">
  20. <img src="./image-1.jpg" alt="Sample Image">
  21. </div>
  22. <script src="script.js"></script>
  23. </body>
  24. </html>

CSS

Here's the CSS file script known as style.css. The file contains the stylesheet codes for designing the web page layout, image container, and overlay elements.

  1. @import url('https://fonts.googleapis.com/css2?family=Dongle:wght@300;400;700&family=Roboto+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;1,100;1,200;1,300;1,400;1,500;1,600&display=swap" rel="stylesheet');
  2. *{
  3. margin: 0;
  4. padding: 0;
  5. box-sizing: border-box;
  6. font-family: 'Dongle', sans-serif;
  7. font-family: 'Roboto Mono', monospace;
  8. }
  9. ::selection{
  10. color: #fff;
  11. background: #4db2ec;
  12. }
  13. body{
  14. display: flex;
  15. align-items: center;
  16. justify-content: center;
  17. min-height: 100vh;
  18. background: #0B2447;
  19. background-image: linear-gradient(to right, #2E4F4F 0%, #0B2447 100%);
  20. padding: 2em 0;
  21. }
  22. #page-title{
  23. color: #fff;
  24. text-align: center;
  25. font-weight: 500;
  26. }
  27. #title_hr{
  28. width:60px;
  29. border: 2px solid #ffffff;
  30. margin: .35em auto;
  31. }
  32. /* Card Wrapper */
  33. #wrapper{
  34. width: 600px;
  35. margin: 1em auto;
  36. }
  37.  
  38. @media (max-width:680px){
  39. #wrapper{
  40. width: 95% !important;
  41. }
  42. }
  43.  
  44. div#img-container {
  45. width: 100%;
  46. height: 70vh;
  47. overflow: hidden;
  48. cursor: zoom-in;
  49. }
  50. div#img-container > img{
  51. width: 100%;
  52. height: 100%;
  53. object-fit: cover;
  54. object-position: center center;
  55. }
  56. div#img-zoom-container {
  57. position: fixed;
  58. width: 200px;
  59. height: 200px;
  60. overflow: hidden;
  61. right: 0;
  62. bottom: 0;
  63. display: none;
  64. box-shadow: 0px 0px 10px #0000003d;
  65. border: 3px solid #8f8d8d;
  66. background-color: #000;
  67. }
  68. div#img-zoom-container > img{
  69. position: relative;
  70. width: 100%;
  71. height: 100%;
  72. object-fit: cover;
  73. object-position: center center;
  74. /* transform: scale(2); */
  75. }

JavaScript

Lastly, here's the JavaScript file script known as script.js. The file contains the JS codes that make the zoomed image overlay feature functional.

  1. // Displayed Image Selector
  2. const imageEl = document.querySelector('#img-container>img')
  3. // Hover Image Container
  4. const imageHoverContainer = document.getElementById('img-zoom-container')
  5. // Hover Image Selector
  6. const imageHoverEl = imageHoverContainer.querySelector('img')
  7.  
  8. // Trigger Zoom
  9. const mouseZoomMove = (e) => {
  10. // Original Image Element Width
  11. var oimgWidth = imageEl.parentElement.offsetWidth
  12. // Original Image Element Height
  13. var oimgHeight = imageEl.parentElement.offsetHeight
  14. // Resized Image Element Width
  15. let imgWidth = imageEl.parentElement.offsetWidth * 3
  16. // Resized Image Element Height
  17. let imgHeight = imageEl.parentElement.offsetHeight * 3
  18.  
  19. // Update Overlay Element to zoomed Image Width and Height
  20. imageHoverEl.style.width = `${imgWidth}px`
  21. imageHoverEl.style.height = `${imgHeight}px`
  22.  
  23. // Cursor Position
  24. var zoomPosX = e.clientX
  25. var zoomPosY = e.clientY
  26.  
  27. // Cursor position on Image Container
  28. var nzoomX = e.offsetX
  29. var nzoomY = e.offsetY
  30.  
  31. // Converting Cursor position on Image to Zoomed Image position
  32. nzoomX = (nzoomX / oimgWidth) * imgWidth
  33. nzoomY = (nzoomY / oimgHeight) * imgHeight
  34.  
  35. // Reposition Zoomed Image Overlay according converted position
  36. imageHoverEl.style.left = `-${nzoomX - 100}px`
  37. imageHoverEl.style.top = `-${nzoomY - 100}px`
  38.  
  39. // Check if Overlay Position does not exceeds or less than the window height
  40. if((zoomPosY + 200) > window.outerHeight){
  41. zoomPosY = window.outerHeight - 200
  42. }
  43. if((zoomPosY - 100) < 0){
  44. zoomPosY = 110
  45. }
  46.  
  47. // Check if Overlay Position does not exceeds or less than the window width
  48. if((zoomPosX + 200) > window.outerWidth){
  49. zoomPosX = window.outerWidth - 200
  50. }
  51. if((zoomPosX - 100) < 0){
  52. zoomPosX = 110
  53. }
  54.  
  55. // Update position of overlay
  56. imageHoverContainer.style.left = `${zoomPosX + 20}px`
  57. imageHoverContainer.style.top = `${zoomPosY - 100}px`
  58.  
  59. // Show Overlay
  60. imageHoverContainer.style.display = `block`
  61.  
  62. }
  63.  
  64.  
  65. imageEl.addEventListener('mouseenter', e =>{
  66. // Start Zoom overlay
  67. imageEl.addEventListener('mousemove', mouseZoomMove)
  68. // Stop Zoom overlay
  69. imageEl.addEventListener('mouseout', e =>{
  70. imageEl.removeEventListener('mousemove', mouseZoomMove)
  71. // Hide Overlay
  72. imageHoverContainer.style.display = `none`
  73. })
  74. })
  75.  

Snapshots

Here are some snapshots of the overall result of the provided scripts.

Page Interface

Zoomed Image Overlay using CSS and JavaScript

Image Container

Zoomed Image Overlay using CSS and JavaScript

Zoomed Image Overlay

Zoomed Image Overlay using CSS and JavaScript

There you go! I have provided also the complete source code zip file of the web page scripts above on this website and it is free to download. Feel free to download and modify the source code and some experiments to enhance your programming capabilities.

That's it! I hope this Creating a Zoom Image Overlay on Hover using CSS and JavaScript Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment