Image comparison slider. Compare images before and after. Supports React, Vue, Angular.

Overview

img-comparison-slider

npm package Published on webcomponents.org

Slider component for comparing images (before and after).

Usage examples: https://sneas.github.io/img-comparison-slider

Example

Browsers support

IE / Edge
IE / Edge
Firefox
Firefox
Chrome
Chrome
Safari
Safari
iOS Safari
iOS Safari
Samsung
Samsung
IE11 (limited), Edge last 2 versions last 2 versions last 2 versions last 2 versions last 2 versions

Why yet another slider?

Because all existing solutions lack at least one of the features:

  • Mobile friendly
  • Accessible
  • Responsive
  • jQuery free
  • Compatible with modern frameworks (React, Angular, Vue2, Vue3)
  • Distributed through CDN or NPM

Installation

HTML

<script
  type="module"
  src="https://unpkg.com/img-comparison-slider@3/dist/component/component.esm.js"
>script>
<script
  nomodule=""
  src="https://unpkg.com/img-comparison-slider@3/dist/component/component.js"
>script>
<link
  rel="stylesheet"
  href="https://unpkg.com/img-comparison-slider@3/dist/collection/styles/initial.css"
/>

<img-comparison-slider>
  <img slot="before" src="before.jpg" />
  <img slot="after" src="after.jpg" />
img-comparison-slider>

Frameworks support

Styling

The component could be styled with the help of CSS3 variables.

Example:

<style type="text/css">
  img-comparison-slider {
    --divider-width: 2px;
    --divider-color: #c0c0c0;
    --handle-opacity: 0.3;
  }
style>

Available variables

