0

I have aa div that I want to slide in once the user scrolls down a specified amount. It initially works but after than the div keeps moving to the left a little on every scroll action instead of staying in place. Anyone has an idea why is this happening?

  var opening = false;
  var closing = false; 
 $(window).scroll(function(){
     var windowHeight = $(window).height();
     var windowScroll = $(window).scrollTop(); 
     var position1 = $("#Support").offset().top;         
    if ( windowScroll > (position1 - (windowHeight/2)) )  
     {
        if (!opening) {
            opening = true;
            closing = false;
            $("#SupportImage1").stop().animate({
                left: "1200px"                  
              }, 1500, function(){
                  opening = false;
                  });
            }            
     }
     else
     {
        if (!closing) {
            closing = true;
            opening = false;
             $("#SupportImage1").stop().animate({
                left: "100%"                    
              }, 1400, function() {
                  closing = false;
              });                
        }
     }       
 });
KobeBryant
  • 1,341
  • 3
  • 10
  • 13

1 Answers1

0

the scroll bar is most likely affecting your width calculations.

everytime you run that function against the scroll bar you are adding in the total screen width plus the offset by the scroll bar.

Dan Kanze
  • 18,485
  • 28
  • 81
  • 134