直接上代码,要求见:模仿京东顶部导航条 ,这次带下拉框的制作。
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>京东顶部导航条练习3</title>
<!-- 引入重置样式表 -->
<link rel="stylesheet" href="reset.css">
<!-- 引入图标字体 -->
<link rel="stylesheet" href="./css/all.css">
<style>
body{
/* 设置字体 */
font:12px/1.5 Microsoft YaHei,Heiti SC,tahoma,arial,Hiragino Sans GB,"\5B8B\4F53",sans-serif;
}
/* 解决高度塌陷的问题 */
.clearfix::before,
.clearfix::after{
content:"";
display:table;
clear:both;
}
/* 设置外部容器的样式 */
.top-bar-wrapper{
/* 设置宽度 */
width: 100%;
/* 设置高度 */
height: 30px;
/* 设置背景颜色 */
background-color:#e3e4e5 ;
/* 设置下边框 */
border-bottom:1px solid rgb(221,221,221);
/* 设置行高,使文字垂直居中 */
line-height: 30px;
}
/* 设置内部容器的样式 */
.top-bar{
/* 固定宽度 */
width: 1190px;
/* 设置水平居中 */
margin:0 auto;
position: relative;
}
/* 设置字体颜色 */
.top-bar a,
.top-bar span,
.top-bar i{
color:#999;
text-decoration:none;
}
/* 设置链接的颜色 */
.top-bar a:hover,
.top-bar .highlight{
color:#f10215;
}
/* 设置location */
.location{
float: left;
}
/* 设置城市列表 */
.location .city-list{
display: none;
width: 320px;
height: 436px;
background-color: white;
border:1px solid rgb(204,204,204);
/* 设置绝对定位,使其不占用页面的位置 */
position: absolute;
box-shadow:0 2px 2px rgba(0,0,0,.2);
z-index:999;
top:31px;
}
/* 错误示例:绑定元素不对。当鼠标放到current-city的时候,让它的兄弟元素city-list显示出来 */
/* .current-city:hover + .city-list{
display: block;
} */
/* 正确的示例,hover一定要给父元素 */
.location:hover .city-list{
display: block;
}
/* 给current-city设置边距 */
.current-city{
padding:0 10px;
border:1px solid transparent;
border-bottom: none;
position: relative;
z-index: 9999;
}
/* 设置current-city鼠标移入的效果 */
.location:hover .current-city{
background-color: white;
border:1px solid rgb(204,204,204);
border-bottom: none;
padding-bottom: 1px;
}
/* 设置location图标字体 */
.location .fas{
color:#f10215;
}
/* 设置shortcut */
.shortcut{
float: right;
}
/* 设置li */
.shortcut li{
float: left;
}
/* 设置分割线 */
.shortcut .line{
width: 1px;
height: 10px;
background-color: #ccc;
margin:11px 15px 0;
}
</style>
</head>
<body>
<!-- 创建外部容器 -->
<div class="top-bar-wrapper">
<!-- 创建内部容器 -->
<div class="top-bar clearfix">
<!-- 左侧菜单 -->
<div class="location">
<div class="current-city">
<i class="fas fa-map-marker-alt"></i>
<a href="javascript:;">北京</a>
</div>
<!-- 设置下拉框-->
<div class="city-list">
</div>
</div>
<!-- 设置右侧菜单 -->
<ul class="shortcut clearfix">
<li>
<a href="javascript:;">你好,请登录</a>
<a class="highlight" href="javascript:;">免费注册</a>
</li>
<!-- 分割线设置 -->
<li class="line"></li>
<li><a href="#">我的订单</a></li>
<li class="line"></li>
<li>
<a href="#">我的京东</a>
<i class="fas fa-angle-down"></i>
</li>
<li class="line"></li>
<li><a href="#">京东会员</a></li>
<li class="line"></li>
<li>
<a class="highlight" href="#">企业采购</a>
<i class="fas fa-angle-down"></i>
</li>
<li class="line"></li>
<li>
<span>客户服务</span>
<i class="fas fa-angle-down"></i>
</li>
<li class="line"></li>
<li>
<span>网站导航</span>
<i class="fas fa-angle-down"></i>
</li>
<li class="line"></li>
<li>
<span>手机京东</span>
</li>
</ul>
</div>
</div>
</body>
</html>