
We are going to add the functionality to our web-page so that whenever the user scrolls up or scrolls down on the image, then the image changes. We have used only 3 images but it can easily be expanded for multiple images.
We are keeping the images on top of each other, this makes sure only one image is visible at a time. When we scroll, we decrement the z-coordinate of the current image and increments the z-coordinate of the new image. By doing this the new image overlays the old image and it comes on top of all images and becomes visible.
- HTML Code: It is used to create a basic structure to include images.
<!DOCTYPE html><html><head><metacharset="utf-8"/><title>Change Image Dynamicallywhen User Scrolls</title></head><body><h1>GeeksforGeeks</h1><b>A Computer Science Portal for Geeks</b><divid="scroll-image"><imgsrc="CSS.png"class="test"/><imgsrc="html.png"class="test"/><imgsrc="php.png"class="test"/></div></body></html>chevron_rightfilter_none - CSS Code: Css is used to desing the structure. The position property is the most important things here. It will make all the images to appear on top of each other.
<style>body {text-align: center;}h1 {color: green;}img {position: absolute;left: 300px;}</style>chevron_rightfilter_none - Javscript code: In this section we will add JavaScript code to perform the scrolling on the image.
<script>window.onload =function() {// Index of current image// which is on displayvarimageIndex = 0;// Object array of all the// images of class testvarimages =document.getElementsByClassName('test');// This tells us if mouse if over// image or not, We only change// image if mouse if over itvarisMouseOverImage =false;// Object of parent element// containing all imagesvarscrollImages =document.getElementById('scroll-image');// Stores the current scoll co-ordinates// so that the window don't scroll down// while scrolling the imagesvarx, y;// This function sets the scroll to x, yfunctionnoScroll() {window.scrollTo(x, y);}// The following event id fired once when// We hover mouse over the imagesscrollImages.addEventListener("mouseenter",function() {// We store the current page// offset to x,yx = window.pageXOffset;y = window.pageYOffset;// We add the following event to// window object, so if we scroll// down after mouse is over the// image we can avoid scrolling// the windowwindow.addEventListener("scroll", noScroll);// We set isMouseOverImage to// true, this means Mouse is// now over the iamgeisMouseOverImage =true;});// The following function is fired// when mouse is no longer over// the imagesscrollImages.addEventListener("mouseleave",function() {// We set isMouseOverImage to// false, this means mouse is// not over the iamgeisMouseOverImage =false;// We remove the event we previously// added because we are no longer// over the image, the scroll will// now scroll the windowwindow.removeEventListener("scroll", noScroll);});// The following function is called// when we move mouse wheel over// the imagesscrollImages.addEventListener("wheel",function(e) {// We check if we are over// image or notif(isMouseOverImage) {varnextImageIndex;// The following condition// finds the next image// index depending if we// scroll up or scroll downif(e.deltaY > 0)nextImageIndex = (imageIndex + 1)% images.length;elsenextImageIndex =(imageIndex - 1+ images.length)% images.length;// We set the z index of current// image to 0images[imageIndex].style.zIndex ="0";// We set the z index of next// image to 1, this makes// The new image appear on top// of old imageimages[nextImageIndex].style.zIndex ="1";imageIndex = nextImageIndex;}});}</script>chevron_rightfilter_none
Final Solution: In this section we will combine the avobe three section.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title> Change image dynamically when user scrolls </title> <style> body { text-align: center; } h1 { color: green; } img { position: absolute; left: 300px; } </style> </head> <body> <h1>GeeksforGeeks</h1> <b>A Computer Science Portal for Geeks</b> <div id="scroll-image"> <img src="CSS.png" class="test" /> <img src="html.png" class="test" /> <img src="php.png" class="test" /> </div> <script> window.onload = function() { // Index of current image // which is on display var imageIndex = 0; // Object array of all the // images of class test var images = document.getElementsByClassName('test'); // This tells us if mouse if over // image or not, We only change // image if mouse if over it var isMouseOverImage = false; // Object of parent element // containing all images var scrollImages = document.getElementById('scroll-image'); // Stores the current scoll co-ordinates // so that the window don't scroll down // while scrolling the images var x, y; // This function sets the scroll to x, y function noScroll() { window.scrollTo(x, y); } // The following event id fired once when // We hover mouse over the images scrollImages.addEventListener( "mouseenter", function() { // We store the current page // offset to x,y x = window.pageXOffset; y = window.pageYOffset; // We add the following event to // window object, so if we scroll // down after mouse is over the // image we can avoid scrolling // the window window.addEventListener("scroll", noScroll); // We set isMouseOverImage to // true, this means Mouse is // now over the iamge isMouseOverImage = true; }); // The following function is fired // when mouse is no longer over // the images scrollImages.addEventListener( "mouseleave", function() { // We set isMouseOverImage to // false, this means mouse is // not over the iamge isMouseOverImage = false; // We remove the event we previously // added because we are no longer // over the image, the scroll will // now scroll the window window.removeEventListener( "scroll", noScroll); }); // The following function is called // when we move mouse wheel over // the images scrollImages.addEventListener( "wheel", function(e) { // We check if we are over // image or not if (isMouseOverImage) { var nextImageIndex; // The following condition // finds the next image // index depending if we // scroll up or scroll down if (e.deltaY > 0) nextImageIndex = (imageIndex + 1) % images.length; else nextImageIndex = (imageIndex - 1 + images.length) % images.length; // We set the z index of current // image to 0 images[imageIndex].style.zIndex = "0"; // We set the z index of next // image to 1, this makes // The new image appear on top // of old image images[nextImageIndex].style.zIndex = "1"; imageIndex = nextImageIndex; } }); } </script> </body> </html> |
Output:

Note: The above code will change image only if mouse if over the image.
Recommended Posts:
- Change Image Dynamically when User Scrolls Down | Set 2
- How to detect when user scrolls to the bottom of a div ?
- How to change/update image size dynamically using amp-bind in Google AMP ?
- How to change/update image dynamically using amp-bind in Google AMP ?
- How to change style attribute of an element dynamically using JavaScript ?
- How to dynamically change the title of web page using JavaScript ?
- How to create an image element dynamically using JavaScript ?
- How to change/update CSS class dynamically using amp-bind in Google AMP ?
- How to change/update text dynamically using amp-bind in Google AMP ?
- How to change favicon dynamically?
- How to dynamically insert id into table element using JavaScript ?
- How to merge properties of two JavaScript objects dynamically?
- How to get the javascript function parameter names/values dynamically?
- How to Create a Form Dynamically with the JavaScript?
- How to dynamically create new elements in JavaScript ?
- How to add elements to an existing array dynamically in JavaScript ?
- Dynamically Execute JavaScript in ElectronJS
- MySQL | Change User Password
- How to change the text and image by just clicking a button in JavaScript ?
- How to get the new image URL after refreshing the image using JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


