如何使用滚动更改导航栏背景?

时间:2016-09-10 07:47:58

标签: javascript jquery html css navigation

我是网络开发的新手。对于我的一个项目,我想在用户滚动时更改导航栏的背景。 我希望它看起来像这样: https://www.nlogic.co/understanding-vlan-hopping-attacks/

这是我的代码:



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>
&#13;
&#13;
&#13;

我不擅长jQuery。事实上,我只知道Javascript的基础知识。如果有人能在这里帮助我,我将非常感激。

2 个答案:

答案 0 :(得分:2)

试试这个:

$(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"});


    })

})

最终代码:

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

试试这个。根据需要更改sizes。请参考此示例,并在工作演示中的project.try下实现它。

<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>