html - 下拉菜单在淡出期间失去背景渐变

标签 html css drop-down-menu css-transitions transition

我用 HTML 和 CSS 编写了一个下拉菜单,当用户的鼠标悬停在菜单中的按钮上时,我可以成功地让它淡入。但是,当鼠标离开下拉菜单时,下拉菜单会淡出,除了下拉菜单的背景。

这是因为当鼠标离开下拉框后,带有背景的容器 div高度立即变为零。

如果我不将高度设置为零,

如何解决 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>

关于html - 下拉菜单在淡出期间失去背景渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41818818/

相关文章:

javascript - 从下拉列表列表中仅显示所选列表及其子类别

java - HTML 5 输入类型提交不会作为参数传递给 Controller

javascript - 将类别应用于选定的 LI 和所有以前的 LI

html - 在水平显示中添加菜单项

html - ios 11 Beta 5 选择下拉列表问题

javascript - 点击提交按钮后如何使用 "display: block"?

html - 没有相对宽度的CSS位置相对?

css - 用于在非链接元素上悬停样式的 Internet Explorer hack?

javascript - 试图让我的 slider 响应。不断添加边距以在@media 查询之间进行偏移

css - 如何在下拉菜单上显示白色背景溢出?