Variable Description Default value
--divider-width Width of the vertical line dividing both images 1px
--divider-color Color of the vertical line dividing both images #d7d7d7
--handle-size Size of the handle in the middle of divider 40px
--handle-color Color of the handle in the middle of divider #d7d7d7
--handle-opacity Opacity of the handle 0.5
--handle-opacity-active On focus opacity of the handle 0
Comments
  • ReferenceError: document is not defined (nuxt3)

    ReferenceError: document is not defined (nuxt3)

    I am using the following ecosystem:

    "nuxt": "3.0.0-rc.4"
    "@img-comparison-slider/vue": "^7.6.0"
    

    I have imported the ImgComparisonSlider in the component as below. I have tried using it without the <client-only> tag as well.

    File : myComponent.vue

    <template>
      <div class="w-full bg-theme-primary-200 px-2 py-2">
        <client-only>
          <img-comparison-slider
            class="flex justify-center items-center slider-example-opacity-and-size"
            hover="hover"
          >
            <template #first>
              <figure class="before">
                <img
                  class="h-72 w-full object-cover"
                  :src="imageOne"
                  :alt="altOne"
                />
                <figcaption>Before</figcaption>
              </figure>
            </template>
    
            <template #second>
              <figure class="after">
                <img
                  class="h-72 w-full object-cover"
                  :src="imageTwo"
                  :alt="altTwo"
                />
                <figcaption>After</figcaption>
              </figure>
            </template>
          </img-comparison-slider>
        </client-only>
      </div>
    </template>
    
    <script>
    import { ImgComparisonSlider } from "@img-comparison-slider/vue";
    export default {
      props: {
        imageOne: {
          type: String,
        },
        altOne: {
          type: String,
        },
        imageTwo: {
          type: String,
        },
        altTwo: {
          type: String,
        },
      },
    };
    </script>
    

    The error I am getting is document is not defined. Error Log

    ReferenceError: document is not defined
        at D:\myProject\node_modules\img-comparison-slider\dist\index.js:1:3640
        at D:\myProject\node_modules\img-comparison-slider\dist\index.js:1:10336
        at Object.<anonymous> (D:\myProject\node_modules\img-comparison-slider\dist\index.js:1:10340)
        at Module._compile (node:internal/modules/cjs/loader:1101:14)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
        at Module.load (node:internal/modules/cjs/loader:981:32)
        at Function.Module._load (node:internal/modules/cjs/loader:822:12)
        at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:190:29)
        at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
        at async Promise.all (index 0)
    
    opened by ktx-drashti 8
  • [Edge 17] TypeError: Unable to get property 'define' of undefined or null reference

    [Edge 17] TypeError: Unable to get property 'define' of undefined or null reference

    Good day. I'm using this slider on my website and when I visit my website via Edge browser I get the following error:

    TypeError: Unable to get property 'define' of undefined or null reference
    

    Could you use a polyfill to avoid common errors? For example:

    defineElement = function( tag, opt ){
        if( typeof customElements !== 'undefined' ){
            customElements.define( tag, opt );
        } else {
            document.registerElement( tag, opt );
        }
    }
    class HTMLxSearchElement extends HTMLElement{
        constructor(){
            super();
            console.log('super');
        }
    }
    defineElement( 'x-search', HTMLxSearchElement )
    

    Thank you a lot.

    released 
    opened by TangoPJ 8
  • hint-icon is not visible all the time

    hint-icon is not visible all the time

    Hi! I love this wonderful plugin. I really appreciate your efforts in coding this great plugin. But I was wondering if there is an option to make hint-icon always visible on the images. For now, It disappears when we slide the image but If we want to make it visible all the time?

    released 
    opened by Usamaahmad751 8
  • Unable to setup with vue-cli (vue 3)

    Unable to setup with vue-cli (vue 3)

    Hi,

    Appreciate the library. While I was setting it up with vue-cli (using vue v3.0.0), the app was returning an "Uncaught Reference Error: Vue is not defined.". I looked into it and traced the error back to the index.browser.js file.

    I tried to rectify it by 'requiring' vue at the start of the file. I was able to remove the error and run the app, but the img slider shows the image vertically stacked on top of each other.

    Not sure if this is an issue or I'm operating the library a little wrong.

    Thanks for the help.

    released 
    opened by quinncheong 6
  • Before after img height

    Before after img height

    I have a small suggestion for this plug-in unit. if I upload two images with diffrent width and height , the style will be like this: image Could you help to update the two picture's or div's as same height.

    opened by aimilin 6
  • The component behaves incorrectly when an after image changes / loads with a delay

    The component behaves incorrectly when an after image changes / loads with a delay

    Hello,

    I did the installation for Angular according to the documentation and it is with error to pass the image using the mouse. What should I do to resolve?

    opened by villyaraujo 6
  • SSR support. Fix window is not defined error

    SSR support. Fix window is not defined error

    Is it possible to add check like if (typeof window !== 'undefined') in this place https://github.com/sneas/img-comparison-slider/blob/15f44f27d48f0f3a320eb540e4d8549e6fdc409b/src/index.ts#L396 ? Because for now Angular Universal throw error 'window is not defined'. I can create a PR if you do not mind

    enhancement released 
    opened by vpetrusevici 5
  • Before and After Images are in reverse order

    Before and After Images are in reverse order

    This screenshot is straight from the demo:

    Screen Shot 2021-06-16 at 2 18 30 PM

    The dog shows up on the left-hand side as the "after" picture and the cat shows up on the right-hand side as the "before" picture. This is really confusing. I expect the order to be reversed, first show a before and then show an after picture.

    To make it clear to users on the demo site, can we use the same image just with a filter? Say a cat image with Sepia filter? Or a day then night image as an example.

    If you look at https://www.remove.bg/ you will see what I mean. Left side is always before and right side is after.

    released 
    opened by yagudaev 5
  • Slider does not always goes to the full start/end and instead select text

    Slider does not always goes to the full start/end and instead select text

    Hey there, thanks for your library, super useful for me (I'll send you the website once I get it published).

    When using the slider, there's a small quirk I'd like your advice with, here it is in the video:

    https://user-images.githubusercontent.com/123822/116557401-b19dcb00-a8fe-11eb-8811-90df79913e1f.mp4

    In this video, I start by grabbing the handle with a click and never release the click. But we can still see that if I move fast then the slider does not go to the end of the image. Even when moving slowly it does not really go at the very beginning/end of the image sides.

    And instead, sometimes it will select text around.

    Is there a way to prevent this you think?

    Thanks!

    released 
    opened by vvo 5
  • How to change the slider position only by dragging the handle, and set hover effect to the second slot

    How to change the slider position only by dragging the handle, and set hover effect to the second slot

    As the video, how to trigger slider only by dragging the handle and get "--exposure" value in the second element to set block width for hover

    https://user-images.githubusercontent.com/38325453/195651743-afb36a31-b4b4-4bfd-92bd-185fa9b2c387.mov

    released 
    opened by JarvisChao 4
  • Add vertical slider option

    Add vertical slider option

    closes #56

    Hi, I needed this feature for a custom project as well, so I built it 😄 I added a new attribute direction which can be set to horizontal (which is default) or vertical, but I'm not sure if this should be switched around. I also thought about adding new keyboard controls when the slider is sliding vertically using ArrowUp and ArrowDown, but also wasn't sure if this makes sense.

    released 
    opened by alexdanylyschyn 4
  • Enhancement: Divider Height

    Enhancement: Divider Height

    Hello Sneas, awesome slider lib!

    Requesting additional feature to modify the height of the divider...it would be modifiable the same way the width is.

    --divider-width: 1px --divider-height: 100%

    Regardless of direction you choose, thanks in advance.

    opened by shanestreator 0
  • Feature Request: Svelte support

    Feature Request: Svelte support

    Hello everyone, I have tried this package before on NuxtJS and it's working perfect. I do like it a lot and thinking of using this package again with SvelteKit, but it seem like it isn't supported at the moment. So, will img-comparison-slider support Svelte anything soon? Or is there a way I can make it working with Svelte at the moment? Thanks.

    enhancement 
    opened by VisionaryAppDev 3
  • vue3: ImgComparisonSlider component does not pass props down to the web component

    vue3: ImgComparisonSlider component does not pass props down to the web component

    Code Example

    <template>
      <ImgComparisonSlider direction="vertical">
        <!-- eslint-disable -->
        <img
            slot="first"
            style="width: 100%"
            src="https://img-comparison-slider.sneas.io/demo/images/before.webp"
        />
        <img
            slot="second"
            style="width: 100%"
            src="https://img-comparison-slider.sneas.io/demo/images/after.webp"
        />
        <!-- eslint-enable -->
      </ImgComparisonSlider>
    </template>
    
    <script>
    import { ImgComparisonSlider } from '@img-comparison-slider/vue'
    
    export default {
      name: 'App',
      components: {
        ImgComparisonSlider
      }
    }
    </script>
    

    Expected Behavior

    direction option is applied and working.

    Current Behavior

    direction option does not take effect.

    Some Insights

    1. The ImgComparisonSlider is a simple wrapper for the web component. It could be found in https://github.com/sneas/img-comparison-slider/blob/5d201518be32307d81e8d51028b51c760aca0956/bindings/vue/src/index.ts
    2. The example in action could be found in https://github.com/sneas/img-comparison-slider-vue-example
    3. The attribute gets applied by HMR (run the app with npm start and change the direction option back and forth. It will be applied).
    help wanted 
    opened by sneas 1
  • Doesn't work quite right with transparent images

    Doesn't work quite right with transparent images

    When using images with an alpha channel (e.g. transparent PNGs), the existing masking approach doesn't work quite right.

    The current approach assumes that .first-overlay will cover .second. When transparent images are in use, that's not a safe assumption - .second "shows through" where it shouldn't.

    Fortunately, I was able to find a workaround in which I composited my background onto the images themselves. However, for a fully responsive design, it would be better to support transparent images natively.

    enhancement 
    opened by rinogo 1
  • Feature Request: Enable Animation

    Feature Request: Enable Animation

    Hi @sneas, hope you are doing well Is it possible to add a feature to animate the divider? :)

    As done in this slider: https://github.com/OnurErtugral/react-image-comparison-slider

    Thanks & Regards, Antariksh

    opened by aapatre 2
