我已经用HTML和CSS编写了一个下拉菜单,当用户的鼠标悬停在菜单中的按钮上时,我可以成功地让它淡入。但是,当鼠标离开下拉列表时,下拉列表将淡出,但下拉列表的背景除外。
这是因为带有背景的容器div
height
在鼠标离开下拉列表后立即更改为零。
如果我不将高度设置为零,
我怎样才能用div
解决这个高度问题?
body {
background-color: gray;
}
.top-block-container {
float: left;
width: 50%;
margin: 0.5% 0.25%;
}
.top-block-container:hover .top-block-dropdown {
visibility: visible;
opacity: 1;
transform: translateY(0);
height: auto;
}
.top-block {
width: 100%;
font-family: Lato;
font-weight: 900;
text-align: center;
padding: 10px 0;
border-radius: 50px;
font-size: 25px;
transition: background-color 0.25s linear, box-shadow 0.25s linear;
float: left;
background: linear-gradient(to bottom, #CCCCCC, #AAAAAA);
box-shadow: 0 0 5px black;
margin: 0;
}
.top-block:hover {
cursor: pointer;
box-shadow: 0 0 15px black;
}
.top-block-dropdown {
width: 100%;
background: linear-gradient(to bottom, #CCCCCC, #AAAAAA);
float: left;
margin-top: 7.5px;
border-radius: 25px;
box-shadow: 0 0 15px black;
visibility: hidden;
opacity: 0;
height: 0;
transform: translateY(-2em);
transition: visibility 0.25s, transform 0.25s, opacity 0.25s linear;
}
.dropdown-option-heading {
font-family: Lato;
font-weight: 700;
margin: 5px 0 0 0;
padding: 5px 10px;
border-bottom: 2px solid white;
}
.dropdown-option {
font-family: Lato;
font-weight: 400;
text-align: center;
border-bottom: 1px solid white;
margin: 0;
padding: 5px 10px;
transition: box-shadow 0.25s linear;
}
.dropdown-option:hover {
background: linear-gradient(to top, dodgerblue, #00B0FF);
cursor: pointer;
}
.last-option {
border: 0;
border-bottom-left-radius: 25px;
border-bottom-right-radius: 25px;
}
<div class="top-block-container">
<h1 class="top-block">ECWMF</h1>
<div class="top-block-dropdown">
<p class="dropdown-option-heading">Global Models</p>
<p class="dropdown-option">GFS</p>
<p class="dropdown-option">ECMWF</p>
<p class="dropdown-option">CMC</p>
<p class="dropdown-option">NAVGEM</p>
<p class="dropdown-option">UKMET</p>
<p class="dropdown-option-heading">Mesoscale Models</p>
<p class="dropdown-option">HRRR</p>
<p class="dropdown-option">HWRF</p>
<p class="dropdown-option">NAM 32km</p>
<p class="dropdown-option">NAM 12km</p>
<p class="dropdown-option">RAP</p>
<p class="dropdown-option">SREF</p>
<p class="dropdown-option last-option">HIRESW</p>
</div>
</div>
从.top-block-dropdown
中删除height: 0
并使其position: absolute
。
并使其父级position: relative
.
使用top, bottom, left, right
移动它。
body {
background-color: gray;
}
.top-block-container {
float: left;
width: 50%;
margin: 0.5% 0.25%;
position: relative; /* add */
}
.top-block-container:hover .top-block-dropdown {
visibility: visible;
opacity: 1;
transform: translateY(0);
/*height: auto;*/
}
.top-block {
width: 100%;
font-family: Lato;
font-weight: 900;
text-align: center;
padding: 10px 0;
border-radius: 50px;
font-size: 25px;
transition: background-color 0.25s linear, box-shadow 0.25s linear;
float: left;
background: linear-gradient(to bottom, #CCCCCC, #AAAAAA);
box-shadow: 0 0 5px black;
margin: 0;
}
.top-block:hover {
cursor: pointer;
box-shadow: 0 0 15px black;
}
.top-block-dropdown {
width: 100%;
background: linear-gradient(to bottom, #CCCCCC, #AAAAAA);
float: left;
margin-top: 7.5px;
border-radius: 25px;
box-shadow: 0 0 15px black;
visibility: hidden;
opacity: 0;
/*height: 0;*/
transform: translateY(-2em);
transition: visibility 0.25s, transform 0.25s, opacity 0.25s linear;
position: absolute; /* add */
top: 100%; /* push so it appear after .top-block-container*/
}
.dropdown-option-heading {
font-family: Lato;
font-weight: 700;
margin: 5px 0 0 0;
padding: 5px 10px;
border-bottom: 2px solid white;
}
.dropdown-option {
font-family: Lato;
font-weight: 400;
text-align: center;
border-bottom: 1px solid white;
margin: 0;
padding: 5px 10px;
transition: box-shadow 0.25s linear;
}
.dropdown-option:hover {
background: linear-gradient(to top, dodgerblue, #00B0FF);
cursor: pointer;
}
.last-option {
border: 0;
border-bottom-left-radius: 25px;
border-bottom-right-radius: 25px;
}
<div class="top-block-container">
<h1 class="top-block">ECWMF</h1>
<div class="top-block-dropdown">
<p class="dropdown-option-heading">Global Models</p>
<p class="dropdown-option">GFS</p>
<p class="dropdown-option">ECMWF</p>
<p class="dropdown-option">CMC</p>
<p class="dropdown-option">NAVGEM</p>
<p class="dropdown-option">UKMET</p>
<p class="dropdown-option-heading">Mesoscale Models</p>
<p class="dropdown-option">HRRR</p>
<p class="dropdown-option">HWRF</p>
<p class="dropdown-option">NAM 32km</p>
<p class="dropdown-option">NAM 12km</p>
<p class="dropdown-option">RAP</p>
<p class="dropdown-option">SREF</p>
<p class="dropdown-option last-option">HIRESW</p>
</div>
</div>