• We just launched and are currently in beta. Join us as we build and grow the community.

Creating an Image Comparison Slider using HTML, CSS, and JavaScript Tutorial

Caretux

Waifu Economist
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
199
Likes
118
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, you will learn how to create a simple Image Comparison Slider using HTML, CSS, and JavaScript. The main purpose of this tutorial is to provide the students and beginners with a reference for learning to build a usable feature for websites. Here, I will be providing a simple web page source code that demonstrates the creation and functionality of an Image Comparison Slider.

How does the Image Comparison Slider work?

Image Comparison Slider is a feature that allows the user to compare the difference between two images. The images overlay each other. The vertical line serves as the slider where users can slide or move it from left to right or vice versa. The images are partly shown side-by-side and when the slider has been moved, the top image will display area will change to allow the user to see the part of the other image.

css-js-image-comparison-demo.gif


How to create an Image Comparison Slider?

The Image Comparison Slider is not that complicated to create. Using CSS, we can design the comparison container of images where the images overlaying to each other and are partly shown side-by-side. We can simply add an HTML element that will serve as the vertical line or the slider such as the <span></span>. And for the functionality of the comparison feature, JavaScript event listeners will be helpful for updating the with of image area and the X-axis position of the slider.

Example

Here are the scripts of a simple web page that I created that demonstrate the interface and functionality of an Image Comparison Slider.

Interface

The script below is the HTML file script of the web page where the images are loaded.

index.html

  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <meta

    charset

    =

    "UTF-8"

    >
  5. <meta

    http-equiv

    =

    "X-UA-Compatible"

    content

    =

    "IE=edge"

    >
  6. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  7. <title

    >

    HTML, CSS, and JS - Image Compare</

    title

    >
  8. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.googleapis.com"

    >
  9. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.gstatic.com"

    crossorigin>
  10. <link

    href

    =

    "https://fonts.googleapis.com/css2?family=Mynerve&display=swap"

    rel

    =

    "stylesheet"

    >
  11. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  12. </

    head

    >
  13. <body

    >
  14. <section

    >
  15. <h1

    class

    =

    "text-center"

    ><b

    >

    Creating a Image Compare using HTML and CSS</

    b

    ></

    h1

    >
  16. <hr

    width

    =

    "50px"

    >
  17. <!-- Image Comparison Container -->
  18. <div

    class

    =

    "img-comparison-container"

    >
  19. <div

    class

    =

    "img-comparison-item"

    >
  20. <img

    src

    =

    "image2.jpg"

    alt

    =

    "Sample Image"

    /

    >
  21. </

    div

    >
  22. <div

    class

    =

    "img-comparison-item"

    >
  23. <img

    src

    =

    "image1.jpg"

    alt

    =

    "Sample Image"

    /

    >
  24. </

    div

    >
  25. <span

    ></

    span

    >
  26. </

    div

    >
  27. <!-- End of Image Comparison Container -->
  28. </

    section

    >
  29. <script

    src

    =

    "image-comparison.js"

    ></

    script

    >
  30. </

    body

    >
  31. </

    html

    >

Without any CSS for the image comparison slider design, the images will look like the following image.

css-js-image-comparison-no-design.jpg


Stylesheet

The script below is the CSS file which contains the styles script of the web page layout and image comparison elements design.

