导致导航无法正确显示的保证金

时间:2017-09-04 12:54:19

标签: html css

我有一个简单的粘性导航栏,它包含在一个div中,每边有40px的边距。固定导航栏的宽度为100%,因此它应占用其父包装器div内的所有空间。在左侧,它似乎考虑了父边距,但在右侧,它似乎溢出。我试过隐藏在父级上的溢出但它似乎没有做到这一点。我知道我在这里错过了一些非常简单的东西,但我不能把手指放在上面。

$(window).scroll(function() {
  if ($(window).width() > 768) {
    if ($(document).scrollTop() > 100) {
      $('header').addClass('nav-border');
    } else {
      $('header').removeClass('nav-border');
    }
  }
});

$('.burger-icon').click(function() {
  $(this).toggleClass('burger-active');
  $('.main-nav').fadeToggle();
  $('body').toggleClass('nav-scroll-lock');
});
.test-div {
  height: 1800px;
  width: 100%;
  background: black;
}

.main-wrapper {
  margin: 0 40px;
}

.container {
  max-width: 800px;
  padding-right: 15px;
  padding-left: 15px;
  margin-right: auto;
  margin-left: auto
}

.row {
  margin-left: -15px;
  margin-right: -15px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}

.col {
  -webkit-box-flex: 1;
  -ms-flex: 1 1 0px;
  flex: 1 1 0;
}

header {
  background-color: #fff;
  position: fixed;
  width: 100%;
  z-index: 99;
  -webkit-transition: all .3s;
  transition: all .3s;
  font-family: sans-serif;
}

.navbar {
  height: 60px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}

.logo {
  position: relative;
  z-index: 1000;
  background: #151515;
  padding: 8px;
}

.logo a {
  font-weight: 700;
  color: #fff;
  font-size: 18px;
  letter-spacing: 1px;
}

.burger-icon {
  position: relative;
  z-index: 1000 !important;
  -ms-flex-item-align: end;
}

