javascript - Change image while scrolling the mouse wheel -
var myimages=[ "images/dad.png", "images/terminal.png", "images/hi.png", "images/hengameh.png", "images/shrinedefense.png" ] var slideshow=document.getelementbyid("slideshowers") var nextslideindex=0 function rotateimage(e){ var evt=window.event || e var delta=evt.detail? evt.detail*(-120) : evt.wheeldelta nextslideindex=(delta<=-120)? nextslideindex+1 : nextslideindex-1 nextslideindex=(nextslideindex<0)? myimages.length-1 : (nextslideindex>myimages.length-1)? 0 : nextslideindex slideshow.src=myimages[nextslideindex] if (evt.preventdefault) evt.preventdefault() else return false } var mousewheelevt=(/firefox/i.test(navigator.useragent))? "dommousescroll" : "mousewheel" if (slideshow.attachevent) slideshow.attachevent("on"+mousewheelevt, rotateimage) else if (slideshow.addeventlistener) slideshow.addeventlistener(mousewheelevt, rotateimage, false) <img id="slideshowers" class="cover-images" src="http://www.ammaryar.ir/images/dad.png" /> hi, want change image, while scrolling mouse wheel. can find want in following link: http://www.ammaryar.ir there lantern on right side, have 4 lantern different colors. want change these lanterns while scrolling mouse wheel. did something, it's not working properly. if move mouse on lantern picture , trying scroll, images change random , repeat. don't change randomly , repeat, have 5 menu tabs , each menu have specific image while scroll mouse wheel.
const pageheight = document.documentelement.scrollheight - window.innerheight, imgelement = document.getelementbyid('img'), images = ['http://lorempixel.com/400/200/city/1', 'http://lorempixel.com/400/200/city/2', 'http://lorempixel.com/400/200/city/3', 'http://lorempixel.com/400/200/city/4']; let lastimage = 0; document.addeventlistener('scroll', () => { let index = parseint(document.documentelement.scrolltop / pageheight * images.length); index = math.min(index, images.length - 1); // prevent few pixel overflow if (index !== lastimage) { // if need display different image lastimage = index; imgelement.src = images[lastimage]; } }); body { height: 700vh; margin: 0; } img { position: fixed; top: 0; } <img id="img" src="http://lorempixel.com/400/200/city/1"/> quoting myself another answer:
document.documentelement.scrollheight - window.innerheightheight interested in. height of entire document reduced height of viewport. why need reduction? because whatever our current scroll position, never scroll out of document, i.e. see portion of , portion equals viewport's height.document.documentelement.scrolltop / pageheightgive our scroll position relatively document's height, i.e. how many percent of document have scrolled. number in range 0 1, interested in range 0 4, hence multiplying.
Comments
Post a Comment