Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change header background colour when page scrolls

I've been looking at a solution for this but I cannot get it to work.

I would like my page header to change from a transparent background to a white background as the user begins scrolling the page.

HTML code is:

<div class="header">
    <div class="topbar"></div>
    <div class="sitelogo"></div>
    <nav id="navigation">
        <ul>
            <li id="twitter"><a href="http://www.twitter.com/iamdanmorris"><em>Twitter</em></a></li>
            <li><a href="#contact">Contact</a></li>
            <li><a href="#blog">Blog</a></li>
            <li><a href="#">Portfolio</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Home</a></li>
        </ul>
    </nav>
    <div style="clear:both;"></div>
</div>

CSS code is:

.header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    padding: 0;
    z-index: 10000;
    -webkit-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
    -moz-box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
    box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
    transition: all 0.2s ease-in-out;
    height: auto;
    background-color:transparent;   
}
like image 929
iamdanmorris Avatar asked Feb 01 '15 19:02

iamdanmorris


People also ask

How do I change the background color of my header in CSS?

You can use all the custom CSS codes at the same time to change all three colors. Simply change the color hex code of your desired color in the codes and you can copy and paste all the given codes one after another in your Dashboard > Appearance > Customize > Theme Options > Advanced Options > custom CSS field.


2 Answers

$(window).on("scroll", function() {
    if($(window).scrollTop() > 50) {
        $(".header").addClass("active");
    } else {
        //remove the background property so it comes transparent again (defined in your css)
       $(".header").removeClass("active");
    }
});

fiddle: http://jsfiddle.net/634d6vgq/2/

This will add the background-color: #fff to the element if user has scrolled more than 50 pixels from the top

This will add a class "active" so you can style it within the css (easier to maintain)

edit:

$(function() {
    $(window).on("scroll", function() {
        if($(window).scrollTop() > 50) {
            $(".header").addClass("active");
        } else {
            //remove the background property so it comes transparent again (defined in your css)
           $(".header").removeClass("active");
        }
    });
});

And your css:

.active { background-color: #fff}

Make sure you also add this css rule, orherwise the background color will not change

like image 89
fdsugfh Avatar answered Oct 22 '22 15:10

fdsugfh


Here is my straightforward solution for this topic. You just need to add an ID to the element to change color. In my case, elementor gives the id as "header_frame" in Advance Tab.

Put the below code into the footer:

jQuery(window).on('scroll', function() {
    if(jQuery(window).scrollTop() > 300) {
        jQuery('#header_frame').css('background-color', '#FFFFFF');
    } else {
       jQuery('#header_frame').css('background-color', '#EFF2F4');
    }
});

No need to add some class like 'active' etc.

like image 37
Ashish Tiwari Avatar answered Oct 22 '22 15:10

Ashish Tiwari