javascript - 响应式菜单不显示

标签 javascript html css menu responsive

我创建了一个响应式菜单,但它没有显示。我想使用 汉堡包菜单 按钮。当它处于事件状态时(按钮转换+转换汉堡菜单图标 -> 关闭图标),ul 将从右侧滑出(背景 -> opacity.5)。

我不确定哪部分是错误的。你有什么建议吗?

$(document).ready(function() {
  $('#toggle').on("click", function() {
    $("#overlay").toggleClass('active');
    $('#overlay').toggleClass('open');
  });
});
.button-container {
  position: fixed;
  top: 5%;
  right: 2%;
  height: 27px;
  width: 35px;
  cursor: pointer;
  z-index: 100;
  transition: opacity .25s ease;
  opacity: 1;
  content: "";
}

.button-container:hover {
  opacity: .7;
}

.top:active {
  transform: translate(11px) translateX(0) rotate(45deg);
}

.middle:active {
  opacity: 0;
}

.bottom:active {
  transform: translateY(-11px) translateX(0) rotate(-45deg);
}

span {
  background: rgba(0, 0, 0, 0.6);
  border: none;
  height: 5px;
  width: 5%;
  position: absolute;
  top: 0;
  left: 0;
  transition: all .35s ease;
  cursor: pointer;
}

.overlay {
  position: fixed;
  background: rgba(0, 0, 0, 0.6);
  top: 0;
  left: 0;
  width: 5%;
  height: 0%;
  opacity: .6;
  visibility: hidden;
  transition: opacity .35s, visibility .35s, height .35s;
}

li {
  animation: fadeInRight .5s ease forwards;
  animation-delay: .35s;
}

li:nth-of-type(2) {
  animation-delay: .4s;
}

li:nth-of-type(3) {
  animation-delay: .45s;
}

li:nth-of-type(4) {
  animation-delay: .50s;
}

nav {
  position: relative;
  height: 70%;
  top: 20%;
  transform: translateY(-50%);
  font-size: 0.8em;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0 auto;
  display: inline-block;
  position: relative;
  height: 100%;
}

li {
  display: block;
  height: 25%;
  min-height: 50px;
  position: relative;
  opacity: 0;
}

a {
  display: block;
  position: relative;
  text-decoration: none;
  overflow: hidden;
}

a:hover {
  transform: scale(1);
}

a:hover:after,
a:focus:after,
a:active:after {
  width: 30%;
}

a:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0%;
  transform: translateX(-50%);
  height: 3px;
  background: rgba(0, 0, 0, 0.6);
  transition: .35s;
}

@keyframes fadeInRight {
  0% {
    opacity: 0;
    left: 20%;
  }
  100% {
    opacity: 1;
    left: 0;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="button-container" id="toggle">
  <span class="top"></span>
  <span class="middle"></span>
  <span class="bottom"></span>
</div>
<div class="overlay" id="overlay">
  <nav class="overlay-menu">
    <ul>
      <li><a href="#constellation">Home</a></li>
      <li><a href="#what is constellations?">About</a></li>
      <li><a href="#12 constellations">12 Constellations</a></li>
      <li><a href="#How to Find Constellations in the Night Sky?">Stargazing</a></li>
    </ul>
  </nav>
</div>

最佳答案

您需要将visibility:visible 添加到.active 类。以及一些 css 修改以显示菜单栏。

.active {
  visibility: visible;
}

$(document).ready(function() {
  $('#toggle').on("click", function() {
    $("#overlay").toggleClass('active');
    $('#overlay').toggleClass('open');
  });
});
.button-container {
  position: fixed;
  top: 5%;
  right: 2%;
  height: 27px;
  width: 35px;
  cursor: pointer;
  z-index: 100;
  transition: opacity .25s ease;
  opacity: 1;
}

.button-container:hover {
  opacity: .7;
}

.top:active {
  transform: translate(11px) translateX(0) rotate(45deg);
}

.middle:active {
  opacity: 0;
}

.bottom:active {
  transform: translateY(-11px) translateX(0) rotate(-45deg);
}

span {
  background: rgba(0, 0, 0, 0.6);
  border: none;
  height: 5px;
  width: 30px;
  position: absolute;
  top: 0;
  left: 0;
  transition: all .35s ease;
  cursor: pointer;
}
.middle{
  top:10px;
}
.bottom{
  top:20px;
}
.overlay {
  position: fixed;
  background: rgba(0, 0, 0, 0.6);
  top: 0;
  left: 0;
  width: 5%;
  height: 0%;
  opacity: .6;
  visibility: hidden;
  transition: opacity .35s, visibility .35s, height .35s;
}

li {
  animation: fadeInRight .5s ease forwards;
  animation-delay: .35s;
}

li:nth-of-type(2) {
  animation-delay: .4s;
}

li:nth-of-type(3) {
  animation-delay: .45s;
}

li:nth-of-type(4) {
  animation-delay: .50s;
}

nav {
  position: relative;
  height: 70%;
  top: 20%;
  transform: translateY(-50%);
  font-size: 0.8em;
}

ul {
  list-style: none;
  padding: 0;
  margin: 0 auto;
  display: inline-block;
  position: relative;
  height: 100%;
}

li {
  display: block;
  height: 25%;
  min-height: 50px;
  position: relative;
  opacity: 0;
}

a {
  display: block;
  position: relative;
  text-decoration: none;
  overflow: hidden;
}

a:hover {
  transform: scale(1);
}

a:hover:after,
a:focus:after,
a:active:after {
  width: 30%;
}

a:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 0%;
  transform: translateX(-50%);
  height: 3px;
  background: rgba(0, 0, 0, 0.6);
  transition: .35s;
}

@keyframes fadeInRight {
  0% {
    opacity: 0;
    left: 20%;
  }
  100% {
    opacity: 1;
    left: 0;
  }
}

.active {
  visibility: visible;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="button-container" id="toggle">
  <span class="top"></span>
  <span class="middle"></span>
  <span class="bottom"></span>
</div>
<div class="overlay" id="overlay">
  <nav class="overlay-menu">
    <ul>
      <li><a href="#constellation">Home</a></li>
      <li><a href="#what is constellations?">About</a></li>
      <li><a href="#12 constellations">12 Constellations</a></li>
      <li><a href="#How to Find Constellations in the Night Sky?">Stargazing</a></li>
    </ul>
  </nav>
</div>

关于javascript - 响应式菜单不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57491350/

相关文章:

javascript - Node.js 中的内联子进程

php - 如何从 Playstation Store 获取价格

javascript - jQuery 音频 : How to Allow Overlapping Sounds?

html - Bootstrap data-affix/position-fixed col 与其他 col 重叠

javascript - 更改 leaflet markercluster 图标颜色,继承其余默认 CSS 属性

html - 掩码属性在 Firefox 中不起作用

css - 更改 Bootstrap Carousel 淡入淡出过渡

javascript - JavaScript 的问题

javascript - FB.Event.subscribe & FB.getLoginStatus 未触发

javascript - 如何在a(c.test)中得到与c.test()相同的结果?