html - Change Image to gif on scroll with jquery -


i trying change couple of images gifs scroll down page. have images changing gifs on scroll, gif resets scroll again. gif continue play if scrolling.

here jquery:

  $(window).scroll(function(){   var top = $(window).scrolltop();    var img1top = $('.wrap').offset().top;   if(img1top){     $('.wrap').attr('src','../wrap.gif');   };     var img2top = $('.vest').offset().top;   if(top=(img2top)){     $('.vest').attr('src','../vest.gif');   };    var img3top = $('.loop').offset().top;   if(top=(img3top)){     $('.loop').attr('src','../loop.gif');   };  }); 

1. code resetting src every time page scrolls, , although path may not changing, cause image refresh.

you need wrap each src change in conditional updates image if hasn't been updated.

2. if statements using = instead of ==, won't work.

3. doing == compare top of window top of elements not best way this, because if scroll fast enough, chances of event being called when 2 values identical very slim. should use >= instead.

$(window).scroll(function(){   var top = $(window).scrolltop();    var img1top = $('.wrap').offset().top;   if(img1top && $('.wrap').attr('src') != '../wrap.gif'){     $('.wrap').attr('src','../wrap.gif');   };     var img2top = $('.vest').offset().top;   if(top >= img2top && $('.vest').attr('src') != '../vest.gif'){     $('.vest').attr('src','../vest.gif');   };    var img3top = $('.loop').offset().top;   if(top >= img3top && $('.loop').attr('src') != '../loop.gif'){     $('.loop').attr('src','../loop.gif');   };  }); 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -