javascript - Change statically positioned image with scroll -


I am trying to change the source of a statically illustrated image based on the window's scroll position.

Actually, I want to cycle through MRI scan images, where the viewport is relative to the page. I want to get all 100+ images compared to the length of the page. This length will change, so I am trying to work to make it proportional.

I believe that I am new to coding, so instead of trying the code, it is best to use the pseudocode:

  window.image1position == ( 1 / [#images]) * document.totalheight window.image2position == (2 / [#images]) * document.totalheight ... if window.scrollposition = = Window.image1position set image.src = image1.png elseif window .scrollposition == window.image2position set image.src = image2.png ...   

Would anyone be able to help me with this idea? Any input will be greatly appreciated.

Allen

Since you did not specify whether jQuery was acceptable, I Throwing something quickly to do this using plain javascript. Instead of first setting the appropriate scroll distance for each image, I chose to do so on the fly reference array index. (It assumes that you have element):

  // Keep track of them in store image sources Var imgArray = ["Img1.jpg", "img2.jpg", "img3.jpg"]; // Get the scroll height of the body, calculate the steps to move between the pictures. Http://stackoverflow.com/questions/1145850/how-to-get-height-of-entire-document-with-javascript var body = Document.body, html = document.documentElement; Var Totlheigt = Mathkmaks (Bodikskrollheigt, Bodikoffsetheigt, Htmlkcliaateigt, Htmlkskrollheigt, Htmlkoffsetheigt) - Vindokinrheigt; Var stepSize = totalHeight / imgArray.length; // Bind to scroll the event window. Formulas = function () {var scrolled = document.body.scrollTop; Var img = document.getElementById ("changeImg"); // set the right indicator to use, and swap src var imgIndex = Math.min (Math. Floor (scrolled / stepSize), imgArray.length - 1); Img.src = imgArray [imgIndex]; };   

This is to show you the code in action Hope it will help! Tell me if you have any questions.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -