模拟新浪下拉菜单 (前端必学)
作者:互联网
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>新浪下拉菜单</title>
<style>
ul {
margin: 0;
padding: 0;
list-style: none;
}
a {
display: block;
width: 100%;
height: 100%;
color: #333;
font-size: 12px;
box-sizing: border-box;
padding-left: 15px;
text-decoration: none;
}
.nav {
width: 80px;
height: 40px;
margin: 0 auto;
margin-top: 30px;
}
.nav>li {
position: relative;
width: 80px;
height: 40px;
line-height: 40px;
}
/* 这里添加倒三角过度效果 */
.nav>li:hover::after {
transform: rotate(225deg);
transition: all .5s linear;
}
/* 这里也可以直接选择背景图插入 我这里是做了一个三角*/
.nav>li::after {
position: absolute;
top: 16px;
right: 25px;
content: '';
width: 4px;
height: 4px;
border-right: 2px solid #E77800;
border-bottom: 2px solid #E77800;
transform: rotate(45deg);
}
.sub {
/* 将sub这个ul定位在nav下面 */
position: absolute;
left: 0;
top: 40px;
/* 这里注意要提前把sub这个ul隐藏 */
display: none;
}
.sub li {
/* 添加边框 */
width: 80px;
height: 32px;
line-height: 32px;
box-sizing: border-box;
border-left: 1px solid #FECC5B;
border-right: 1px solid #FECC5B;
border-bottom: 1px solid #FECC5B;
}
.nav>li a:hover {
/* 鼠标经过改变背景颜色和字体颜色 */
color: #ff8400;
}
.sub li a:hover {
background-color: #FFF5DA;
}
</style>
</head>
<body>
<ul class="nav">
<li>
<a href="#">微博</a>
<!-- 这里需要将sub这个ul放在外层li里面 确保在同一盒子里 -->
<ul class="sub">
<li><a href="#">私信</a> </li>
<li><a href="#">评论</a> </li>
<li><a href="#">@我</a> </li>
</ul>
</li>
</ul>
<script>
//获取元素
var nav = document.querySelector('.nav');
var sub = document.querySelector('.sub');
//onmouseover为鼠标经过事件
nav.onmouseover = function() {
// 鼠标经过显示
sub.style.display = 'block';
nav.style.backgroundColor = ' #eee';
}
// onm ouseout为鼠标离开事件。
nav.onmouseout = function() {
// 鼠标移开隐藏
sub.style.display = 'none';
nav.style.backgroundColor = ' #fff';
}
</script>
</body>
</html>
标签:style,sub,前端,必学,height,nav,li,border,下拉菜单 来源: https://blog.csdn.net/LiDeKang1/article/details/120113342