2

I'm new to web-developing. For one of my projects, I want to change the navigation bar's background when the user scrolls. I want it to look like exactly this: https://www.nlogic.co/understanding-vlan-hopping-attacks/

Here's my code:

nav .row {
  margin-right: 0px;
}
nav {
  border: 2px ridge #999;
  position: fixed;
}
nav li {
  float: left;
  list-style: none;
  padding: 20px 30px;
}
nav a {
  color: white;
  font-size: 20px;
}
.btn button {
  background-color: transparent;
  color: white;
  margin: 10px 30px;
  padding: 10px 25px;
  border: 2px solid #999;
  border-radius: 5px;
}
.btn button:hover {
  border: 2px solid white;
  border-radius: 10px;
}
<nav class="col-md-12">
  <div class="row">
    <ul class="col-md-8">
      <li><a href="#">About</a>
      </li>
      <li><a href="#">Contact</a>
      </li>
      <div class="clearfix"></div>
    </ul>
    <div class="btn col-md-4">
      <button>Sign Up</button>
      <button>Sign In</button>
    </div>
  </div>
</nav>

I am not good at jQuery. In fact, I only know the basics of Javascript. I will be Very Very Grateful if someone can help me here.

chirag
  • 1,761
  • 1
  • 17
  • 33
Sk Golam Saroar
  • 376
  • 1
  • 5
  • 20

2 Answers2

2

Try this :

$(document).ready(function(){

    $(window).on("scroll",function(){

        if($(document).scrollTop() > 150)
            $(".col-md-12").css({backgroundColor:"gray",position:"fixed"});


        else
             $(".col-md-12").css({backgroundColor:"transparent",position:"absolute"});


    })

})

Final code :

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        body {
            height: 1500px;
        }
        nav .row{
    margin-right: 0px;
}

nav{
    border: 2px ridge #999;
    position:absolute;
}

nav li{
    float: left;
    list-style: none;
    padding: 20px 30px;
}

nav a{
    color:white;
    font-size: 20px;    
}

.btn button{
    background-color: transparent;
    color: white;
    margin: 10px 30px;
    padding: 10px 25px;
    border: 2px solid #999;
    border-radius: 5px;
}

.btn button:hover{
    border: 2px solid white;
    border-radius: 10px;
}
    </style>
</head>
    
    <body>
        
        <nav class="col-md-12">
    <div class="row">
        <ul class="col-md-8">
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
            <div class="clearfix"></div>
        </ul>   
        <div class="btn col-md-4">
            <button>Sign Up</button>
            <button>Sign In</button>
        </div>
    </div>
</nav>
        
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>
            
    $(document).ready(function(){

        $(window).on("scroll",function(){

            if($(document).scrollTop() > 150)
                $(".col-md-12").css({backgroundColor:"gray",position:"fixed"});


            else
                $(".col-md-12").css({backgroundColor:"transparent",position:"absolute"});


        })

    })
        
        </script>
    </body>
</html>
Ehsan
  • 12,655
  • 3
  • 25
  • 44
  • Thank you.. it works fine except one thing.. the "position:fixed" isn't working.. when I scroll, background color changes but it is not fixed to the top.. – Sk Golam Saroar Sep 10 '16 at 08:19
  • in nav style, change `position: fixed;` to `position:absolute;` – Ehsan Sep 10 '16 at 08:25
1

try this. change the sizes as your need. refer this example and implement it for your project.try below working demo.

<html>
<head></head>
<title></title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<style type="text/css">

body{
  height: 1000px;
  margin: 0;
  padding: 0;
}

div.navbar{
  width: 100%;
  height: 100px;
  background-color: black;
  position: fixed;
}

</style>

<body>

<div class="navbar"></div>
 
</body>

<script type="text/javascript">

$(window).scroll(function(){
  var thescroll = $('body').scrollTop();
  //alert(thescroll);
  //in here, get the scroll position, if it is greater than you navbar height then change the color or whatever.
  if (thescroll > 80) 
    {
      $("div.navbar").css({"background-color":"pink"});
    }
    else
    {
      $("div.navbar").css({"background-color":"black"});
    }
});

</script>

</html>
caldera.sac
  • 4,918
  • 7
  • 37
  • 69