頁面導(dǎo)航欄滾動時改變導(dǎo)航欄背景樣式
html:
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<meta charset="utf-8" />
<link rel="stylesheet" >
<link rel="stylesheet" type="text/css" href="css/style2.css">
<style>
.hero{
padding-top: 409px;
}
</style>
</head>
<body>
<header>
<nav id="navbar" class="navbar navbar-custom navbar-fixed-top" data-spy="affix" data-offset-top="70">
<div class="container">
<div class="row">
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<i class="fa fa-bars"></i>
</button>
<a class="navbar-brand page-scroll logo-light" href="index.html"><img alt="" src="image/logo.png"></a>
<a class="navbar-brand page-scroll logo-clr" href="index.html"><img alt="" src="image/logo-clr.png"></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<div class="right-nav text-right">
<ul class="nav navbar-nav menu">
<li>
<a href="#home">Home</a>
</li>
<li>
<a href="#feature">Feature</a>
</li>
<li>
<a href="#overview">Overview</a>
</li>
<li>
<a href="#pricing">Pricing</a>
</li>
<li>
<a href="#team">Team</a>
</li>
<li>
<a href="#blog">blog</a>
</li>
</ul>
<div class="nav-btn">
<a class="btn grdnt-purple">Download</a>
</div>
</div>
</div>
</div>
</div>
</nav>
<div id="home" class="hero grdnt-purple">
</div>
</header>
<section style="height: 500px">
aaaa
</section>
<section style="height: 500px">
bbb
</section>
<script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script src="js/vendor.bundle.js"></script>
</body>
</html>
詳細(xì)的css與js請查看我的GitHub:
https://github.com/AMONTOP/navbar
返回頂部:
1、在壓面 body 中的任何地方加入一下代碼
<div id="scroll">
<div id="toTop" class="scrollItem">
回到頂部
</div>
<div id="toBottom" class="scrollItem">
回到底部
</div>
</div>
在head中加入css
#scroll {position:fixed; top:300px; right:100px;z-index:999}
.scrollItem {width:20px; height:70px;border:#e1e1e1 1px solid; cursor: pointer; text-align: center; padding-top: 10px;}
當(dāng)然你也可以定義自己的外觀,可以放上一張圖片來替代,關(guān)鍵的是保持id與js代碼中一致,而且要將滾動導(dǎo)航層的position設(shè)置為fixed,這樣當(dāng)我們拖動滾動條的時候,滾動導(dǎo)航層才不會動。
2、引入jqeury庫文件
<script type="text/javascript">
$( function () {
var speed = 1000;//自定義滾動速度
//回到頂部
$("#scroll").hide();
$(window).scroll(function(event){
if( $(window).scrollTop() === 0 ){
$("#scroll").hide();
}else{
$("#scroll").show();
}
});
$( "#toTop").click( function () {
$( "html,body").animate({ "scrollTop" : 0 }, speed);
});
});
</script>
在以上的JS代碼中,我們可以自定義滾動速度,speed值越大,滾動越快
var speed = 1000;
那么你的頁面便實現(xiàn)了回到頂部和底部的功能。
在此我還要多添加一個功能,那就是滑動到指定ID的元素,語法如下。需要把scrollTop設(shè)置為
$(‘#ID’).offset().top