0

Let's say I have a header that is 100px in height. When the user scrolls down, I want to change how the header looks and make it 50px in height.

Would it make more sense to style both header and display the main header by default and then use JavaScript to hide the main header and show the hidden (smaller header) when the user scrolls?

This seems like a really elaborate process which is why I'm not sure if there is an easier/more efficient way to do this.

2 Answers2

3

To make it work when somebody scrolls you could try this:

$(function(){
    $(window).scroll(function() { 
        if ($(this).scrollTop() > 100) { 
            $('header').toggleClass('small');
        } 
        else {     
            $('header').toggleClass('small');
        }  
    });
});
rjf0909
  • 183
  • 8
1

If your headers are compatible, i.e., they have some of the same elements, you should have a class added to the header when you want it to change. The benefit of this, is that CSS transitions can be performed, and all styling stays in your style sheets.

Demo

This could be a piece of your CSS.

header {
    background: black;
    height: 100px;
    transition: all 1s ease;
}

header.small {
    height: 50px;
}

When the small class is added, it transitions between the heigts.

Brigand
  • 84,529
  • 20
  • 165
  • 173