.burger-icon .burger-line {
  width: 40px;
  height: 3px;
  background-color: #282828;
  display: block;
  margin: 5px auto;
  -webkit-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

.burger-icon.burger-active .burger-line:nth-child(2) {
  opacity: 0;
}

.burger-icon.burger-active .burger-line:nth-child(1) {
  -webkit-transform: translateY(8px) rotate(45deg);
  transform: translateY(8px) rotate(45deg);
}

.burger-icon.burger-active .burger-line:nth-child(3) {
  -webkit-transform: translateY(-8px) rotate(-45deg);
  transform: translateY(-8px) rotate(-45deg);
}

.main-nav {
  position: fixed;
  background-color: rgba(255, 255, 255, 0.92);
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
  display: none;
}

.nav-items {
  display: block;
  position: relative;
  text-align: center;
  width: 100%;
  top: 50%;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
}

.nav-items li {
  padding: 25px 0;
}

.nav-items li a {
  font-size: 35px;
  font-weight: 700;
  color: #282828;
  text-transform: uppercase;
  -webkit-transition: all .3s;
  transition: all .3s;
}

.nav-border {
  .border-bottom: 1px solid rgba(75, 78, 83, 0.2);
  -webkit-box-shadow: 0 1px 10px #999;
  box-shadow: 0 1px 10px #999;
}

.show-nav {
  -webkit-transition: all .2s ease-in;
  transition: all .2s ease-in;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main-wrapper">
  <header>
    <div class="container">
      <div class="row">
        <div class="col navbar">
          <div class="logo">
            <a href="index.php">Logo</a>
          </div>
          <div class="burger-icon">
            <span class="burger-line"></span>
            <span class="burger-line"></span>
            <span class="burger-line"></span>
          </div>
        </div>
      </div>
      <nav class="main-nav">
        <ul class="nav-items">
          <li class="wow fadeIn" data-wow-delay=".2s"><a href="index.php" class="active">One</a></li>
          <li class="wow fadeIn" data-wow-delay=".4s">
            <a href="about.php">Two</a>
          </li>
          <li class="wow fadeIn" data-wow-delay=".6s">
            <a href="projects.php">Three</a>
          </li>
          <li class="wow fadeIn" data-wow-delay=".8s">
            <a href="contact.php">Four</a>
          </li>
        </ul>
      </nav>
    </div>
  </header>
  <div class="container">
    <div class="row">
      <div class="test-div">

      </div>
    </div>
  </div>
</div>

3 个答案:

答案 0 :(得分:1)

一种可能的解决方案是指定一个左右值,使元素保持两侧的边缘40px并移除宽度。

header {
  background-color: #fff;
  position: fixed;
  left:40px;
  right:40px;
  z-index: 99;
  -webkit-transition: all .3s;
  transition: all .3s;
  font-family: sans-serif;
}

答案 1 :(得分:1)

标题重叠,因为position fixed is always relative to the Document。这意味着默认情况下它将占用文档的宽度... 我建议看这个例子来解决它 Fixed position but relative to container

答案 2 :(得分:1)

width的{​​{1}}更改为header

width: calc(100% - 80px);是双方的边缘。

&#13;
&#13;
80px
&#13;
$(window).scroll(function() {
  if ($(window).width() > 768) {
    if ($(document).scrollTop() > 100) {
      $('header').addClass('nav-border');
    } else {
      $('header').removeClass('nav-border');
    }
  }
});

$('.burger-icon').click(function() {
  $(this).toggleClass('burger-active');
  $('.main-nav').fadeToggle();
  $('body').toggleClass('nav-scroll-lock');
});
&#13;
body{
  margin:0
}
.test-div {
  height: 1800px;
  width: 100%;
  background: black;
}

.main-wrapper {
  margin: 0 40px;
}

.container {
  max-width: 800px;
  padding-right: 15px;
  padding-left: 15px;
  margin-right: auto;
  margin-left: auto
}

.row {
  margin-left: -15px;
  margin-right: -15px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}

.col {
  -webkit-box-flex: 1;
  -ms-flex: 1 1 0px;
  flex: 1 1 0;
}

header {
  background-color: #fff;
  position: fixed;
  width: calc(100% - 80px);
  z-index: 99;
  -webkit-transition: all .3s;
  transition: all .3s;
  font-family: sans-serif;

}

.navbar {
  height: 60px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}

.logo {
  position: relative;
  z-index: 1000;
  background: #151515;
  padding: 8px;
}

.logo a {
  font-weight: 700;
  color: #fff;
  font-size: 18px;
  letter-spacing: 1px;
}

.burger-icon {
  position: relative;
  z-index: 1000 !important;
  -ms-flex-item-align: end;
}

.burger-icon .burger-line {
  width: 40px;
  height: 3px;
  background-color: #282828;
  display: block;
  margin: 5px auto;
  -webkit-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

.burger-icon.burger-active .burger-line:nth-child(2) {
  opacity: 0;
}

.burger-icon.burger-active .burger-line:nth-child(1) {
  -webkit-transform: translateY(8px) rotate(45deg);
  transform: translateY(8px) rotate(45deg);
}

.burger-icon.burger-active .burger-line:nth-child(3) {
  -webkit-transform: translateY(-8px) rotate(-45deg);
  transform: translateY(-8px) rotate(-45deg);
}

.main-nav {
  position: fixed;
  background-color: rgba(255, 255, 255, 0.92);
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
  display: none;
}

.nav-items {
  display: block;
  position: relative;
  text-align: center;
  width: 100%;
  top: 50%;
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
}

.nav-items li {
  padding: 25px 0;
}

.nav-items li a {
  font-size: 35px;
  font-weight: 700;
  color: #282828;
  text-transform: uppercase;
  -webkit-transition: all .3s;
  transition: all .3s;
}

.nav-border {
  .border-bottom: 1px solid rgba(75, 78, 83, 0.2);
  -webkit-box-shadow: 0 1px 10px #999;
  box-shadow: 0 1px 10px #999;
}

.show-nav {
  -webkit-transition: all .2s ease-in;
  transition: all .2s ease-in;
}
&#13;
&#13;
&#13;