Releases(img-comparison-slider-v7.8.1)
Image viewer component for vue, supports rotation, scale, zoom and so on, based on viewer.js

Image viewer component for vue, supports rotation, scale, zoom and so on, based on viewer.js

mirari 2.1k Dec 27, 2022
Image viewer component for vue, supports rotation, scale, zoom and so on, based on viewer.js

v-viewer Image viewer component for vue, supports rotation, scale, zoom and so on, based on viewer.js Live demo Examples directive component thumbnail

hua chen yu 0 Dec 3, 2020
Image viewer component for vue 3.x, supports rotation, scale, zoom and so on, based on viewer.js

v-viewer Image viewer component for vue, supports rotation, scale, zoom and so on, based on viewer.js v-viewer for vue2 Live demo Quick Example direct

mirari 14 Oct 29, 2022
Vue image placeholder directive for broken images.

Vue Image Fallback v-img-fallback Vue image placeholder directive for broken images. If you like this project, please give it a star, and consider fol

Joff Tiquez 46 Oct 13, 2022
An image finder application leveraging the Pexels api, You can also download your favorite Images too

Pexelry ?? Pexelry is an image finder web application that leverages the Pexels api to find images related to a specific search. Prerequisites Get an

Junior Developer 4 Aug 25, 2022
vue-image-markup will provide you to edit uploaded image easily and save it.

