console
<style>
body {
font-family: Helvetica, Arial, sans-serif;
min-height: 200vh;
margin: 0;
}
button {
padding: .5em .7em;
border: 1px solid #8d8d8d;
background-color: white;
font-size: 1em;
}
.top-banner {
padding: 1em 0;
background-color: #ffd698;
}
.top-banner-inner {
width: 80%;
max-width: 1000px;
margin: 0 auto;
}
.modal {
/* 先隐藏 */
/* display: none; */
}
/* 蒙层 */
.modal-backdrop {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.5);
/* 蒙层盖住menu菜单 */
z-index:1;
}
.modal-body {
position: fixed;
top: 3em;
bottom: 3em;
right: 20%;
left: 20%;
padding: 2em 3em;
background-color: white;
overflow: auto;
/* 弹窗主体盖住蒙层 */
z-index:2;
}
.modal-close {
cursor: pointer;
position: absolute;
top: 0;
right: 0;
padding: 0.3em;
font-size: 2em;
height: 1.5em;
width: 1.5em;
border: 0;
}
.modal-close::before {
display: block;
content: '\00D7';
}
/* 无障碍访问:隐藏close */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
}
.container {
width: 80%;
max-width: 1000px;
margin: 1em auto;
}
.dropdown {
display: inline-block;
position: relative;
}
.dropdown-label {
padding: 0.5em 2em 0.5em 1.5em;
border: 1px solid #ccc;
background-color: #eee;
}
.dropdown-label::after {
content: " ";
position: absolute;
right: 1em;
top: 1em;
border: 0.3em solid;
/* 边框倒立小三角 */
border-color: black transparent transparent;
}
/* 打开时翻转小三角 */
.dropdown:hover .dropdown-label::after {
top: 0.7em;
border-color: transparent transparent black;
}
.dropdown-menu {
/* 先隐藏 */
display:none;
position: absolute;
left: 0;
top: 2.1em;
min-width: 100%;
background-color: #eee;
}
.dropdown:hover .dropdown-menu {
/* 再hover显示 */
display: block;
}
.submenu {
padding-left: 0;
margin: 0;
list-style-type: none;
border: 1px solid #999;
}
/* 除了第一个li的所有li */
.submenu>li+li {
border-top: 1px solid #999;
}
.submenu>li>a {
display: block;
padding: 0.5em 1.5em;
background-color: #eee;
color: #369;
text-decoration: none;
}
.submenu>li>a:hover {
background-color: #fff;
}
</style>
<body>
<!-- p和button -->
<header class="top-banner">
<div class="top-banner-inner">
<p>Find out what's going on at Wombat Coffee each month. Sign up for our newsletter:
<button id="open">Sign up</button>
</p>
</div>
</header>
<!-- 弹窗 -->
<div class="modal" id="modal">
<div class="modal-backdrop"></div>
<div class="modal-body">
<button class="modal-close" id="close">
<span class="sr-only">close</span>
</button>
<h2>Wombat Newsletter</h2>
<p>Sign up for our monthly newsletter. No spam.
We promise!</p>
<form>
<p>
<label for="email">Email address:</label>
<input type="text" name="email"/>
</p>
<p><button type="submit">Submit</button></p>
</form>
</div>
</div>
<!-- 下来菜单 -->
<div class="container">
<nav>
<div class="dropdown">
<div class="dropdown-label">Main Menu</div>
<div class="dropdown-menu">
<ul class="submenu">
<li><a href="/">Home</a></li>
<li><a href="/coffees">Coffees</a></li>
<li><a href="/brewers">Brewers</a></li>
<li><a href="/specials">Specials</a></li>
<li><a href="/about">About us</a></li>
</ul>
</div>
</div>
</nav>
<h1>Wombat Coffee Roasters</h1>
</div>
<script type="text/javascript">
var button = document.getElementById('open');
var close = document.getElementById('close');
var modal = document.getElementById('modal');
// 打开
button.addEventListener('click', function(event) {
event.preventDefault();
modal.style.display = 'block';
});
// 隐藏
close.addEventListener('click', function(event) {
event.preventDefault();
modal.style.display = 'none';
});
</script>
</body>