How i Created This Design

  • Scroll Event Listener: This JavaScript code adds an event listener to the window, which listens for scroll events as the user scrolls down the webpage.

  • Image Selection: It selects an image element within the HTML document by its ID, 'fixed-image,' and then further queries for an <img> element within it.

  • Scroll Position: The code captures the current vertical scroll position of the webpage, which represents how far the user has scrolled down the page.

  • Image Filenames: It defines an array called imageFilenames that should contain a list of image filenames. Each filename corresponds to a different scroll position on the page. These filenames determine which image is displayed based on how far the user has scrolled.

  • Threshold: A threshold value is defined, which determines the scrolling distance required to trigger a change in the displayed image. In your code, it's set to 19.

  • Image Index Calculation: The code calculates an imageIndex based on the scroll position and the threshold. This index corresponds to the position in the imageFilenames array, indicating which image should be displayed.

  • Index Limitation: The code ensures that the imageIndex does not go beyond the available images in the imageFilenames array.

  • Image Source Update: Finally, the code updates the src attribute of the selected <img> element with the filename determined by the imageIndex. As a result, the displayed image changes as the user scrolls down the webpage.



  • To Download This Project


  • Get Code
  •  

                

  •