Allen Lantz Allen Lantz - 5 months ago 11
CSS Question

How do i change the header of my website when you scroll down?

I want so that when a user scrolls down the header changes its background and the header stays at the top of the window. I don't know if this needs javascript or you can do it in css.

The example of what i want it too look like is at https://anvilnode.com/

Answer

you can use this kind of code

html:
<nav id="header_nav">nav here</nav>

css:

body {
    height: 1000px;
    width: 100%;
    background-color: #F0F0F0;
}

#header_nav {
    width: 100%;
    height: 100px;
    background-color: #666;
    position: fixed;
    top: 0;
    left: 0;
}

js

$(function(){
    $('#header_nav').data('size','big');
});

$(window).scroll(function(){
    if($(document).scrollTop() > 0) {
        if($('#header_nav').data('size') == 'big') {
            $('#header_nav').data('size','small');
            $('#header_nav').stop().animate({
                height:'40px'
            },600);
        }
    }
    else {
    if($('#header_nav').data('size') == 'small') {
        $('#header_nav').data('size','big');
        $('#header_nav').stop().animate({
            height:'100px'
        },600);
    }  
}});
Comments