我正在尝试创建一个像wistia.com这样的下拉菜单。但我不能得到这种悬停效果。我把我的代码放在下面的一个片段中。当我在主菜单链接上悬停时,下拉列表应该会出现。当我悬停在下拉列表之外时,它就会消失。现在它只在我悬停在菜单上时出现,但离开菜单时不会隐藏。
null
let dropdown = document.querySelector(".dropdown");
let chevron = document.querySelector(".main-link i");
function dropdownDisplay() {
dropdown.style.display = "flex";
}
function dropdownHide() {
dropdown.style.display = "none";
}
function chevronTransition() {
chevron.style.trasform = "rotateX(0deg)";
}
:root {
--font1: roboto;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
nav {
width: 100%;
height: 70px;
border-bottom: 1px solid #ddd;
font-family: var(--font1);
}
nav .container {
width: 1340px;
height: 100%;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
position: relative;
}
nav .container .links {
display: flex;
align-items: center;
}
nav .container .links .menu {
margin-left: 32px;
}
nav .container .links .menu ul .main-link {
list-style: none;
display: inline-block;
margin-right: 24px;
cursor: pointer;
}
nav .container .links .menu ul .main-link:hover i {
transform: rotateX(180deg);
}
nav .container .links .menu ul .main-link .dropdown {
width: 1340px;
position: absolute;
top: 100%;
left: 0;
padding: 32px 0;
cursor: initial;
display: none;
border-bottom: 1px solid #ddd;
}
nav .container .links .menu ul .main-link .dropdown .cta {
width: 300px;
padding-right: 16px;
}
nav .container .links .menu ul .main-link .dropdown .cta h4 {
color: #3ea9f5;
font-size: 30px;
}
nav .container .links .menu ul .main-link .dropdown .cta p {
color: #505050;
margin-bottom: 32px;
margin-top: 24px;
}
nav .container .links .menu ul .main-link .dropdown .cta a {
color: #fff;
background-color: #3ea9f5;
padding: 12px 32px;
border-radius: 6px;
text-decoration: none;
font-weight: bold;
display: inline-block;
}
nav .container .links .menu ul .main-link .dropdown .cta a:focus {
outline: none;
}
nav .container .links .menu ul .main-link .dropdown .major {
width: 150px;
}
nav .container .links .menu ul .main-link .dropdown .major ul li {
display: block;
margin-bottom: 8px;
}
nav .container .links .menu ul .main-link .dropdown .major ul li a {
color: #505050;
font-weight: 700;
text-decoration: none;
}
nav .container .links .menu ul .main-link .dropdown .sub-menu-links {
display: flex;
}
nav .container .links .menu ul .main-link .dropdown .sub-menu-links .sub-menu {
margin-right: 20px;
width: 150px;
}
nav
.container
.links
.menu
ul
.main-link
.dropdown
.sub-menu-links
.sub-menu
h6 {
font-weight: bold;
color: #a7afba;
font-size: 14px;
margin-bottom: 12px;
}
nav
.container
.links
.menu
ul
.main-link
.dropdown
.sub-menu-links
.sub-menu
ul
li {
display: block;
list-style: none;
margin-bottom: 8px;
}
nav
.container
.links
.menu
ul
.main-link
.dropdown
.sub-menu-links
.sub-menu
ul
li
a {
font-size: 15px;
text-decoration: none;
color: #505050;
}
nav .container .links .menu ul .main-link i {
font-size: 12px;
}
nav .container .links .logo h3 {
font-size: 36px;
}
<nav>
<div class="container">
<div class="links">
<div class="logo">
<h3>WISTIA</h3>
</div>
<div class="menu">
<ul>
<li
class="main-link"
onmouseenter="dropdownDisplay()"
onmouseout="chevronTransition()"
>
Product <i class="fas fa-chevron-down"></i>
<div class="dropdown">
<div class="cta">
<h4>Product</h4>
<p>
Use our software and your videos to grow your business.
</p>
<a href="#">Start for free</a>
</div>
<div class="major">
<ul>
<li><a href="#">Features</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Integration</a></li>
</ul>
</div>
<div class="sub-menu-links">
<div class="sub-menu">
<h6>CREATE</h6>
<ul>
<li><a href="#">Recording</a></li>
<li><a href="#">Hosting</a></li>
<li><a href="#">Collaboration</a></li>
<li><a href="#">Management</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>PRESENT</h6>
<ul>
<li><a href="#">Player</a></li>
<li><a href="#">Embedding</a></li>
<li><a href="#">Interactivity</a></li>
<li><a href="#">Channels</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>GROW</h6>
<ul>
<li><a href="#">SEO</a></li>
<li><a href="#">Subscribers</a></li>
<li><a href="#">Reach & retarget</a></li>
<li><a href="#">Analytics</a></li>
</ul>
</div>
</div>
</div>
</li>
<li class="main-link">
Learning Center <i class="fas fa-chevron-down"></i>
</li>
<li class="main-link">
Podcasts & Series <i class="fas fa-chevron-down"></i>
</li>
<li class="main-link">
About <i class="fas fa-chevron-down"></i>
</li>
</ul>
</div>
</div>
<div class="buttons">
<a href="#">Sign In</a>
<a href="#">Get Started</a>
</div>
</div>
</nav>
null
您尚未为下拉列表添加悬停状态。你得加上
nav .container .links .menu ul .main-link:hover .dropdown {
display: block;
}
添加mouseover和mouseout事件:
null
let dropdown = document.querySelector(".dropdown");
let chevron = document.querySelector(".main-link i");
function dropdownDisplay() {
dropdown.style.display = "flex";
}
function dropdownHide() {
dropdown.style.display = "none";
}
function chevronTransition() {
chevron.style.trasform = "rotateX(0deg)";
}
document.querySelector('#product-link').addEventListener("mouseover", () => {
dropdownDisplay();
});
document.querySelector('#product-link').addEventListener("mouseout", () => {
dropdownHide();
});
:root {
--font1: roboto;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
nav {
width: 100%;
height: 70px;
border-bottom: 1px solid #ddd;
font-family: var(--font1);
}
nav .container {
width: 1340px;
height: 100%;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
position: relative;
}
nav .container .links {
display: flex;
align-items: center;
}
nav .container .links .menu {
margin-left: 32px;
}
nav .container .links .menu ul .main-link {
list-style: none;
display: inline-block;
margin-right: 24px;
cursor: pointer;
}
nav .container .links .menu ul .main-link:hover i {
transform: rotateX(180deg);
}
nav .container .links .menu ul .main-link .dropdown {
width: 1340px;
position: absolute;
top: 100%;
left: 0;
padding: 32px 0;
cursor: initial;
display: none;
border-bottom: 1px solid #ddd;
}
nav .container .links .menu ul .main-link .dropdown .cta {
width: 300px;
padding-right: 16px;
}
nav .container .links .menu ul .main-link .dropdown .cta h4 {
color: #3ea9f5;
font-size: 30px;
}
nav .container .links .menu ul .main-link .dropdown .cta p {
color: #505050;
margin-bottom: 32px;
margin-top: 24px;
}
nav .container .links .menu ul .main-link .dropdown .cta a {
color: #fff;
background-color: #3ea9f5;
padding: 12px 32px;
border-radius: 6px;
text-decoration: none;
font-weight: bold;
display: inline-block;
}
nav .container .links .menu ul .main-link .dropdown .cta a:focus {
outline: none;
}
nav .container .links .menu ul .main-link .dropdown .major {
width: 150px;
}
nav .container .links .menu ul .main-link .dropdown .major ul li {
display: block;
margin-bottom: 8px;
}
nav .container .links .menu ul .main-link .dropdown .major ul li a {
color: #505050;
font-weight: 700;
text-decoration: none;
}
nav .container .links .menu ul .main-link .dropdown .sub-menu-links {
display: flex;
}
nav .container .links .menu ul .main-link .dropdown .sub-menu-links .sub-menu {
margin-right: 20px;
width: 150px;
}
nav .container .links .menu ul .main-link .dropdown .sub-menu-links .sub-menu h6 {
font-weight: bold;
color: #a7afba;
font-size: 14px;
margin-bottom: 12px;
}
nav .container .links .menu ul .main-link .dropdown .sub-menu-links .sub-menu ul li {
display: block;
list-style: none;
margin-bottom: 8px;
}
nav .container .links .menu ul .main-link .dropdown .sub-menu-links .sub-menu ul li a {
font-size: 15px;
text-decoration: none;
color: #505050;
}
nav .container .links .menu ul .main-link i {
font-size: 12px;
}
nav .container .links .logo h3 {
font-size: 36px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Navbar wistia</title>
<link rel="stylesheet" href="style.css" />
<script src="https://kit.fontawesome.com/7e5d687fd9.js" crossorigin="anonymous"></script>
</head>
<body>
<nav>
<div class="container">
<div class="links">
<div class="logo">
<h3>WISTIA</h3>
</div>
<div class="menu">
<ul>
<li class="main-link" id="product-link">
Product <i class="fas fa-chevron-down"></i>
<div class="dropdown">
<div class="cta">
<h4>Product</h4>
<p>
Use our software and your videos to grow your business.
</p>
<a href="#">Start for free</a>
</div>
<div class="major">
<ul>
<li><a href="#">Features</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Integration</a></li>
</ul>
</div>
<div class="sub-menu-links">
<div class="sub-menu">
<h6>CREATE</h6>
<ul>
<li><a href="#">Recording</a></li>
<li><a href="#">Hosting</a></li>
<li><a href="#">Collaboration</a></li>
<li><a href="#">Management</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>PRESENT</h6>
<ul>
<li><a href="#">Player</a></li>
<li><a href="#">Embedding</a></li>
<li><a href="#">Interactivity</a></li>
<li><a href="#">Channels</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>GROW</h6>
<ul>
<li><a href="#">SEO</a></li>
<li><a href="#">Subscribers</a></li>
<li><a href="#">Reach & retarget</a></li>
<li><a href="#">Analytics</a></li>
</ul>
</div>
</div>
</div>
</li>
<li class="main-link">
Learning Center <i class="fas fa-chevron-down"></i>
</li>
<li class="main-link">
Podcasts & Series <i class="fas fa-chevron-down"></i>
</li>
<li class="main-link">
About <i class="fas fa-chevron-down"></i>
</li>
</ul>
</div>
</div>
<div class="buttons">
<a href="#">Sign In</a>
<a href="#">Get Started</a>
</div>
</div>
</nav>
<script src="./app.js"></script>
</body>
</html>
它可以只做与css改变图标(人字形)在下拉菜单。
nav .container .links .menu ul .main-link:hover i {
transform: rotateX(180deg);
}
nav .container .links .menu ul .main-link i {
transform: rotateX(0deg);
}
悬停“显示下拉内容”
nav .container .links .menu ul .main-link:hover .dropdown {
display: flex;
}
nav .container .links .menu ul .main-link .dropdown {
display: none;
}
null
:root {
--font1: roboto;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
nav {
width: 100%;
height: 70px;
border-bottom: 1px solid #ddd;
font-family: var(--font1);
}
nav .container {
width: 1340px;
height: 100%;
margin: 0 auto;
display: flex;
justify-content: space-between;
align-items: center;
position: relative;
}
nav .container .links {
display: flex;
align-items: center;
}
nav .container .links .menu {
margin-left: 32px;
}
nav .container .links .menu ul .main-link {
list-style: none;
display: inline-block;
margin-right: 24px;
cursor: pointer;
}
nav .container .links .menu ul .main-link:hover i {
transform: rotateX(180deg);
}
nav .container .links .menu ul .main-link i {
transform: rotateX(0deg);
}
nav .container .links .menu ul .main-link .dropdown {
width: 1340px;
position: absolute;
top: 100%;
left: 0;
padding: 32px 0;
cursor: initial;
display: none;
border-bottom: 1px solid #ddd;
}
nav .container .links .menu ul .main-link:hover .dropdown {
display: flex;
}
nav .container .links .menu ul .main-link .dropdown .cta {
width: 300px;
padding-right: 16px;
}
nav .container .links .menu ul .main-link .dropdown .cta h4 {
color: #3ea9f5;
font-size: 30px;
}
nav .container .links .menu ul .main-link .dropdown .cta p {
color: #505050;
margin-bottom: 32px;
margin-top: 24px;
}
nav .container .links .menu ul .main-link .dropdown .cta a {
color: #fff;
background-color: #3ea9f5;
padding: 12px 32px;
border-radius: 6px;
text-decoration: none;
font-weight: bold;
display: inline-block;
}
nav .container .links .menu ul .main-link .dropdown .cta a:focus {
outline: none;
}
nav .container .links .menu ul .main-link .dropdown .major {
width: 150px;
}
nav .container .links .menu ul .main-link .dropdown .major ul li {
display: block;
margin-bottom: 8px;
}
nav .container .links .menu ul .main-link .dropdown .major ul li a {
color: #505050;
font-weight: 700;
text-decoration: none;
}
nav .container .links .menu ul .main-link .dropdown .sub-menu-links {
display: flex;
}
nav .container .links .menu ul .main-link .dropdown .sub-menu-links .sub-menu {
margin-right: 20px;
width: 150px;
}
nav
.container
.links
.menu
ul
.main-link
.dropdown
.sub-menu-links
.sub-menu
h6 {
font-weight: bold;
color: #a7afba;
font-size: 14px;
margin-bottom: 12px;
}
nav
.container
.links
.menu
ul
.main-link
.dropdown
.sub-menu-links
.sub-menu
ul
li {
display: block;
list-style: none;
margin-bottom: 8px;
}
nav
.container
.links
.menu
ul
.main-link
.dropdown
.sub-menu-links
.sub-menu
ul
li
a {
font-size: 15px;
text-decoration: none;
color: #505050;
}
nav .container .links .menu ul .main-link i {
font-size: 12px;
}
nav .container .links .logo h3 {
font-size: 36px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Navbar wistia</title>
<link rel="stylesheet" href="style.css" />
<script
src="https://kit.fontawesome.com/7e5d687fd9.js"
crossorigin="anonymous"
></script>
</head>
<body>
<nav>
<div class="container">
<div class="links">
<div class="logo">
<h3>WISTIA</h3>
</div>
<div class="menu">
<ul>
<li
class="main-link"
>
Product <i class="fas fa-chevron-down"></i>
<div class="dropdown">
<div class="cta">
<h4>Product</h4>
<p>
Use our software and your videos to grow your business.
</p>
<a href="#">Start for free</a>
</div>
<div class="major">
<ul>
<li><a href="#">Features</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Integration</a></li>
</ul>
</div>
<div class="sub-menu-links">
<div class="sub-menu">
<h6>CREATE</h6>
<ul>
<li><a href="#">Recording</a></li>
<li><a href="#">Hosting</a></li>
<li><a href="#">Collaboration</a></li>
<li><a href="#">Management</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>PRESENT</h6>
<ul>
<li><a href="#">Player</a></li>
<li><a href="#">Embedding</a></li>
<li><a href="#">Interactivity</a></li>
<li><a href="#">Channels</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>GROW</h6>
<ul>
<li><a href="#">SEO</a></li>
<li><a href="#">Subscribers</a></li>
<li><a href="#">Reach & retarget</a></li>
<li><a href="#">Analytics</a></li>
</ul>
</div>
</div>
</div>
</li>
<li class="main-link">
Learning Center <i class="fas fa-chevron-down"></i>
<div class="dropdown">
<div class="cta">
<h4>Learning Center</h4>
<p>
Use our software and your videos to grow your business.
</p>
<a href="#">Start for free</a>
</div>
<div class="major">
<ul>
<li><a href="#">Features</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Integration</a></li>
</ul>
</div>
<div class="sub-menu-links">
<div class="sub-menu">
<h6>CREATE</h6>
<ul>
<li><a href="#">Recording</a></li>
<li><a href="#">Hosting</a></li>
<li><a href="#">Collaboration</a></li>
<li><a href="#">Management</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>PRESENT</h6>
<ul>
<li><a href="#">Player</a></li>
<li><a href="#">Embedding</a></li>
<li><a href="#">Interactivity</a></li>
<li><a href="#">Channels</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>GROW</h6>
<ul>
<li><a href="#">SEO</a></li>
<li><a href="#">Subscribers</a></li>
<li><a href="#">Reach & retarget</a></li>
<li><a href="#">Analytics</a></li>
</ul>
</div>
</div>
</div>
</li>
<li class="main-link">
Podcasts & Series <i class="fas fa-chevron-down"></i>
<div class="dropdown">
<div class="cta">
<h4>Podcasts & Series</h4>
<p>
Use our software and your videos to grow your business.
</p>
<a href="#">Start for free</a>
</div>
<div class="major">
<ul>
<li><a href="#">Features</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Integration</a></li>
</ul>
</div>
<div class="sub-menu-links">
<div class="sub-menu">
<h6>CREATE</h6>
<ul>
<li><a href="#">Recording</a></li>
<li><a href="#">Hosting</a></li>
<li><a href="#">Collaboration</a></li>
<li><a href="#">Management</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>PRESENT</h6>
<ul>
<li><a href="#">Player</a></li>
<li><a href="#">Embedding</a></li>
<li><a href="#">Interactivity</a></li>
<li><a href="#">Channels</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>GROW</h6>
<ul>
<li><a href="#">SEO</a></li>
<li><a href="#">Subscribers</a></li>
<li><a href="#">Reach & retarget</a></li>
<li><a href="#">Analytics</a></li>
</ul>
</div>
</div>
</div>
</li>
<li class="main-link">
About <i class="fas fa-chevron-down"></i>
<div class="dropdown">
<div class="cta">
<h4>About</h4>
<p>
Use our software and your videos to grow your business.
</p>
<a href="#">Start for free</a>
</div>
<div class="major">
<ul>
<li><a href="#">Features</a></li>
<li><a href="#">Pricing</a></li>
<li><a href="#">Integration</a></li>
</ul>
</div>
<div class="sub-menu-links">
<div class="sub-menu">
<h6>CREATE</h6>
<ul>
<li><a href="#">Recording</a></li>
<li><a href="#">Hosting</a></li>
<li><a href="#">Collaboration</a></li>
<li><a href="#">Management</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>PRESENT</h6>
<ul>
<li><a href="#">Player</a></li>
<li><a href="#">Embedding</a></li>
<li><a href="#">Interactivity</a></li>
<li><a href="#">Channels</a></li>
</ul>
</div>
<div class="sub-menu">
<h6>GROW</h6>
<ul>
<li><a href="#">SEO</a></li>
<li><a href="#">Subscribers</a></li>
<li><a href="#">Reach & retarget</a></li>
<li><a href="#">Analytics</a></li>
</ul>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
<div class="buttons">
<a href="#">Sign In</a>
<a href="#">Get Started</a>
</div>
</div>
</nav>
<script src="./app.js"></script>
</body>
</html>