为什么我的导航栏不能像人像那样横向滚动?

时间:2019-05-03 06:31:09

标签: html css

我有一个带有一堆示例文本的导航栏(仅用于测试),当我将其置于纵向模式而不是横向模式时,它将滚动。

我已经使用overflow: scroll来进行肖像拍摄,并且可以使用,但是我使用了单独的风景代码,它具有相同的功能,但是不会滚动。

这是我的代码,能够复制该问题。

我的HTML:

$(document).ready(function() {
  $('.mobile-button').on('click touchend', function(event) {
    event.stopPropagation();
    $('.mobile-sidenavbar').css({
      'width': 100 + '%',
    });
  });
  $('.closebtn').on('click touchend', function() {
    $('.mobile-sidenavbar').css({
      'width': 0 + '%',
    });
  });
});
  body,
html {
  margin: auto 0;
  height: 100%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

nav {
  background-color: #222;
  height: 42px;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1;
}

li {
  list-style: none;
  display: inline-block;
  float: left;
  margin-left: 20px;
  margin-top: -6px;
}

li a {
  text-decoration: none;
  color: white;
  font-weight: bold;
  font-size: 20px;
}

li a img {
  height: 45px;
  margin-top: -12px;
  z-index: -1;
  margin-left: -45px;
}

.nav-first {
  margin-left: 5px;
}

.mobile-button,
.mobile-navbar-text,
.closebtn,
.media-links {
  display: none;
  cursor: pointer;
}

.slideshow {
  width: 100%;
  height: 75%;
  position: absolute;
  top: 42px;
}

.slideshow img {
  object-fit: cover;
  width: 100%;
  height: 100%;
}

.dot {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  display: inline-flex;
  cursor: pointer;
  background-color: lightgrey;
  transition: background-color 0.3s ease;
}

.dot:hover {
  background-color: grey;
}

.dots {
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
}


/* Mobile */

@media (max-width: 599px) {
  nav {
    height: 64px;
    position: absolute;
  }
  li a img {
    transform: scale(1.5);
    margin-top: 0px;
    -webkit-tap-highlight-color: transparent;
  }
  .navbar-text {
    display: none;
  }
  .mobile-navbar-text {
    padding: 8px 8px 8px 8px;
    text-decoration: none;
    font-size: 25px;
    color: white;
    display: block;
    text-align: center;
    white-space: nowrap;
    border-bottom: 1px solid white;
  }
  .mobile-button {
    text-align: center;
    color: white;
    border: none;
    display: block;
    float: right;
    height: 64px;
    width: 62px;
    margin-top: -16px;
    line-height: 65px;
    font-size: 30px;
    font-weight: bold;
    -webkit-tap-highlight-color: transparent;
  }
  .mobile-sidenavbar {
    background-color: #222;
    position: fixed;
    right: 0;
    top: 0;
    width: 0px;
    height: 100%;
    z-index: 2;
    transition: 0.4s;
    overflow: scroll;
  }
  .instagram,
  .facebook,
  .twitter {
    color: white;
    padding: 8px 8px 8px 8px;
    text-decoration: none;
    white-space: nowrap;
  }
  .closebtn {
    text-decoration: none;
    font-size: 50px;
    color: white;
    font-weight: bold;
    margin-left: 20px;
    display: inline-block;
  }
  .media-links {
    display: inline-flex;
    justify-content: center;
    font-size: 38px;
    width: 101vw;
    position: absolute;
    bottom: 0;
  }
  .slideshow {
    top: 64px;
  }
  .slideshow img {
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
  }
}


/* Mobile Landscape to Fix Media Link Issue */

@media (orientation: landscape) {
  .media-links {
    position: absolute;
    top: 150%;
    overflow: scroll;
  }
  /* Tablet */
  @media (min-width: 600px) and (max-width: 1023px) {
    nav {
      height: 64px;
      position: absolute;
    }
    li a img {
      transform: scale(2);
      margin-top: 0px;
      margin-left: -32px;
      -webkit-tap-highlight-color: transparent;
    }
    .navbar-text {
      display: none;
    }
    .mobile-navbar-text {
      padding: 8px 8px 8px 32px;
      text-decoration: none;
      font-size: 30px;
      color: white;
      display: block;
      text-align: center;
      white-space: nowrap;
      border-bottom: 1px solid white;
    }
    .mobile-button {
      text-align: center;
      color: white;
      background-repeat: no-repeat;
      border: none;
      display: block;
      float: right;
      font-size: 40px;
      font-weight: bold;
      margin-top: -13px;
      margin-right: 15px;
      -webkit-tap-highlight-color: transparent;
    }
    .mobile-sidenavbar {
      background-color: #222;
      position: fixed;
      right: 0;
      top: 0;
      width: 0px;
      height: 100%;
      z-index: 2;
      transition: 0.4s;
    }
    .instagram,
    .facebook,
    .twitter {
      color: white;
      padding: 8px 8px 8px 8px;
      text-decoration: none;
      white-space: nowrap;
    }
    .closebtn {
      text-decoration: none;
      font-size: 9vw;
      color: white;
      font-weight: bold;
      margin-left: 30px;
      display: inline-block;
    }
    .media-links {
      display: inline-flex;
      justify-content: center;
      font-size: 38px;
      width: 101vw;
      position: absolute;
      bottom: 0;
      cursor: default;
    }
    .slideshow {
      top: 64px;
    }
    .slideshow img {
      background-size: cover;
      background-repeat: no-repeat;
      background-position: center;
    }
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav>
  <ul>
    <li>
      <a href="#"><img src="https://seeklogo.net/wp-content/uploads/2012/10/american-flag-logo-vector.png"></a>
    </li>
    <li><a href="#" class="nav-first navbar-text">Sample Text</a></li>
    <li><a href="#" class="navbar-text">Sample Text</a></li>
    <li><a href="#" class="navbar-text">Sample Text</a></li>
    <li><a href="#" class="navbar-text">Sample Text</a></li>
    <li><a href="#" class="navbar-text">Sample Text</a></li>
    <span class="mobile-button">&#9776;</span>
  </ul>
</nav>
<div class="mobile-sidenavbar">
  <a href="javascript:void(0)" class="closebtn">&times;</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <a href="#" class="mobile-navbar-text">Sample Text</a>
  <div class="media-links">
    <a href="#" class="fa facebook">&#xf082;</a>
    <a href="#" class="fa twitter">&#xf081;</a>
    <a href="#" class="fa instagram">&#xf16d;</a>
  </div>
</div>

我希望它也能够在横向滚动。截至目前,它以纵向滚动而不是横向滚动,overflow: scroll将不起作用。

4 个答案:

答案 0 :(得分:4)

对于横向媒体查询,您没有为overflow:scroll添加CSS .mobile-sidenavbar。 请尝试在@media (min-width: 600px) and (max-width: 1023px)中使用以下代码段。

  @media (min-width: 600px) and (max-width: 1023px) {
    .mobile-sidenavbar {
      overflow: scroll;
    }
  }

答案 1 :(得分:0)

请仔细检查您的html和css,并遵循此步骤

要在横向模式下滚动,请使用

 overflow: -webkit-paged-x;

要以纵向模式滚动,请使用此

 overflow: -webkit-paged-y;

要同时滚动横向和纵向模式,请使用

 overflow: scroll;

答案 2 :(得分:0)

请在媒体CSS 600像素到1023像素中将图像宽度固定为100%。参见下文:

@media (min-width: 600px) and (max-width: 1023px) {
  li a img {
    transform: scale(2);
    margin-top: 0px;
    margin-left: -32px;
    width : 100%;  //add for responsive and remove horizontal(landscape) scroll
    -webkit-tap-highlight-color: transparent;
  }
}

答案 3 :(得分:0)

就像(简单的)旁注一样,在Bootstrap中,将此CSS添加到导航栏的<ul>

max-height: 85vh;

解决了这个问题。

如果智能手机处于横向模式并且引导导航栏对于屏幕而言过高,它将创建2(或更多)列