0

I'm designing a nav that has the background bar appear after the user has scrolled down the page a bit. When they scroll back to the top, the bar (background color) disappears. I'm doing it using the instructions over at:

Add/remove class with jquery based on vertical scroll?

It works fine but now I would like to add fading in and out of the bar on scroll. I've tried adding the fadeIn() and fadeOut() methods. Problem is when it fades out, it fades out the whole #nav div! Not just the background colour. Here's the query

$(function() {
//caches a jQuery object containing the header element
var header = $('.noBackground');
$(window).scroll(function() {
    var scroll = $(window).scrollTop();

    if (scroll >= 500) {
        header.removeClass('noBackground').addClass('blackBackground').fadeIn();
    } else {
        header.removeClass('blackBackground').fadeOut().addClass('noBackground');
    }
});
});

Full, HTML, CSS and jQuery on this fiddle

4
  • 1
    If I understand your question, you want to animate the background of a <div>. Take a look at this - jqueryui.com/animate. Commented Mar 25, 2013 at 5:41
  • 1
    You can't really fade colors with jQuery unless you use jQuery UI or the color plugin, but why not use CSS3 transitions ? Commented Mar 25, 2013 at 5:44
  • I'm using addClass to load .blackBackground and removeClass to to load noBackground. There is noBackground by default. CSS transitions doesn't work because I want the class to switch, triggering the fade at a certain point. Commented Mar 25, 2013 at 5:53
  • is this what you're looking for? Commented Mar 25, 2013 at 5:58

2 Answers 2

1

The problem here is your #nav div is hidden when you scroll back to top. It is because the .fadeOut() method hides the matched elements by fading them to transparent. So you you remove .fadeOut() from else condition and it works fine.

Here is the edited code.

$(function() {
    //caches a jQuery object containing the header element
    var header = $('.noBackground');
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();

        if (scroll >= 500) {
            header.removeClass('noBackground').addClass('blackBackground').fadeIn();
        } else {
            header.removeClass('blackBackground').addClass('noBackground');
        }
    });
});

Edit:

A simple twist will show the effect:

$(function() {
    //caches a jQuery object containing the header element
    var header = $('.noBackground');
    $(window).scroll(function() {
        var scroll = $(window).scrollTop();

        if (scroll >= 500) {
            if(header.hasClass('noBackground')) {
                header.hide();
                header.removeClass('noBackground')
               .addClass('blackBackground').fadeIn(2000);
            }
        } else {
            if(header.hasClass('blackBackground'))  {   
                header.hide();
                header.removeClass('blackBackground')
               .addClass('noBackground').fadeIn(2000);
            }
        }
    });
});

Demo Fiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I did notice that but there are a couple draw backs. For some reason there is no actual fade. It just snaps into appearance with no fade. Also, is there no way to reverse that effect when you scroll back up to the top?
0

fadeOut() method can be implemented on a jquery element,not on a class..your code is working perfectly fine

Comments

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.