1

If the user is scrolling, I want to hide the navigation -

I am using (window).scroll and trying to make it into an if/else function with no luck.

Is .scroll not the right approach because it is a singular action?

$(document).ready(function(){

  if $(window).scroll(function() {
      $(".navbar").fadeOut(400);
    }
  else { 
      $(".navbar").fadeIn(400);
    });
});
Joe Isaacson
  • 4,012
  • 6
  • 30
  • 40
  • 1
    "if/else function" what? – Yury Tarabanko Oct 16 '13 at 16:19
  • @YuryTarabanko as it says in the code, fadeOut and fadeIn – Joe Isaacson Oct 16 '13 at 16:21
  • 1
    @JoeIsaacson You're inventing a syntax that doesn't exist. If you don't know JavaScript, consider learning it instead of trying to guess the syntax. – Šime Vidas Oct 16 '13 at 16:23
  • 1
    Looks at this [example](http://jsfiddle.net/LJVMH/). I think it does what you intend to do. – Danny Oct 16 '13 at 16:23
  • 1
    You have misinterpreted how to use the scroll function here. You need a timer to trigger an event when a user stops scrolling. Check out this answer for more information: http://stackoverflow.com/questions/9144560/jquery-scroll-detect-when-user-stops-scrolling – Brendan Bullen Oct 16 '13 at 16:24
  • @JoeIsaacson you mean do something on scroll-up and something else on scroll down? – Yuriy Galanter Oct 16 '13 at 16:24
  • @JoeIsaacson The syntax is incorrent. Atleast in javascript there is no such thing as "if/else function". `jQuery.prototype.scroll` is a function that bind on scroll event. http://api.jquery.com/scroll/ – Yury Tarabanko Oct 16 '13 at 16:25

1 Answers1

2

Taken from this website.

$(document).scroll(function(){
    $('.navbar').fadeOut(400);

    var scrollA = $('body').scrollTop();

    setTimeout(function(){
        if(scrollA == $('body').scrollTop()){
            $('.navbar').fadeIn(400);
        }
    }, 750);
});

When scrolling it will assign the scrollTop of the body to a variable and will create a timeout to check if the value of that variable is still the same after 750ms.

JSFiddle

Danny
  • 7,368
  • 8
  • 46
  • 70