Bootstrap實現(xiàn)漸變頂部固定自適應導航欄
本文實例為大家分享了Bootstrap實現(xiàn)漸變頂部固定自適應導航欄的具體代碼,供大家參考,具體內(nèi)容如下
具體代碼如下所示:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <meta charset="utf-8" /> <link href="<%=path%>/css/style.css" rel="stylesheet" type="text/css" /> <link rel="stylesheet" > <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <nav class="navbar navbar-fixed-top my-navbar" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#example-navbar-collapse"> <span class="sr-only">切換導航</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">菜鳥教程</a> </div> <div class="collapse navbar-collapse" id="example-navbar-collapse"> <ul class="nav navbar-nav"> <li class="active"><a href="#">iOS</a></li> <li><a href="#">SVN</a></li> <li><a href="#">Asp.Net</a></li> </ul> </div> </div> </nav> <div class="bg"></div> <script> $(window).scroll(function () { if ($(".navbar").offset().top > 50) {$(".navbar-fixed-top").addClass("top-nav"); }else {$(".navbar-fixed-top").removeClass("top-nav");} })</script> </body></html>
style.css
html, body {width:100%;height:100%;} /*非常重要的樣式讓背景圖片100%適應整個屏幕*/ .bg {display: table;width: 100%;height: 100%;padding: 100px 0;text-align: center;color: #fff;background: url(http://www.xiandanke.cn/Image/intro-bg.jpg) no-repeat bottom center;background-color: #000;background-size: cover;} .my-navbar {padding:20px 0;transition: background 0.5s ease-in-out, padding 0.5s ease-in-out;} .my-navbar a{background:transparent !important;color:#fff !important} .my-navbar a:hover {color:#45bcf9 !important;background:transparent;outline:0} .my-navbar a {transition: color 0.5s ease-in-out;}/*-webkit-transition ;-moz-transition*/ .top-nav {padding:0;background:#000;} button.navbar-toggle {background-color:#fbfbfb;}/*整個背景都是transparent透明的,會看不到,所以再次覆蓋一下*/ button.navbar-toggle > span.icon-bar {background-color:#dedede}
真的只用了9行代碼,原理挺簡單的,但是要注意以下幾點
1.html,body{width:100%;height:100%}
,必須寫這個樣式,才能讓html中的子元素100%占滿整個屏幕,也就是要實現(xiàn)背景圖片占滿100%的整個屏幕
2.bootstrap中的類 nav-fixed-top
的意義在于固定導航欄在頂部
3.添加scroll 事件,在切換class的時候?qū)崿F(xiàn)動態(tài)的效果
4.整個效果的實現(xiàn)原理是使用了transition 屬性,transition屬性的使用方法是:
以上所述是小編給大家介紹的Bootstrap實現(xiàn)漸變頂部固定自適應導航欄,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
利用Print.js實現(xiàn)打印pdf、HTML及圖片(可設置樣式可分頁)
在我們需要在頁面中打印某個區(qū)域的內(nèi)容或者生成pdf的時候,我們可以直接用printJs庫,這篇文章主要給大家介紹了關于利用Print.js實現(xiàn)打印pdf、HTML及圖片的相關資料,可設置樣式可分頁,需要的朋友可以參考下2024-05-05使用mini-define實現(xiàn)前端代碼的模塊化管理
這篇文章主要介紹了使用mini-define實現(xiàn)前端代碼的模塊化管理,十分不錯的一篇文章,這里推薦給有需要的小伙伴。2014-12-12