滚动条上的导航栏高度

时间:2018-08-19 19:46:58

标签: html css html5 css3 flexbox

我正在使用bem方法,对flexbox使用css来创建角度6的简单导航。

以下是导航标记:

<div class="main-header">
  <nav class="main-nav" ng-sticky [offSet]="0" [addClass]="'main-sticky'" appMainNav #ref="appMainNav">
    <div class="main-nav__logo " ng-sticky [offSet]="0" [addClass]="'main-sticky__logo'">
      <img class="man-nav__logo-green" src="/assets/images/logo-white.png">
    </div>
    <div class="main-nav__toggle">
      <i class="main-nav__bars fa fa-bars" ng-sticky [offSet]="0" [addClass]="'main-bars'"></i>
    </div>
    <ul class="main-nav__list " ng-sticky [addClass]="'main-sticky-link'" [ngClass]="ref.click === true? 'Navbar__ToggleShow' :''">
      <li class="main-nav__item">
        <a class="main-nav__link" href="#">Home</a>
      </li>
      <li class="main-nav__item">
        <a class="main-nav__link" href="#">About us</a>
      </li>
      <li class="main-nav__item">
        <a class="main-nav__link" href="#">What we do</a>
      </li>
      <li class="main-nav__item">
        <a class="main-nav__link " href="#">Projects</a>
      </li>
      <li class="main-nav__item">
        <a class="main-nav__link " href="#">Contact</a>
      </li>
    </ul>
  </nav>

  <div class="main-banner">
    <h2>We are a team of
      <strong>experts</strong>.</h2>
    <p>Founded in 2014, the BDTS Poland specializes in IT personnel outsourcing for the areas of banking, insurance, telecommunications,
      high-tech, pharmacy, logistics and many others</p>
    <a href="http://en.astek.pl/about-us/" class="main-banner__green-button main-banner__arrow-right">Read more
      <i></i>
    </a>
    <a href="#" class="main-banner__arrow-down"></a>
  </div>
</div>

以下是导航栏的移动CSS:

@media only screen and(max-width: 768px) {
  .main-nav {
    margin: 0;
    display: block;
    position: inherit;
    height: auto;
    overflow: auto;
    background: white;
  }
  .main-nav__list {
    margin-top: 20px;
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .main-nav__item:after {
    content: '';
    width: 1px;
  }
  .Navbar__ToggleShow {
    display: flex;
  }
  .main-nav__logo {
    position: relative;
    bottom: 49px;
    background-repeat: no-repeat;
    background-image: url("/assets/images/logo-green.png");
  }
  .man-nav__logo-green {
    visibility: hidden !important;
  }
  .main-nav__bars {
    display: flex;
    justify-content: flex-end;
    cursor: pointer;
    padding: 26px;
    font-size: 50px;
    color: #00964e;
    top: 0;
    margin-top: -196px;
  }
  .main-nav__link {
    color: #444;
  }

}

在桌面版本中,一切正常,但在移动版本中,导航栏会增加其滚动高度,并且汉堡菜单消失,

这是我尝试的解决方案:(移除.man-nav中的高度)

.main-nav {
  margin: 0;
  display: block;
  position: inherit;
  overflow: auto;
  background: white;
}

现在导航栏的高度在滚动时不会增加,但是汉堡菜单会消失。


更新-以下是导航栏组件的完整CSS:

@charset "UTF-8";
@font-face {
  font-family: "icomoon";
  src: url("/assets/fonts/icomoon.ttf");
}
.main-header {
  width: 100%;
  float: left;
  display: block;
  min-height: 100vh;
  height: 100%;
  background-size: cover;
  background-position: center;
  padding-bottom: 100px;
  background-image: url("/assets/images/banner.jpg");
}

.main-sticky {
  background-color: white;
  height: 100px;
}
.main-sticky__logo {
  position: relative !important;
  background-repeat: no-repeat;
  background-image: url("/assets/images/logo-green.png");
}

.main-sticky-link .main-nav__link {
  color: #444 !important;
}

.main-nav {
  position: sticky;
  align-items: center;
  justify-content: space-around;
  left: 0;
  display: flex;
  z-index: 2;
  width: 100%;
}
.main-nav__logo {
  position: relative;
}
.main-nav__bars {
  color: white;
}
.main-nav__list {
  list-style: none;
  display: flex;
  color: white;
  position: relative !important;
}
.main-nav__item {
  padding: 24px 27px;
  position: relative;
}
.main-nav__item:after {
  -webkit-transition: all 0.15s ease-in-out;
  transition: all 0.15s ease-in-out;
  content: "";
  display: block;
  float: left;
  background-color: #f5f5f5;
  width: 2px;
  height: 60px;
  -webkit-transform: rotate(20deg);
  transform: skew(155deg);
  position: absolute;
  right: 0;
  bottom: 6px;
}
.main-nav__item:last-child::after {
  content: "";
  width: 0px;
}
.main-nav__item:hover::after {
  width: calc(100% + 1px);
}
.main-nav__item:hover .main-nav__link {
  color: #444;
  position: relative;
  z-index: 3;
}
.main-nav__link {
  list-style: none;
  font-size: 1rem;
  color: white;
  font-family: "proxima-nova-n6", "proxima-nova";
  font-style: normal;
  font-weight: 600;
}
.main-nav__bars {
  display: none;
}

.main-banner {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
}
.main-banner h2 {
  font-size: 3.75rem;
  line-height: 5.375rem;
  margin-bottom: 1.5rem;
  color: #fff;
  font-family: "proxima-nova-n4", "proxima-nova";
  font-style: normal;
  font-weight: 400;
  margin-top: 70px;
  font-weight: normal;
}
.main-banner p {
  font-size: 1.25rem;
  font-weight: normal;
  color: white;
  text-align: center;
  max-width: 700px;
  margin-bottom: 3.375rem;
  margin-right: auto;
  margin-left: auto;
}
.main-banner__green-button {
  height: 3.4375rem;
  background-color: #00964e;
  text-decoration: none;
  color: #fff;
  padding: 11px 13px;
  display: inline-block;
  box-sizing: border-box;
  line-height: 27px;
  border-radius: 30px;
  border: 3px solid #00964e;
  font-size: 1.125rem;
  padding-right: 50px;
  font-family: "proxima-nova-n6", "proxima-nova";
  font-style: normal;
  font-weight: 600;
  position: relative;
  -webkit-transition: all 0.15s ease-in-out;
  transition: all 0.15s ease-in-out;
}
.main-banner__arrow-right::after {
  font-family: "icomoon";
  font-style: normal;
  font-weight: normal;
  font-variant: normal;
  text-transform: none;
  content: "" !important;
  font-size: 33px;
}
.main-banner__green-button::after {
  float: right;
  content: "";
  display: block;
  width: 34px;
  height: 34px;
  margin-left: 10px;
  top: 10px;
  position: absolute;
  right: 10px;
}
.main-banner__green-button:hover {
  background-color: white;
  color: green;
}
.main-banner__arrow-down {
  transform: rotate(90deg);
  width: 34px;
  height: 34px;
  position: absolute;
  bottom: 176px;
  margin-left: -17px;
  left: 50%;
  z-index: 2;
  -webkit-transition: all 0.15s ease-in-out;
  transition: all 0.15s ease-in-out;
  background-image: url("/assets/images/icon-arrow-right.png");
}

#main-sticky {
  background-color: white;
}

