treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Add class only when browser scrolling

  • The title pretty much describe what my problem is, I need to add a class to a fixed nav but only when the browser is scrolling if the user is not scrolling remove that class.

    Thanks in advance....
    Alex
  • I am...intrigued. Are there any examples out there that you can link?
  • Not that I know, just a problem that I have and that will solve it
  • I'm not sure why you'd do this, but check out this demo and code:
    $(function() {

    var timer, el = $('body'),
    flag = false;
    $(window).scroll(function() {
    if (!flag) {
    flag = true;
    el.addClass('scrolling');
    }
    clearTimeout(timer);
    timer = setTimeout(function() {
    el.removeClass('scrolling');
    flag = false;
    }, 200);
    });

    });​
  • Just what I need! but is not working on my page