Posted in Image Zoom
9735
10:56 pm, February 28, 2022

Silky Smooth Image Zoom and Lightbox

What is Silky Smooth Image Zoom and Lightbox?

This is a nice and small javascript library that adds a image zoom or lightbox effect to your images, with no need for JQuery or any other scripts. 

The author originally intended this script to behave like the images on medium with a smooth animation when clicked and then if clicked again it zooms back to its original location. 

Preview


a preview of Silky Smooth Image Zoom and Lightbox

What can i use this for?

You can add this to your page, if you want a nice simple lightbox or image zoom plugin, that requires little configuration.

How to use Smooth Image Zoom?

Add the script

HTML

<script src="https://cdn.jsdelivr.net/npm/smooth-zoom/dist/zoom.browser.js"></script>

Add your images with a class name

HTML

<img src="https://source.unsplash.com/Y9ksK2aKzkU/1200x800" class="zoomable" style="width:50%;" />

Add this javascript attaching the script to your image class names.

Javascript

Zoom(".zoomable",{
    background: "auto"
  });

You should now have a working image zoom. 

Conclusion

Very slick and light script made just to zoom in and out your images, check it out below. 

Original Source

Thanks to the author marshallku this code comes from smooth-zoom on github.

Silky Smooth Image Zoom and Lightbox Demo

View Demo Full Screen View Demo New Tab

Silky Smooth Image Zoom and Lightbox Code

HTML

<img src="https://source.unsplash.com/Y9ksK2aKzkU/1200x800" class="zoomable" style="width:50%;" />
  <img src="https://source.unsplash.com/a2TsN86vay0/800x1200" class="zoomable" style="width:50%;"  />

Scripts

<script src="https://cdn.jsdelivr.net/npm/smooth-zoom/dist/zoom.browser.js"></script>

Javascript

Zoom(".zoomable",{
    background: "auto"
  });

/* Full Source Below - zoom.browser.js

window.Zoom=function(e,t){const n=t&&t.originalizer?t.originalizer:e=>e,o={screenWidth:0,screenHeight:0,scrollBar:0},r=t&&t.background?t.background:null,i=()=>{const{documentElement:e}=document;o.screenWidth=window.innerWidth||e.clientWidth,o.screenHeight=window.innerHeight||e.clientHeight,o.scrollBar=o.screenWidth-e.offsetWidth},c=e=>{const t=e.currentSrc||e.src,{srcset:i}=e,{screenWidth:c,screenHeight:s,scrollBar:d}=o,{width:l,height:m,left:h,top:g}=e.getBoundingClientRect(),f=(c-d)/2-h-l/2,u=(s-m)/2-g,v=document.createElement("div"),w=document.createElement("img"),E=()=>{v.classList.remove("zoom-bg--reveal"),w.style.transform="",w.addEventListener("transitionend",()=>{v.remove(),e.classList.remove("zoom-original--hidden"),w.remove()},{once:!0}),v.removeEventListener("click",E),w.removeEventListener("click",E),window.removeEventListener("scroll",E),window.removeEventListener("resize",E)};let L=e.naturalWidth;if(w.style.top=`${g+window.scrollY}px`,w.style.left=`${h}px`,w.style.width=`${l}px`,w.style.height=`${m}px`,v.classList.add("zoom-bg"),r)if("auto"===r){const t=a(e);v.style.background=t?`rgb(${t.r}, ${t.g}, ${t.b})`:"rgb(0, 0, 0)"}else v.style.background=r;if(i){const e=i.match(/ ([0-9]+)w/gm);e&&e.forEach(e=>{const t=+e.trim().replace("w","");t>L&&(L=t)})}L>=c&&(L=c);const p=L*(m/l);p>=s&&(L=L*s/p),w.classList.add("zoom-img"),w.src=t,w.width=l,w.height=m,v.addEventListener("click",E,{once:!0}),w.addEventListener("click",E,{once:!0}),window.addEventListener("scroll",E,{once:!0}),window.addEventListener("resize",E,{once:!0}),document.body.append(v),document.body.append(w),w.offsetWidth,e.classList.add("zoom-original--hidden");const y=L!==l?L/l:1;w.style.transform=`matrix(${y}, 0, 0, ${y}, ${f}, ${u})`,v.classList.add("zoom-bg--reveal"),w.addEventListener("transitionend",()=>{w.src=n(t)})},s=e=>{c(e.target)},a=e=>{const t={r:0,g:0,b:0},n=document.createElement("canvas"),o=n.getContext("2d");if(!o)return null;const r=n.width=e.naturalWidth||e.offsetWidth||e.width,i=n.height=e.naturalHeight||e.offsetHeight||e.height;let c,s,a=-4,d=0;o.drawImage(e,0,0);try{c=o.getImageData(0,0,r,i)}catch(e){return null}for(s=c.data.length;(a+=20)<s;)++d,t.r+=c.data[a],t.g+=c.data[a+1],t.b+=c.data[a+2];return t.r=Math.floor(t.r/d),t.g=Math.floor(t.g/d),t.b=Math.floor(t.b/d),t},d=e=>{if(e&&("string"==typeof e?document.querySelectorAll(e).forEach(l):e instanceof HTMLElement?l(e):(e instanceof NodeList||e instanceof Array)&&e.forEach(l),"string"==typeof e)){const t=document.createElement("style"),n=document.head||document.getElementsByTagName("head")[0];t.appendChild(document.createTextNode(`${e}{cursor:zoom-in}`)),n.appendChild(t)}},l=e=>{if(e instanceof HTMLElement)if("IMG"===e.tagName)e.addEventListener("click",s);else{const t=e.querySelector("img");t&&t.addEventListener("click",s)}};return i(),window.addEventListener("resize",i,{passive:!0}),d(e),{zoom:c,attach:d}};!function(){const e=document.createElement("style"),t=document.head||document.getElementsByTagName("head")[0];e.appendChild(document.createTextNode(".zoom-bg,.zoom-img{z-index:90000;cursor:zoom-out}.zoom-img{position:absolute;display:block;will-change:transform;transition:transform .3s ease-in-out}.zoom-bg{position:fixed;top:0;left:0;width:100vw;height:100vh;background:#121212;opacity:0;will-change:opacity;transition:opacity .3s ease-in-out}.zoom-bg.zoom-bg--reveal{opacity:.95}.zoom-original--hidden{visibility:hidden}")),t.appendChild(e)}();

*/