@media only screen and (max-width: 768px) {
  .main-nav {
    margin: 0;
    display: block;
    position: inherit;
    height: auto;
    overflow: auto;
    background: white;
  }

  .main-nav__list {
    margin-top: 20px;
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }

  .main-nav__item:after {
    content: "";
    width: 1px;
  }

  .Navbar__ToggleShow {
    display: flex;
  }

  .main-nav__logo {
    position: relative;
    bottom: 49px;
    background-repeat: no-repeat;
    background-image: url("/assets/images/logo-green.png");
  }

  .man-nav__logo-green {
    visibility: hidden !important;
  }

  .main-nav__bars {
    display: flex;
    justify-content: flex-end;
    cursor: pointer;
    padding: 26px;
    font-size: 50px;
    color: #00964e;
    /* position: absolute; */
    top: 0;
    /* bottom: 36px; */
    /* right: 28px; */
    margin-top: -196px;
  }

  .main-nav__link {
    color: #444;
  }

  .main-banner__arrow-down {
    display: block !important;
    position: initial;
    margin: 0 auto;
    margin-top: 50px;
  }
  .main-banner h2 {
    text-align: center;
  }
}

Here is a link to the live demo

Here is GitHub repo if interested

我的代码有什么问题?请帮助,任何建议将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:1)

要调试的东西很多,这就是我发现的东西:

导航栏(.main-nav):

  • 首先,您获得了一个脚本(index.js),该脚本在滚动时将main-navmain-nav__barsmain-nav__list的位置设置为fixed。您无需为所有三个组件都这样做。只需设置main-nav并让孩子继承即可。

  • 接下来,最好将height: auto更改为min-height: 125px(足以容纳徽标)

  • 然后,您需要设置导航.main-nav overflow-x: hidden以消除发生溢出错误时右侧的滚动条。

    • 或者更好的是,使徽标本身及其容器(.main-nav__logo)具有与(height: 125px)相匹配的固定高度,并让徽标height: inherit。 (您需要裁剪图像才能使它看起来不错。请参阅下一节的原因)
  • 您还将使用媒体查询将display: flex覆盖到block。删除它,以便您可以使用flex属性。之后,您可以执行以下操作:(附加到您的移动媒体查询中)

.main-nav {
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: flex-start;
  align-content: stretch;
  align-items: stretch;
}

.main-nav__logo {
  flex: 2 1 60%;
}

.main-nav__toggle {
  flex: 2 1 30%;
}

.main-nav__list {
  flex: 0 1 100%;
}

这应该给您这样的结果:

|-----------------------|----------|
|        Logo           |   Bars   |
|-----------------------|----------|
|                                  |
|             Nav ul               |
|                                  |
|----------------------------------|

徽标(.man-nav__logo-green):

  • 接下来,您的徽标是200x200,具有很多透明边距,请使用Photoshop或其他将固定边距元素化的方式来修复徽标。这将使您的徽标适合您,而不会溢出导航栏。 (并且在边距样式方面更加灵活)
    • 一个肮脏的解决方案是将徽标的容器更改为(.main-nav__logoheight: 160px,并添加一个margin-top: -35px

希望这会使您的生活更轻松。问候。