How to compare two images using Image comparison slider? Last Updated : 25 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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:Create an HTML file in which we are going to add the main div, further we are adding two div's in it for adding images in them.Create a CSS file to add images in the respective div.Create a JavaScript file for changing the direction and compare the 2 images.HTML Code:Firstly, create an HTML file (index.html).Now after the creation of our HTML file, we are going to give a title to our webpage using the <title> tag. It should be placed between the <head> tag.Then we link the CSS file that provides all the background images to our HTML. This is also placed in between the <head> tag.Coming to the body section of our HTML code.Firstly, create a main div as the main box.In that div, add 2 more divs to add image 1 and image 2.At the end of our body add <script> tag which links the JavaScript file with our HTML file. index.html <!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.Restore all the browser effects.Use classes and ids to give effects to HTML elements. style.css /* 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 Detection of mouse movement for movement of images.Use of different keyboard keys in event listeners for vertical and horizontal movement.document.querySelector('css_selector') => returns the first element that matches with specified CSS selector.Guide for the vertical and horizontal movementDefault horizontal movement of the slider.Vertical movement is active when the user presses the (v) key once.Horizontal movement is active again when the user presses the (h) key once.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. index.js // 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. Comment More infoAdvertise with us Next Article How to compare two images using Image comparison slider? R rahulmahajann Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies HTML CSS JavaScript-Questions +2 More Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript 11 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon 8 min read Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are 10 min read AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of 4 min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read Sliding Window Technique Sliding Window Technique is a method used to solve problems that involve subarray or substring or window. The main idea is to use the results of previous window to do computations for the next window. This technique is commonly used in algorithms like finding subarrays with a specific sum, finding t 13 min read Like