导航栏的下拉菜单
利用html +css+js实现导航条下拉动作
效果图:
鼠标移到菜单栏上,显示下拉的二级菜单栏,鼠标移出则二级菜单栏隐藏。
实现代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
body{
margin: 0;
padding: 0;
box-sizing: border-box;
}
a{
text-decoration: none;
color: #bbb;
}
.nav{
height: 50px;
line-height: 50px;
background-color: black;
}
.nav > ul > li{
margin: 0 20px;
float: left;
list-style: none;
}
.nav > ul > li > a:hover{
color: white;
}
.nav > ul > li > ul{
position: absolute;
top: 50px;
width: 150px;
border: 1px solid black;
display: none;
.nav ul> li ul > li {
list-style: none;
float: left;
margin: 0 10px;
}
.nav ul> li ul > li > a{
color:black;
}
</style>
<body>
<div class="nav">
<ul>
<li>
<a href="">天猫</a>
<ul>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
</ul>
</li>
<li><a href="">聚划算</a></li>
<li><a href="">天猫超市</a></li>
<li><a href="">淘抢购</a></li>
<li>
<a href="">电器城</a>
<ul>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
</ul>
</li>
</ul>
<script>
const navs = document.querySelectorAll('.nav > ul > li');
navs.forEach(function (nav) {
// 鼠标移入时: 显示子菜单
nav.addEventListener("mouseover", showSubMenu);
// 鼠标移出时: 关掉子菜单
nav.addEventListener("mouseout", closeSubMenu);
});
// 显示子菜单
function showSubMenu(ev) {
console.log(ev.target);
// 当前这个导航有没有子菜单?
if (ev.target.nextElementSibling !== null) {
ev.target.nextElementSibling.style.display = "block";
}
}
// 关掉子菜单
function closeSubMenu(ev) {
if (ev.target.nodeName === "A" && ev.target.nextElementSibling !== null) {
ev.target.nextElementSibling.style.display = "none";
}
}
</script>
</body>
</html>
代码解析:
html页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="nav">
<ul>
<li>
<a href="">天猫</a>
<ul>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
</ul>
</li>
<li><a href="">聚划算</a></li>
<li><a href="">天猫超市</a></li>
<li><a href="">淘抢购</a></li>
<li>
<a href="">电器城</a>
<ul>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
<li><a href="">天猫</li>
</ul>
</li>
</ul>
</div>
</body>
</html>
效果图:
利用一个大<div>标签,下面用ul和li标签实现的
没有css样式的html 效果朴实无华,仿佛回到了3g时代。
html+css页面:
<style>
body{
margin: 0;
padding: 0;
box-sizing: border-box;
}
a{
text-decoration: none;
color: #bbb;
}
.nav{
height: 50px;
line-height: 50px;
background-color: black;
}
.nav > ul > li{
margin: 0 20px;
float: left;
list-style: none;
}
.nav > ul > li > a:hover{
color: white;
}
.nav > ul > li > ul{
position: absolute;
top: 50px;
width: 150px;
border: 1px solid black;
display: none;
}
.nav ul> li ul > li {
list-style: none;
float: left;
margin: 0 10px;
}
.nav ul> li ul > li > a{
color:black;
}
</style>
利用css内部样式 给div 和ul li 标签进行添加样式。效果立马高大上!
效果图:
js代码:
<script>
const navs = document.querySelectorAll('.nav > ul > li');
navs.forEach(function (nav) {
// 鼠标移入时: 显示子菜单
nav.addEventListener("mouseover", showSubMenu);
// 鼠标移出时: 关掉子菜单
nav.addEventListener("mouseout", closeSubMenu);
});
// 显示子菜单
function showSubMenu(ev) {
console.log(ev.target);
// 当前这个导航有没有子菜单?
if (ev.target.nextElementSibling !== null) {
ev.target.nextElementSibling.style.display = "block";
}
}
// 关掉子菜单
function closeSubMenu(ev) {
if (ev.target.nodeName === "A" && ev.target.nextElementSibling !== null) {
ev.target.nextElementSibling.style.display = "none";
}
}
</script>
1.利用document.querySelectorAll()方法 获取所有的标签放在常量navs里。