Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a Div to return to its original position when scrolled up

I have a div with animation when you scrolls down and up, the problem is that when I scroll up and down very fast without letting the div to complete its animation the div little by little gets out of the screen in the upper part.

If I remove the .stop() in the .animate() function and scroll up and down very fast, the div keeps doing this for a while.

I want to keep the animation when scrolled up and down with the only difference that the menu does not get out of the screen when scrolling up and down fast, I have look thorough stack overflow questions like this one but nothing that I found work the code of the jsfiddle can be found here:

http://jsfiddle.net/QLLkL/4/

$(window).scroll(function(){
    if($(window).scrollTop() > 480 && !animationStarted){
         $("#menu").stop().animate({ "top": "+=180px" }, 1000);
        animationStarted  = true;
    };
    if($(window).scrollTop() < 480 && animationStarted){
         $("#menu").stop().animate({ "top": "-=180px" }, 1000);          
        animationStarted  = false;
    };        
});
like image 631
user2879138 Avatar asked Dec 10 '13 04:12

user2879138


People also ask

How do I make a div move up and down when I'm scrolling the page?

Just add position: fixed; in your div style. I have checked and Its working fine in my code.

How do I fix a div at the top?

It's the position:fixed that's most important, because it takes the top div from the normal page flow and fixes it at it's pre-determined position. It's also important to use the padding-top:1em because otherwise the term-defs div would start right under the top div.


3 Answers

Why are you using "+=" and "-="? The fact is that when you scroll up without the animation to complete, the current value is taken and '567px' are subtracted from it. I suggest you make it to "567px" and "0px" respectively. You could even try the CSS3 transitions in case you are sure to not target Internet Explorer 9.

Refer an example here: http://jsfiddle.net/myTerminal/QLLkL/6/

$(window).scroll(function(){
        if($(window).scrollTop() > 480 && !animationStarted){
             $("#menu").addClass("bottom");
            animationStarted  = true;
        };
        if($(window).scrollTop() < 480 && animationStarted){
             $("#menu").removeClass("bottom");
            animationStarted  = false;
        };        
});

Edit: Updated example, works with Firefox: http://jsfiddle.net/myTerminal/QLLkL/13/ missed to set "top: 0px" earlier.

like image 140
myTerminal Avatar answered Oct 27 '22 10:10

myTerminal


http://jsfiddle.net/prollygeek/QLLkL/14/

$(document).ready(function(){
    var animationStarted = false;
    var x=$("#menu").css("top")   
    $(window).scroll(function(){    
        if($(window).scrollTop()>x)
        {
            $("#menu").stop().animate({ "top": x+"px" }, 20);
        }
        else
        {
            $("#menu").stop().animate({ "top": $(window).scrollTop()+"px" }, 20);
        }
        // animationStarted  = true;              
    });
});

This should fix it all .

like image 38
ProllyGeek Avatar answered Oct 27 '22 09:10

ProllyGeek


$(document).ready(function(){
    var animationStarted = false;

      $(window).scroll(function(){
        if($(window).scrollTop() > 1 && !animationStarted){
            $("#menu").stop().animate({ "top": $(window).scrollTop()+"px" }, 20);
           // animationStarted  = true;
        };
        if($(window).scrollTop() < 1 && animationStarted){
             ("#menu").stop().animate({ "top":$(window).scrollTop()-50+"px" }, 20);
           // animationStarted  = false;
        };        
    });
});
like image 41
anuj baliyan Avatar answered Oct 27 '22 10:10

anuj baliyan