html - 添加桌面布局时移动菜单中出现奇怪的底部边距(或空格)

标签 html css mobile responsive-design codepen

我正在为网站设计移动菜单(使用 codepen)。菜单的桌面布局已经写好了。

移动布局已按预期编写并运行良好。但是添加桌面布局后,移动布局中菜单底部出现了一个奇怪的空间。桌面布局保持不变。

可以在以下位置找到布局:https://codepen.io/pcassima/pen/ZgWYOd 这仅包括菜单的 HTML 和样式。

我已经尝试在桌面布局中注释掉填充和边距,但这并没有删除空间。

我希望移动菜单的菜单栏底部没有空间。

function hamburgerNav() {
  // Get the navigation list from the page
  let nav_list = document.getElementById("nav-list");
  // Get the hamburger from the page (to change the shape)
  let hamburger = document.getElementById("hamburger");

  // Add or remove the active class from the navigation list
  if (nav_list.classList.contains("active")) {
    nav_list.classList.remove("active");
  } else {
    nav_list.classList.add("active");
  }
  // Add or remove the active class from the hamburger
  if (hamburger.classList.contains("active")) {
    hamburger.classList.remove("active");
  } else {
    hamburger.classList.add("active");
  }
}
* {
  /* Unsetting the margin and padding for everything */
  margin: 0;
  padding: 0;
}

body {
  /* Setting the global font */
  font-family: Roboto, sans-serif;
}


/* Desktop layout */

nav {
  /* The global navigation container for the hamburger and navigation*/
  box-shadow: 3px 3px 6px 0 rgba(75, 75, 75, 0.64);
  background-color: #20dce1;
  text-align: center;
  display: block;
  z-index: 1;
  position: sticky;
  top: 0;
}

nav .hamburger {
  /* In desktop mode hide the hamburger */
  display: none;
}

.top-navbar {
  /* The container for the actual navigation */
  display: inline-block;
  width: 100%;
  margin-bottom: -4px;
}

.nav-list {
  /* The unordered list for the navigation links */
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav-list li {
  /* The menu list items */
  float: left;
  width: 20%;
}

nav a {
  /* styles for the menu links */
  padding-top: 4px;
  padding-bottom: 6px;
  color: white;
  display: inline-block;
  font-weight: normal;
  font-size: 28px;
  text-decoration: none;
  text-transform: uppercase;
}

.nav-top-accent {
  /* The top accents for the menu links (for the desktop view) */
  background-color: #e11c84;
  height: 4px;
  transform-origin: center;
  transform: scaleX(0);
  transition: transform 350ms ease-in-out;
}

.nav-list li:hover .nav-top-accent,
.nav-list .nav-active .nav-top-accent {
  /* Scaling the top accent bar for hover and the active link (current page)   */
  transform: scaleX(1);
}

@media screen and (max-width: 750px) {
  /* Mobile layout */
  nav .top-navbar {
    /* The container for the actual navigation */
    overflow: auto;
    width: 100%;
    margin: 0;
  }
  .nav-list li {
    /* The menu list items */
    /* Unsetting the float, from the desktop layout */
    float: none;
    width: 100%;
    margin-bottom: 4px;
  }
  nav .nav-top-accent {
    /* The top accents for the menu links (for the desktop view) */
    display: none;
  }
  nav a {
    /* styles for the menu links */
    text-align: center;
    font-size: 24px;
    padding: 0;
  }
  nav .nav-active {
    /* The active menu link (the current page) */
    background-color: #e11c84;
  }
  nav .nav-list {
    /* The default display is none (inactive) */
    display: none;
  }
  nav .nav-list.active {
    /* When the menu becomes active, display the menu links */
    display: block;
  }
  nav .hamburger {
    /* The container for the hamburger button */
    display: block;
    float: right;
    margin-right: 8px;
    margin-top: 4px;
    margin-bottom: 4px;
    text-align: center;
    cursor: pointer;
  }
  nav .hamburger.active #h-bar1 {
    /* When the menu becomes active, rotate the top bar */
    transform: translate(0, 250%) rotate(45deg);
  }
  nav .hamburger.active #h-bar2 {
    /* When the menu becomes active, scale the bar in the x-direction to 0 */
    transform-origin: 100% 50%;
    transform: scaleX(0);
  }
  nav .hamburger.active #h-bar3 {
    /* When the menu becomes active, rotate the bottom bar */
    transform: translate(0, -250%) rotate(-45deg);
  }
  nav .hamburger-bar {
    /* Styling for the individual bars for the hamburger */
    width: 40px;
    height: 4px;
    background-color: white;
    margin: 6px 0;
    /* Animating the transistions */
    transition: all ease-in-out 350ms;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>Page Title</title>

  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto" />
</head>

<body>
  <header>
    <h1 style="text-align:center; font-size:38px;margin:16px;">responsive menu</h1>
  </header>
  <nav>
    <!-- The construction for the hamburger (for mobile menus)-->
    <div class="hamburger" id="hamburger" onclick="hamburgerNav()">
      <div class="hamburger-bar" id="h-bar1"></div>
      <div class="hamburger-bar" id="h-bar2"></div>
      <div class="hamburger-bar" id="h-bar3"></div>
    </div>
    <!-- The actual nav-bar with the links-->
    <div class="top-navbar">
      <ul class="nav-list" id="nav-list">
        <li class="nav-active">
          <div class="nav-top-accent"></div>
          <a href="index.html">home</a>
        </li>
        <li>
          <div class="nav-top-accent"></div>
          <a href="#">projects</a>
        </li>
        <li>
          <div class="nav-top-accent"></div>
          <a href="#">tutorials</a>
        </li>
        <li>
          <div class="nav-top-accent"></div>
          <a href="#">about</a>
        </li>
        <li>
          <div class="nav-top-accent"></div>
          <a href="#">contact</a>
        </li>
      </ul>
    </div>
  </nav>

  <h1 style="margin:8px;">A responsive menu</h1>
  <p style="margin:8px;">A responsive menu for mobile and desktop. On desktop the menu has a hover animation, while on mobile there is an animated hamburger button to open the menu.</p>

</body>

</html>

最佳答案

如果我理解正确,那么如果您删除 .top-navbar 类上的显示内联 block ,您应该可以直观地看到空间消失/删除。

仅供引用:只有当您的移动菜单处于事件状态时,您才应该删除它。在桌面版本上,这仍然是必要的。

关于html - 添加桌面布局时移动菜单中出现奇怪的底部边距(或空格),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57180116/

相关文章:

javascript - 如何删除数据表上的文本 "entries"

html - 如何强制某些对象位于两条不同的线上?

html - div 高度在 firefox 的浏览器窗口中不完全 100%,但在 chrome、ie 和 safari 中效果很好

html - 文本对齐:居中;不工作

c# - 从移动设备查看的 asp.net 页面上的回发问题

javascript - 如何以通用样式排列文本框

php - 尝试链接填充的下拉列表 PHP

html - 在 Google map 之外保存 map 实例

ios - 如何检查 iOS 中的 "Use Mobile Data"设置?

jQuery Mobile 控件组水平布局问题