0

This js add class only while Scroll Up the browser page. But problem is after Scroll Up browser to top of the page .darkHeader class are not remove, means i want to remove this class after page Scroll Up to top.

JS

lastScroll = 0;
$(window).on('scroll',function() {    
    var scroll = $(window).scrollTop();
    if(lastScroll - scroll > 0) {
        $(".nav").addClass("darkHeader");
    } else {
        $(".nav").removeClass("darkHeader");
    }
    lastScroll = scroll;
});

JSfiddle >>

How to remove .darkHeader class after page has completely scroll Up on top of page/head? How to prevent adding class without browser scrolling?

1 Answer 1

3

You could add another condition which checks if the scrolling is at the top of the page, and removes the class like this:

    if(scroll === 0){
        $(".nav").removeClass("darkHeader");
    } else if(lastScroll - scroll > 0) {
        $(".nav").addClass("darkHeader");
    } else {
        $(".nav").removeClass("darkHeader");
    }
Sign up to request clarification or add additional context in comments.

2 Comments

The dark header is still showing when you scroll back up to the top of the page? What browser are you using? And are you sure you implemented it correctly (here's a forked fiddle: jsfiddle.net/9fbr320y/1 )?
woo works, thanks, now i have try it on my site :) on more is it works in IE9? thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.