before and after image slider

How we can create before and after comparison slider using JavaScript and CSS? Solution: Before and After Image Slider Using CSS JavaScript, Comparison Slide.

Previously I have shared a Comparison Slider, but this time the slider is with resizing cursor and less JS codes. Basically, a comparison slider is used to compare two images or check before and after effects of elements. If you search on the internet for this type of program, you will get lots of JS codes but this has 2 lines of JS.

Today you will learn to create comparison slider using pure JavaScript and CSS. This program will help to gain your CSS and JS knowledge because the program is based on these. You also can use this on your website or project, that will be fun. In this program, most of the functions based on CSS.

So, Today I am sharing Before and After Image Slider Using CSS and JavaScript. There are two images on is black & white and another is colorful, there is a slider range on the middle of two images and its works as a compare slider. This has a responsive design, and I have also placed extension codes like –WebKit –Moz, etc for supporting multiple browsers.

If you are thinking now how the before and after slider actually is, then see the preview given here below.

Preview of Comparison Image Slide

See this video preview to getting an idea of how this slide looks like.

Live Demo

Now you can see this visually, You also can see it live by pressing the button given above. If you like this, then get the source code of its.

You May Also Like:

Before and After Image Slider Using CSS JavaScript Source Code

Before sharing source code, let’s talk about it. First I have created the main div with class name ‘container‘ inside it, I have placed another div and an input range. For creating the input range I have used HTML <input type="range"> element and gave minimum value 0 and maximum 100, by default at starting its value is 50.

In the input range, I have placed a javascript function two times first for oninput="beforeAfter()"  (info) and second for onchange="beforeAfter()" . Now in the CSS file, I have done lots of works, like placing all the elements, place slider, etc. In the figure section, I have placed two images with absolute position and 50% width.

Now javascript detect the range slider using the function we have placed in HTML file. JS fetch the slider range status which is between 0 and 100 and put the same % of width to the image. That is the whole concept, left other things you will understand easily after getting the codes.

For creating this program you have to create 3 files, first for HTML, second for CSS and third for JavaScript. Follow the steps to creating this without any error.

index.html

Create an HTML file named ‘index.html‘ and put these codes given here below.

style.css

Now create a CSS file named ‘style.css‘ and put these codes given here.

function.js

The final step, Create a JavaScript file named ‘function.js‘ and put the codes.

That’s It. Now you have successfully created Before and After Image Slider Using CSS JavaScript, Comparison Slide. If you have any doubt or question comment down below.

Thanks For Visiting, Keep Visiting.

5 COMMENTS

  1. hi, this works well with the Semplice theme for WordPress. However, is it possible to change the design of the slider? I want to make the line thinner and use a different arrow set.

  2. Thank you for sharing this tip.

    But do you think it is possible to do an automatic before and after picture comparison just like some amazon product have?

  3. Great stuff thanks heaps!
    Could you please tell me how to change so I can multiple before – after images on the same web page (just under each other)?

LEAVE A REPLY

Please enter your comment!
Please enter your name here