1. 程式人生 > >Js控制網頁滑動的時候頂部導航條變成半透明例項

Js控制網頁滑動的時候頂部導航條變成半透明例項

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <!--引入jQuery-->  
  <script src="http://apps.bdimg.com/libs/jquery/1.8.3/jquery.js"></script>  
  <title>Js控制網頁滑動的時候頂部導航條變成半透明</title>
  <style type="text/css">
	.main{
		width:100%;
		height:2000px;
		background:gray;
	}
	.nav{
		width:100%;
		height:100px;
		background:black;
		position:fixed;
		top:0;
	}
	.content{
		position: f;
		top: 0;
		right: 0;
		left: 0;
		bottom: 2.5rem;
	}
  </style>
 </head>
 <body>
  <div class="nav"></div>
  <div class="main"></div>
  <script>
	    function _scroll(){
            var scrollTop =$(window).scrollTop();
            if(scrollTop<10){
                $('.nav').css('opacity',1);
            }else{
                $('.nav').css('opacity',0.1);
            }
        }
        $(window).on('scroll',function() {
            _scroll()
        });
  </script>
 </body>
</html>