導航欄實現
一、導航欄展示
二、導航欄實現思路
導航欄在不同的屏幕大小下呈現不同的樣式,移動端下使用手風琴菜單(有現成的代碼),在sm屏幕下,除了微金所圖標和個人中心外,其餘都不可見。
三、相關代碼
- HTML代碼
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3個meta標籤*必須*放在最前面,任何其他內容都*必須*跟隨其後! -->
<title>導航欄</title>
<link href="./lib/css/bootstrap.css" rel="stylesheet">
<link rel="stylesheet" href="./css/webFont.css">
<link rel="stylesheet" href="./css/index.css">
<!-- HTML5 shim 和 Respond.js 是爲了讓 IE8 支持 HTML5 元素和媒體查詢(media queries)功能 -->
<!-- 警告:通過 file:// 協議(就是直接將 html 頁面拖拽到瀏覽器中)訪問頁面時 Respond.js 不起作用 -->
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dest/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="wjs_nav">
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<!-- 手風琴組件 -->
<button type="button" class="navbar-toggle collapsed wjs_toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand wjs_icon" href="#">
<span class="wjs_font wjs_logo"></span>
<span class="wjs_font wjs_word"></span>
</a>
</div>
<div class="collapse navbar-collapse " id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav wjs_navbar hidden-sm">
<li class="active"><a href="#">我要投資<span class="sr-only">(current)</span></a></li>
<li><a href="#">投資介紹</a></li>
<li><a href="#">投資看點</a></li>
<li><a href="#">我要融資</a></li>
<li><a href="#">內容介紹</a></li>
<li><a href="#">用心推薦</a></li>
</li>
</ul>
<ul class="nav navbar-nav navbar-right wjs_navbar">
<li><a href="#">個人中心</a></li>
</ul>
</div>
</div>
</nav>
</div>
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依賴 jQuery,所以必須放在前邊) -->
<script src="./lib/js/jquery.min.js"></script>
<!-- 加載 Bootstrap 的所有 JavaScript 插件。你也可以根據需要只加載單個插件。 -->
<script src="./lib/js/bootstrap.min.js"></script>
</body>
</html>
- less代碼
//導航
.wjs_nav{
.wjs_icon{
height:80px;
line-height: 50px;
text-align: center;
> .wjs_font{
font-size: 40px;
}
>span:first-of-type{
color:red;
}
>span:last-of-type{
color: #0f0f0f;
}
}
.wjs_navbar{
>li{
>a{
line-height: 50px;
font-size: 16px;
&:active,&:focus{
color: #777;
background-color: transparent;
border-bottom: 2px solid red;
box-sizing: border-box;
}
}
}
.active{
>a, a:hover,a:focus{
background-color: transparent;
border-bottom: 2px solid red;
}
}
}
.wjs_toggle{
margin-top: 12px;
}
}
四、相關知識總結
- 加邊框後要想不影響盒子的寬高,要添加 box-sizing: border-box;
- 導航欄的高度不應該固定,應該讓它的內容撐開其高度