将移动菜单拉伸到全屏高度

时间:2018-08-21 17:30:53

标签: javascript jquery html css

当前我有此移动菜单

$(document).ready(() => {
  $("#btnMenu").click(() => {
    toggleMenu();
  });

  $(".navbarLink").click(() => {
    if ($("#navbarItems").hasClass("activeNavbar")) {
      toggleMenu();
    }
  });
});

function toggleMenu() {
  $("#navbarItems").toggleClass("activeNavbar");
  toggleMenuBtn();
}

function toggleMenuBtn() {
  $("#btnMenu").toggleClass("activeMenuBtn");
}
.link {
  text-decoration: none;
}

body {
  margin: 0;
}

#navbar {
  height: 60px;
  top: 0;
  padding-left: 200px;
  padding-right: 200px;
  position: sticky;
  background: #1e222a;
}

#navbarItems {
  height: 100%;
  display: flex;
  align-items: center;
}

#logoLink {
  display: flex;
  align-items: center;
}

#navbarItems .navbarItemContainer:not(:first-child) {
  margin-left: 30px;
}

.navbarItemContainer {
  background: #1e222a;
}

.navbarLink {
  font-weight: bold;
  color: #ffffff;
}

.navbarLink:hover {
  color: #3abcf3;
}

#btnMenuContainer {
  height: 100%;
  display: none;
}

#btnMenu {
  cursor: pointer;
}

.menuBtnBar {
  width: 35px;
  height: 5px;
  margin: 6px 0;
  background-color: #ffffff;
  transition: 0.4s;
}

.activeMenuBtn #barTop {
  transform: rotate(-45deg) translate(-9px, 6px);
}

.activeMenuBtn #barCenter {
  opacity: 0;
}

.activeMenuBtn #barBottom {
  transform: rotate(45deg) translate(-8px, -8px);
}

@media(max-width: 1200px) {
  #navbar {
    padding-left: 150px;
    padding-right: 150px;
  }
}

@media(max-width: 1100px) {
  #navbar {
    padding-left: 0;
    padding-right: 0;
  }
  #navbarItems .navbarItemContainer:not(:first-child) {
    margin-left: 0;
  }
  #navbarItems .navbarItemContainer:not(:last-child) {
    border-bottom: 1px solid #676767;
  }
  #btnMenuContainer {
    display: flex;
    align-items: center;
  }
  #btnMenu {
    margin-left: 20px;
  }
  #navbarItems {
    margin-left: 0;
    display: none;
  }
  #logoLink {
    display: inline-block;
  }
  .navbarItem {
    width: 100%;
    text-align: center;
  }
  #navbarItems.activeNavbar {
    display: block;
  }
  .navbarLink {
    width: 100%;
    height: 100%;
    display: inline-block;
    padding: 30px 0;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="navbar">
  <div id="btnMenuContainer">
    <div id="btnMenu">
      <div id="barTop" class="menuBtnBar"></div>
      <div id="barCenter" class="menuBtnBar"></div>
      <div id="barBottom" class="menuBtnBar"></div>
    </div>
  </div>

  <div id="navbarItems">
    <div class="navbarItemContainer">
      <div class="navbarItem">
        <a id="logoLink" class="link navbarLink" href="#">
          <img class="img" src="http://icons.iconarchive.com/icons/ph03nyx/super-mario/128/Hat-Mario-icon.png">
        </a>
      </div>
    </div>

    <div class="navbarItemContainer">
      <div class="navbarItem">
        <a class="link navbarLink" href="#sectionTwo">
                    Link 2
                </a>
      </div>
    </div>

    <div class="navbarItemContainer">
      <div class="navbarItem">
        <a class="link navbarLink" href="#sectionThree">
                    Link 3
                </a>
      </div>
    </div>
  </div>
</div>

,我想将移动菜单拉伸到全宽。因此,我必须根据链接的数量来计算每个链接的高度。

假设屏幕高度为100%,n个链接的数量我认为每个链接的高度为

  

100%/ n-(menubarHeight / n)

如何设置每个链接都达到这个高度的CSS?我只想将页面高度除以链接数量。

menu

您可以在此页面上看到所需的移动菜单示例

https://www.thenativeweb.io/#

在我的@media(max-width: 1100px)底部,我认为我必须在此处更改填充

  .navbarLink {
    width: 100%;
    height: 100%;
    display: inline-block;
    padding: 30px 0;
  }

如何设置全高菜单?

2 个答案:

答案 0 :(得分:0)

您的示例中的菜单使用的是flexbox:

.menu-container {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    /* This will make the container as high as the viewport
       Use calc(100vh - menubarHeight) if you need to make some room for it,
       or create another flex container which contains the menubar and this
       container. */
    height: 100vh; 
}

.menu-item {
    flex: 1 1 100%;
    align-items: center;
    justify-content: center;
}

菜单容器是一个弹性容器,它告诉所有孩子伸展以填充剩余的空间。子项是可以自动收缩或增长的弹性项目,它们在水平和垂直方向上都居中对齐。

菜单容器的父级也是一个flex容器,这意味着您不必计算和减去菜单栏的高度,因为flexbox会为您代劳。

我建议您学习https://css-tricks.com/snippets/css/a-guide-to-flexbox/以了解其工作原理。

答案 1 :(得分:0)

尝试以下jQuery脚本,它将修复您的导航问题,并允许您使用多个具有不同高度的导航链接。我试图使代码易于说明。让我知道您是否无法理解

function fixTheMobileNavigation() {
    var $links = $('.navbarItemContainer'),
        linksContainerFixedHeight = $(window).height() - $('#btnMenuContainer').height(),
        linksLength = $links.length,
        linkNecesseryHeight = (linksContainerFixedHeight / linksLength) - (linksLength - 1);

    $('.link.navbarLink').css('padding', 0)

    $links.each(function(i, item) {
        var paddingTobBottom = ((linkNecesseryHeight - $(item).find(':last').height()) / 2) + "px 0px"
        $(item).css('padding', paddingTobBottom)
    })
}

if($(window).width() < 500) fixTheMobileNavigation()