External Link for Silky Smooth Image Zoom and Lightbox

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Items in Image Zoom
Search Code
Search Code by entering your search text above.
Welcome to CSSBundle.com

Hi there and welcome to CSSBundle. Here at CSS Bundle we aim to find the best css and javascript bundles and demos and add them here to make them easy to find all in one place.

If you would like to stay up to date with our latest updates you can subscribe to our mailing list where we will send you one email per week with the latest updates, no spam emails just the latest updates.

Random Quote


Latest Code
css animations Falling Stars CSS Animation html Video different sources on screen sizes video embed html responsive css animations Animated Triangles on Canvas JS HTML HTML Canvas Instant colour fill with HTML Canvas javascript Transform Text into Images using SnapText JS CSS Tools Fancy Border Radius CSS Generator Tool - spin animation keyframes CSS Tools CSS Drop Filter Shadow Creator CSS Slider A CSS and HTML Only Carousel Slider CSS Z-Index Z-Index Code Front - CSS to change z-index when element is active sections section with image to the right text to the left gradient Sections Two Clickable Logo Promo Box Section Foundation HTML CSS Sections Inline Contact Form Formatting HTML CSS Sections Overview full width section in Poppins font with light gray background using foundation Interactive Images Map Image with Easy Dots and Titles using only CSS HTML Sections Social Header Links easy Drop in Code with Font Awesome Icons Sections Responsive Friendly Subscribe to our Newsletter Section CSS Tips Create a button with CSS, HTML and Center it! CSS Text Formatting Truncate Text to an amount of lines using line-clamp CSS Positions Item Positioning in CSS CSS Tips Draw a Circle in CSS css animations Magical Text Effect Bootstrap 4.2 Kitchen Sink CSS Frameworks Make your site look like windows 7 CSS Backgrounds Fancy up your banner image hero sections using overlay gradients CSS Modal Floating Modal Message Fixed on the Bottom of the Screen CSS Modal Create a modal with only CSS css grid display items in a css grid CSS Text Effects Using the selection selector to change the default highlight color Text Truncate your Text with CSS CSS Cursors Change your cursor into an Emoji Cursor or Image Cursor CSS How to Center Everything or Anything with CSS CSS Animations Smooth Scrolling with just CSS CSS Animations Radial Glow under cursor on box hover over effect CSS Shadows Using a filter drop shadow for transparent images CSS Animations Pure CSS 3D Flipping Book with Animations CSS Create a checkerboard background pattern with CSS CSS Animations button animations hover and fill animation CSS Animations button on focus swing animation CSS Animations Animated Button Border when Active Quick CSS create a border with top triangle in css CSS Animations Single Element CSS Spinners CSS Animations CSS Shake - Shake up your elements CSS Framework NES-style(8bit-like) CSS Framework CSS Tips Why to use a CSS Reset? Web Developer Checklists Front-End Checklist Icons Font Awesome Icons CSS Animation Animate Stuff with Animate.css CSS Animations Check Wave Click a Checkbox and Watch the Animation Animation Cloudy spiral animation with CSS3 Notifications Toast Messages and Notifications Standalone Library no JQuery - notify.js