Enlarge/Zoom Image Onclick JavaScript Popup

JavaScript Popup Enlarge Image onclick
Code Snippet:Zooming
Author: Chris Coyier
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 17,461
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript plugin helps you to create a popup enlarge image onclick event. It comes with smooth animations with intuitive gestures. Basically, it picks images from content/article and zooms into a hi-res image. Moreover, the zooming speed, background overlay, and image grab can be customized with plugin options.

How to Create JavaScript Popup Enlarge Image onclick

1. First of all, place your images with data-action=”zoom” and data-original attributes. Define the src attribute for the image thumbnail and data-original for a full-size image that will be loaded when clicked on the thumbnail.

<div class="container">
  <section class="header">
    <h2>Zooming</h2>

    <div class="value-img">
      <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/journey_start_thumbnail.jpg" data-action="zoom" data-original="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/journey_start.jpg" alt="journey_start_thumbnail" />
    </div>
  </section>

  <section class="content">
    <div class="value-img">
      <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/journey_thumbnail.jpg" data-action="zoom" data-original="https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/journey.jpg" alt="journey_thumbnail" />
    </div>
  </section>
</div> 

2. After that, add the following CSS styles to your project:

.container {
  max-width: 600px;
}

section.header {
  margin-top: 4rem;
  text-align: center;
}

section.content {
  text-align: center;
}

.value-img {
  display: block;
  text-align: center;
  margin: 2em 0;
}

img {
  max-width: 100%;
}

@media (min-width: 550px) {
  .header {
    margin-top: 10rem;
  }
}

.button.button-primary {
  background-color: #ce7338 !important;
  border-color: #ce7338 !important;
}

3. Finally, add the following JavaScript code and done.

/* eslint-disable */

var config = Zooming.config(),
TRANSITION_DURATION_DEFAULT = config.transitionDuration,
TRANSITION_DURATION_SLOW = 1.0,
TRANSITION_DURATION_FAST = 0.2,
BG_COLOR_DEFAULT = config.bgColor,
BG_COLOR_DARK = '#000',
ENABLE_GRAB_DEFAULT = config.enableGrab,
ACTIVE_CLASS = 'button-primary',

btnFast = document.getElementById('btn-fast'),
btnSlow = document.getElementById('btn-slow'),
btnDark = document.getElementById('btn-dark'),
btnNoGrab = document.getElementById('btn-no-grab');

function isActive(el) {
  return el.classList.contains(ACTIVE_CLASS);
}

function activate(el) {
  el.classList.add(ACTIVE_CLASS);
}

function deactivate(el) {
  el.classList.remove(ACTIVE_CLASS);
}

function fast() {
  var t;
  if (isActive(btnFast)) {
    t = TRANSITION_DURATION_DEFAULT;
    deactivate(btnFast);
  } else {
    t = TRANSITION_DURATION_FAST;
    activate(btnFast);
    deactivate(btnSlow);
  }

  Zooming.config({ transitionDuration: t });
}

function slow() {
  var t;
  if (isActive(btnSlow)) {
    t = TRANSITION_DURATION_DEFAULT;
    deactivate(btnSlow);
  } else {
    t = TRANSITION_DURATION_SLOW;
    activate(btnSlow);
    deactivate(btnFast);
  }

  Zooming.config({ transitionDuration: t });
}

function dark() {
  var c;
  if (isActive(btnDark)) {
    c = BG_COLOR_DEFAULT;
    deactivate(btnDark);
  } else {
    c = BG_COLOR_DARK;
    activate(btnDark);
  }

  Zooming.config({ bgColor: c });
}

function noGrab() {
  var enable;
  if (isActive(btnNoGrab)) {
    enable = ENABLE_GRAB_DEFAULT;
    deactivate(btnNoGrab);
  } else {
    enable = !ENABLE_GRAB_DEFAULT;
    activate(btnNoGrab);
  }

  Zooming.config({ enableGrab: enable });
}

That’s all! hopefully, you have successfully integrated this popup enlarge image code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

1 thought on “Enlarge/Zoom Image Onclick JavaScript Popup”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X