style.css

  1. :

    root

    {
  2. --mynerve-font

    :

    'Mynerve'

    ,

    cursive

    ;
  3. --light-color

    :

    #f9f9f9

    ;
  4. --dark-color

    :

    #20262E

    ;
  5. }

  6. body,
  7. html{
  8. height

    :

    100%

    ;
  9. width

    :

    100%

    ;
  10. }
  11. h1{
  12. font-family

    :

    var

    (

    --mynerve-font

    )
  13. }
  14. .text-center{
  15. text-align

    :

    center

    ;
  16. }
  17. html{
  18. overflow

    :

    hidden

    ;
  19. }
  20. body{
  21. background-color

    :

    var

    (

    --dark-color

    )

    ;
  22. color

    :

    var

    (

    --light-color

    )

    ;
  23. overflow

    :

    auto

    ;
  24. margin

    :

    0

    ;
  25. padding

    :

    0

    ;
  26. }
  27. img{
  28. width

    :

    600px

    ;
  29. }
  30. /*
  31. * Image Comparison
  32. */
  33. .img-comparison-container{
  34. margin

    :

    auto

    ;
  35. width

    :

    80vw

    ;
  36. height

    :

    75vh

    ;
  37. display

    :

    flex;
  38. position

    :

    relative

    ;
  39. }
  40. .img-comparison-container>

    .img-comparison-item{
  41. width

    :

    100%

    ;
  42. height

    :

    100%

    ;
  43. position

    :

    absolute

    ;
  44. top

    :

    0

    ;
  45. left

    :

    0

    ;
  46. overflow

    :

    hidden

    ;
  47. transition

    :

    all .1s

    ease-in-out;

  48. }
  49. .img-comparison-container>

    .img-comparison-item>

    img{
  50. height

    :

    75vh

    ;
  51. width

    :

    80vw

    ;
  52. object-fit

    :

    cover;
  53. object-position

    :

    center

    center

    ;
  54. position

    :

    absolute

    ;
  55. top

    :

    0

    ;
  56. left

    :

    0

    ;
  57. }
  58. .img-comparison-container>

    .img-comparison-item

    :

    nth-child

    (

    1

    )

    {
  59. z-index

    :

    1

    ;
  60. }
  61. .img-comparison-container>

    .img-comparison-item

    :

    nth-child

    (

    2

    )

    {
  62. z-index

    :

    2

    ;
  63. }
  64. .img-comparison-container>

    span{
  65. position

    :

    absolute

    ;
  66. height

    :

    100%

    ;
  67. border

    :

    2px

    solid

    #fff

    ;
  68. z-index

    :

    3

    ;
  69. cursor

    :

    col-resize;
  70. transition

    :

    all .1s

    ease-in-out;
  71. }

Using the style provided above, the images will look like the following snapshot.

css-js-image-comparison.jpg


JavaScript

Here's the following JavaScript file script that contains the code for making the image comparison slider functional.

image-comparison.js

  1. const

    ImageCompareContainer =

    document.querySelectorAll

    (

    '.img-comparison-container'

    )
  2. const

    compareImage =

    function

    (

    containerEl)

    {
  3. var

    _ci_container =

    containerEl
  4. var

    _ci_width =

    _ci_container.clientWidth
  5. var

    _ci_slider =

    _ci_container.querySelector

    (

    'span'

    )
  6. var

    _ci_img2 =

    _ci_container.querySelector

    (

    '.img-comparison-item:nth-child(2)'

    )
  7. _ci_img2.style

    .width

    =

    (

    _ci_width /

    2

    )

    +

    'px'
  8. _ci_slider.style

    .left

    =

    (

    (

    _ci_width /

    2

    )

    -

    1

    )

    +

    'px'
  9. _ci_slider.addEventListener

    (

    'mousedown'

    ,

    function

    (

    e)

    {
  10. e.preventDefault

    (

    )
  11. console.log

    (

    'Slider'

    )
  12. containerEl.addEventListener

    (

    'mousemove'

    ,

    move_slider)
  13. containerEl.addEventListener

    (

    'mouseup'

    ,

    function

    (

    )

    {
  14. containerEl.removeEventListener

    (

    'mousemove'

    ,

    move_slider)
  15. }

    )
  16. }

    )
  17. function

    move_slider(

    e)

    {
  18. var

    _container_margin =

    containerEl.offsetLeft
  19. if

    (

    (

    e.clientX

    -

    _container_margin)

    >

    0

    &&

    (

    e.clientX

    -

    _container_margin)

    <=

    (

    containerEl.clientWidth

    -

    _ci_slider.clientWidth

    )

    )

    {
  20. _ci_img2.style

    .width

    =

    (

    e.clientX

    -

    _container_margin)

    +

    'px'
  21. _ci_slider.style

    .left

    =

    (

    e.clientX

    -

    _container_margin)

    +

    'px'
  22. }

  23. }
  24. }

  25. ImageCompareContainer.forEach

    (

    ICC =>

    {
  26. compareImage(

    ICC)
  27. }

    )

  28. window.onresize

    =

    function

    (

    )

    {
  29. ImageCompareContainer.forEach

    (

    ICC =>

    {
  30. compareImage(

    ICC)
  31. }

    )
  32. }

Here's the overall output of the web page script that I provided above.

css-js-image-comparison-demo.gif


I have provided also the complete source code zip file of the web page scripts on this website and it is free to download. The download button is located below this tutorial's content. Feel free to download and do some experiments with the code itself to understand it more and enhance your programming capabilities.

That's it! I hope this Creating an Image Comparison Slider using HTML, CSS, and JavaScript Tutorial will help you with what you are looking for and will e useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top