Open In App

How to compare two images using Image comparison slider?

In this project, we are going to create an image slider with which we can check 2 images. If we are making an exact copy of them. We can compare every single boundary of the first image with the second image in both the vertical and horizontal directions.

Approach:



HTML Code:




<!DOCTYPE html>
<html lang="en">
<head>    
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>
      PRESS:'v' for vertical movement & 'h' \
      for horizontal movement
    </h1>
    <div class="main_box">
        <div class="img1"></div>
        <div class="img2"></div>
    </div>
    <script src="index.js"></script>
</body>
</html>

CSS Code: The following is the content for the “style.css” file used in the above code. CSS is used to give different types of animations and effects to our HTML page so that it looks interactive to all the users. Consider the following points.






/* restoring all of the browser effects */
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    overflow: hidden;
}
  
/* same effects to the body */
body{
    background-color: #000;
    color: #fff;
}
  
/* positioning of heading */
h1{
    display: flex;
    justify-content: center;
}
  
/* positions of main div and 2 images */
.main_box,.img1,.img2{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
  
.main_box{
      
    margin-top: 2em;
    margin-bottom: 2em;
}
  
.img1{
    background-image: url(image-url);
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}
  
.img2{
    background-image: URL(image-url);
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    left: 50%;
    background-attachment: fixed;
    border-top: solid red 5px;
    border-left: solid red 5px;
}

JavaScript Code: In the following, we write code for 

document.querySelector('css_selector') 
=> returns the first element that matches with specified CSS selector.

Guide for the vertical and horizontal movement

Note: The v and h are only valid in a small case.

The following is the content for “index.js” file used in the above HTML code.




// calling all of the Html classes
const img2 = document.querySelector('.img2')
  
// horizontal movement
window.addEventListener('keydown',(e)=>{
    if(e.key == 'h'){
        window.addEventListener('mousemove',(e)=>{
            img2.style.left = e.clientX +'px'
            img2.style.top = 0 +'px'
        })
    }
})
  
// vertical movement
window.addEventListener('keydown',(e)=>{
    if(e.key == 'v'){
        window.addEventListener('mousemove',(e)=>{
            img2.style.left = 0 +'px'
            img2.style.top = e.clientY +'px'
        })
    }
})
  
// default movement of slider which is horizontal movement
window.addEventListener('mousemove',(e)=>{
    img2.style.left = e.clientX + 'px'
    img2.scroll.top = 0 + 'px'
})

Output: In this way, you can compare your 2 images together.


Article Tags :