Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make navigation bar change color when you scroll down the page?

Tags:

jquery

nav

I would like the navigation bar to be transparent but when you scroll down the page it changes to color red for example.

<div class="nav">
      <div class="container">
       <div class="logo">
       <a href="#"><img src="RepublicSquare_logo.svg" style="height: 80px;"></a>
       </div>
        <div class="navMain">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Services</a></li>
                <li><a href="#">Portfolio</a></li>
                <li><a href="#">Contact</a></li>
            </ul>
        </div>
      </div>
    </div>
like image 849
user3218008 Avatar asked Dec 05 '14 03:12

user3218008


1 Answers

Something like this:

http://jsfiddle.net/qrhjdfmd/

var a = $(".nav").offset().top;

$(document).scroll(function(){
    if($(this).scrollTop() > a)
    {   
       $('.nav').css({"background":"red"});
    } else {
       $('.nav').css({"background":"transparent"});
    }
});
like image 104
KaMZaTa Avatar answered Sep 26 '22 21:09

KaMZaTa