Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove class when browser scrolls down but add class when it scrolls up

Tags:

jquery

scroll

I'm creating a 1 page website, I want to remove a class when the user scrolls down and add the class when the user scrolls up.

I've got this working to a point but it's not correct, at the moment when the user scrolls down 50px from the top then the class is removed and it's added when the user scrolls up 50px from the top.

I want it so that they can be almost at the bottom of the page and if the scroll up the class is there and if they scroll down then it's removed

Here's the query I have so far

jQuery(document).ready(function($) {
    $(".test").addClass("mobile");
    $(window).scroll(function(){
        var scroll = $(window).scrollTop();
        if (scroll >= 50) {
        $(".test").removeClass("mobile");
        } else if (scroll <= 50) {
        $(".test").addClass("mobile");
        }
    });
});
like image 231
Dan Avatar asked Oct 16 '14 12:10

Dan


People also ask

How do you add a class when scrolling?

addClass('newClass'); } else { $('#dynamic'). removeClass('newClass'); } }); This allows you to add and/or remove classes after the user has scrolled down a certain amount in a page. In this case, fill background with orange after scrolling 50px down.

How do I scroll up in Javascript?

Method 1: Using window.scrollTo() The scrollTo() method of the window Interface can be used to scroll to a specified location on the page. It accepts 2 parameters the x and y coordinate of the page to scroll to. Passing both the parameters as 0 will scroll the page to the topmost and leftmost point.


1 Answers

So this declares a global variable lastScroll to track the last known scroll position. After the user scrolls, it will compare the scroll location to the last known scroll location and add or remove your class depending on that. I also removed the <= and >= because I didn't want it to do anything if the scroll position doesn't change (somehow).

var lastScroll = 0;

jQuery(document).ready(function($) {
    $(".test").addClass("mobile");

    $(window).scroll(function(){
        var scroll = $(window).scrollTop();
        if (scroll > lastScroll) {
            $(".test").removeClass("mobile");
        } else if (scroll < lastScroll) {
            $(".test").addClass("mobile");
        }
        lastScroll = scroll;
    });
});

Per the comments below, add this:

var lastScroll = 0;

jQuery(document).ready(function($) {
    $(".test").addClass("mobile");

    $(window).scroll(function(){
        setTimeout(function() { //give them a second to finish scrolling before doing a check
            var scroll = $(window).scrollTop();
            if (scroll > lastScroll + 30) {
                $(".test").removeClass("mobile");
            } else if (scroll < lastScroll - 30) {
                $(".test").addClass("mobile");
            }
            lastScroll = scroll;
        }, 1000);
    });
});
like image 122
C Bauer Avatar answered Sep 25 '22 15:09

C Bauer