Skip to content

nonsalant/image-compare

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Accessible <image-compare> Web Component (no Shadow DOM)

  • Using Light DOM allows you to style everything with regular CSS
  • Horizontally scrollable container used as fallback if JS doesn't load (this also helps reduce CLS by stabilizing the overall height)
  • No dependencies

Demo

https://codepen.io/nonsalant/pen/gbYRJKd

Importing the component files

Importing from a CDN

<link rel="stylesheet" href="https://unpkg.com/image-compare-light-dom/image-compare.css">
<script type="module" src="https://unpkg.com/image-compare-light-dom/image-compare.js"></script>

Importing from a local copy

  1. Grab the files from the src folder.

  2. Load the style and the main script (adjust the paths as needed):

<link rel="stylesheet" href="../src/image-compare.css">
<script src="../src/image-compare.js" type="module"></script>

Usage

<image-compare>
    <img src="before.jpg" alt="" width="" height="">
    <img src="after.jpg" alt="" width="" height="">
</image-compare>

Options

Control how much of the first image is shown initially by adding an exposure attribute with a value between 0 and 100.

  • 0 means the first image is completely hidden.
  • 100 means the first image is fully visible.
  • 50 means both images are half-shown, half-hidden.

E.g: <image-compare exposure="0">

Credits

About

"Before/after"-style comparison slider. Keyboard accessible. Light DOM prevents CLS on Core Web Vitals and makes it easy to add styles.

Topics

Resources

License

Stars

Watchers

Forks

Contributors