Markup Image with Vue.js (customizable) vue-image-markup will provide you to edit uploaded image easily and save it. Installation npm i vue-image-mark

Lionix 92 Dec 9, 2022
A Vue component for showing loader during image loading https://john015.github.io/vue-load-image/

English | 한국어 Vue-load-image Vue-load-image is 1KB(gzipped size) minimalist Vue component that display loader during image loading and display alterna

Sangwon Lee 60 Dec 7, 2022
Vue-image-loader: image loader for vue js with prevue

Vue-image-loader: image loader for vue js with prevue

null 0 Feb 1, 2020
🌈 A Vue image component. Simple realization of image shadow.

?? vue-image-shadow A Vue image component. Simple realization of image shadow. ?? Example Online: https://image-component.github.io/vue-image-shadow/

null 21 Aug 13, 2022
A Directive for setting fall back image in a vue js application if in case image is not loaded

vue-fall-back-image-directive A Directive for setting fall back image in a vue js application. Installation npm install Run npm install --save vue-fal

Arun sivan 1 Oct 15, 2019
Collecting images and photos like a pro.

Scrapbook Collecting images and photos like a pro. Demo demo Why When you have a tons of photos you took while your traveling, or art images you downl

Fumiya A 5 Jan 31, 2022
A vue component that allows your images to open in full size.

vue-expandable-image Allows your images to open in full size. Demo Preview Setup npm install vue-expandable-image You have two ways to setup vue-expa

Taha Shashtari 28 Oct 16, 2022
An interactive vue component that displays multiple images in a row

vue image wall An interactive vue component that displays multiple images in a row. By hovering mouse over one of the images, that image will expand i

Zijie Wu 18 Jun 21, 2022
V-img is a plugin for Vue.js that allows you to show images in full-screen gallery

V-img is a plugin for Vue.js that allows you to show images in full-screen gallery

Chantouch 0 Dec 2, 2019
Webpack loader used for inline replacement of SVG images with actual content of SVG files in Vue projects.

vue-svg-inline-loader Webpack loader used for inline replacement of SVG images with actual content of SVG files in Vue projects. Loader parses only HT

Oliver Findl 118 Jan 6, 2023
Optimized Images for Nuxt

Optimized Images for Nuxt

Nuxt 808 Jan 5, 2023
A React-Photo-Collage But For Vue.js

React Photo Collage Installation npm install react-photo-collage Props Name Type Required Default Description width string false 800px height an arra

Benny Hsieh 23 Dec 28, 2022
Mobile-friendly picture file input Vue.js component with image preview, drag and drop, EXIF orientation, and more.

vue-picture-input Mobile-friendly picture file input Vue.js component with image preview, drag and drop, EXIF orientation, and more. Installation npm

Robert 0 Jun 14, 2018
Lightweight and simple image gallery component for Vue.js

vue-image-box Description Lightweight and simple-ish image gallery component for Vue.js. Images can also be cycled through using the left & right arro

Adam Romig 2 Oct 3, 2021