Bootstrap實(shí)現(xiàn)漸變頂部固定自適應(yīng)導(dǎo)航欄
本文實(shí)例為大家分享了Bootstrap實(shí)現(xiàn)漸變頂部固定自適應(yīng)導(dǎo)航欄的具體代碼,供大家參考,具體內(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">切換導(dǎo)航</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%適應(yīng)整個(gè)屏幕*/ .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;}/*整個(gè)背景都是transparent透明的,會(huì)看不到,所以再次覆蓋一下*/ button.navbar-toggle > span.icon-bar {background-color:#dedede}
真的只用了9行代碼,原理挺簡(jiǎn)單的,但是要注意以下幾點(diǎn)
1.html,body{width:100%;height:100%}
,必須寫這個(gè)樣式,才能讓html中的子元素100%占滿整個(gè)屏幕,也就是要實(shí)現(xiàn)背景圖片占滿100%的整個(gè)屏幕
2.bootstrap中的類 nav-fixed-top
的意義在于固定導(dǎo)航欄在頂部
3.添加scroll 事件,在切換class的時(shí)候?qū)崿F(xiàn)動(dòng)態(tài)的效果
4.整個(gè)效果的實(shí)現(xiàn)原理是使用了transition 屬性,transition屬性的使用方法是:
以上所述是小編給大家介紹的Bootstrap實(shí)現(xiàn)漸變頂部固定自適應(yīng)導(dǎo)航欄,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 詳解bootstrap導(dǎo)航欄.nav與.navbar區(qū)別
- BootStrap學(xué)習(xí)筆記之nav導(dǎo)航欄和面包屑導(dǎo)航
- BootStrap實(shí)現(xiàn)響應(yīng)式布局導(dǎo)航欄折疊隱藏效果(在小屏幕、手機(jī)屏幕瀏覽時(shí)自動(dòng)折疊隱藏)
- 解決bootstrap導(dǎo)航欄navbar在IE8上存在缺陷的方法
- Bootstrap編寫導(dǎo)航欄和登陸框
- Bootstrap3制作自己的導(dǎo)航欄
- Bootstrap導(dǎo)航欄各元素操作方法(表單、按鈕、文本)
- Bootstrap實(shí)現(xiàn)響應(yīng)式導(dǎo)航欄效果
- bootstrap自定義樣式之bootstrap實(shí)現(xiàn)側(cè)邊導(dǎo)航欄功能
相關(guān)文章
webpack幾種手動(dòng)實(shí)現(xiàn)HMR的方式
眾所周知,在webpack中使用模塊熱替換(HMR),能夠使得應(yīng)用在運(yùn)行時(shí),本文就介紹一下如何實(shí)現(xiàn)HMR,感興趣的可以了解一下2021-07-07防止瀏覽器記住用戶名及密碼的簡(jiǎn)單實(shí)用方法
很多瀏覽器都有自動(dòng)填寫功能,我在input上使用了autocomplete="off",但在有的瀏覽器上還是被記住了用戶名跟密碼,請(qǐng)問有沒有更有效及簡(jiǎn)便的方法來防止瀏覽器記住用戶名及密碼2013-04-04IE DOM實(shí)現(xiàn)存在的部分問題及解決方法
IE DOM實(shí)現(xiàn)存在的部分問題及解決方法2009-07-07利用Print.js實(shí)現(xiàn)打印pdf、HTML及圖片(可設(shè)置樣式可分頁(yè))
在我們需要在頁(yè)面中打印某個(gè)區(qū)域的內(nèi)容或者生成pdf的時(shí)候,我們可以直接用printJs庫(kù),這篇文章主要給大家介紹了關(guān)于利用Print.js實(shí)現(xiàn)打印pdf、HTML及圖片的相關(guān)資料,可設(shè)置樣式可分頁(yè),需要的朋友可以參考下2024-05-05使用mini-define實(shí)現(xiàn)前端代碼的模塊化管理
這篇文章主要介紹了使用mini-define實(shí)現(xiàn)前端代碼的模塊化管理,十分不錯(cuò)的一篇文章,這里推薦給有需要的小伙伴。2014-12-12js定時(shí)器(執(zhí)行一次、重復(fù)執(zhí)行)
這篇文章主要分享一段js代碼,有關(guān)js定時(shí)器的小例子,分為執(zhí)行一次的定時(shí)器與重復(fù)執(zhí)行的定時(shí)器,需要的朋友可以參考下2014-03-03