响应式导航溢出

时间:2018-05-27 03:21:37

标签: javascript html css navigation

我很难弄清楚当锚点溢出较小的移动设备的屏幕尺寸时如何使我的导航列表项滚动。我尝试过使用overflow-y:auto(和scroll)来解决这个问题没有运气。我试图改变我的代码以添加固定的高度和溢出属性。有什么建议吗?

.topnav {
  overflow: hidden;
  background-color: #f4f4f4;
  position: fixed;
  top: 0;    
}

.topnav a {
  float: left;
  display: block;
  color: #212414;
  text-align: center;
  padding: 20px;
  text-decoration: none;
  font-size: 16px;
}

.active {
  background-color: #DAD38B;
  color: #ffffff;
}

.topnav .icon {
  display: none;
}

.dropdown {
    float: right;
    overflow: hidden;
}

.dropdown .dropbtn {
    font-size: 16px;    
    border: none;
    outline: none;
    color: white;
    padding: 14px 16px;
    background-color: inherit;
    font-family: inherit;
    margin: 0;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f4f4f4;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    float: none;
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #C2B78F;
  color: white;
}

.dropdown-content a:hover {
    background-color: #ddd;
    color: black;
}

.dropdown:hover .dropdown-content {
    display: block;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: left;
    display: block;
  }
  .topnav.responsive {position: fixed;}
  .topnav.responsive .icon {
    position: relative;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: center;
    padding: 20px 40px;  
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: fixed;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}

@media screen and (min-width: 600px) {
  .topnav {
  overflow: hidden;
  background-color: #f4f4f4;
  position: relative; 
  }
    
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: center;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}
    <!-- Navigation -->
    <nav class="topnav" id="myTopnav">
            <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>    
            <a href="index.php" class="active">Home</a>
            <a href="#services">Services</a>
            <a href="#my-work">My Work</a>
            <a href="#about-me">About Me</a>
            <a href="#contact">Contact</a>

        <script>
            function myFunction() {
            var x = document.getElementById("myTopnav");
            if (x.className === "topnav") {
            x.className += " responsive";
            } else {
            x.className = "topnav";
            }
            } 
        </script>     
    </nav>

Nav Overflow问题图片如下:

enter image description here

请帮我解决这个问题。

2 个答案:

答案 0 :(得分:0)

溢出:滚动css样式只在包含元素不宽或不足以显示其中的所有信息时放置滚动条。这意味着如果div只能是200px高,但内部的内容需要300px高度来显示,那么会有一个滚动条。

这不适用于你的.topnav元素,因为它足够高以显示内容,因为它伸展到屏幕的高度,这意味着溢出滚动样式未激活,因为div在技术上足够高显示所有内容,即使你看不到它。如果你阻止元素拉伸通过可见屏幕并阻止它更高,那么屏幕的高度任何不适合的内容都会使样式创建滚动条。

下面我的样式中的max-height属性确保您的固定.topnav元素不会高于屏幕大小,这会使任何不适合的内容通过滚动显示。

.topnav {
    overflow-y: scroll;
    max-height: 100vh;
    background-color: #f4f4f4;
    position: fixed;
    top: 0;
}

答案 1 :(得分:0)

overflow-y: scroll;也会将滚动条样式添加到拼贴菜单按钮。您需要使用oberflow-y: auto;以及max-height: 100vh;添加展开菜单的滚动条。我在以下代码段中添加了其他菜单项以进行演示。

.topnav {
  overflow-y: auto;
  background-color: #f4f4f4;
  position: fixed;
  top: 0;
  max-height: 100vh;
}

.topnav a {
  float: left;
  display: block;
  color: #212414;
  text-align: center;
  padding: 20px;
  text-decoration: none;
  font-size: 16px;
}

.active {
  background-color: #DAD38B;
  color: #ffffff;
}

.topnav .icon {
  display: none;
}

.dropdown {
    float: right;
    overflow: hidden;
}

.dropdown .dropbtn {
    font-size: 16px;    
    border: none;
    outline: none;
    color: white;
    padding: 14px 16px;
    background-color: inherit;
    font-family: inherit;
    margin: 0;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f4f4f4;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

.dropdown-content a {
    float: none;
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #C2B78F;
  color: white;
}

.dropdown-content a:hover {
    background-color: #ddd;
    color: black;
}

.dropdown:hover .dropdown-content {
    display: block;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: left;
    display: block;
  }
  .topnav.responsive {position: fixed;}
  .topnav.responsive .icon {
    position: relative;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: center;
    padding: 20px 40px;  
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: fixed;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}

@media screen and (min-width: 600px) {
  .topnav {
  overflow: hidden;
  background-color: #f4f4f4;
  position: relative; 
  }
    
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: center;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}
<!-- Navigation -->
    <nav class="topnav" id="myTopnav">
            <a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>    
            <a href="index.php" class="active">Home</a>
            <a href="#services">Services</a>
            <a href="#my-work">My Work</a>
            <a href="#about-me">About Me</a>
            <a href="#contact">Contact</a>
            <a href="#contact">Contact1</a>

        <script>
            function myFunction() {
            var x = document.getElementById("myTopnav");
            if (x.className === "topnav") {
            x.className += " responsive";
            } else {
            x.className = "topnav";
            }
            } 
        </script>     
    </nav>