Search code examples
javascriptparallax

Trying to create a if statement in a parallax function


I'm trying to change the direction of the bananas between a scrolltop of 400 to 800 but it is not working what m'I doing wrong thanks a lot.

function parallaxbananas(){

var scrolltop = window.pageYOffset; // get number of pixels document has scrolled vertically 

if(scrolltop < 400) {
banana.style.top = -scrolltop * .2 + 'px' ;// move bubble1 at 20% of scroll rate
banana2.style.top = -scrolltop * .5 + 'px';// move bubble2 at 50% of scroll rate
banana2.style.left = -scrolltop * .5 + 'px' ;
know.style.top=scrolltop;
}else if(400 < scrolltop < 800){
banana.style.top = scrolltop * .2 + 'px' ;// move bubble1 at 20% of scroll rate
banana2.style.top = scrolltop * .5 + 'px';// move bubble2 at 50% of scroll rate
banana2.style.left = scrolltop * .5 + 'px' ;
}

}


Solution

  • just got it

    function parallaxbananas(){
    
    var scrolltop = window.pageYOffset; // get number of pixels document has scrolled vertically 
    
    if(scrolltop < 400) {
    banana.style.top = -scrolltop * .2 + 'px' ;// move bubble1 at 20% of scroll rate
    banana2.style.top = -scrolltop * .5 + 'px';// move bubble2 at 50% of scroll rate
    banana2.style.left = -scrolltop * .5 + 'px' ;
    know.style.top=scrolltop;
    }else if(400 < scrolltop && scrolltop < 800){
     var scrolltop2= 400 - scrolltop;
    banana.style.top = banana.style.top --- scrolltop2 * .2 + 'px' ;// move bubble1 at 20% of scroll rate
    banana2.style.top = banana2.style.top --- scrolltop2 * .5 + 'px';// move bubble2 at 50% of scroll rate
    banana2.style.left = scrolltop2 ;
    know.style.top=scrolltop2;
    }
    
    }