首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >响应式移动导航css

问响应式移动导航css
EN

Stack Overflow用户
提问于 2021-01-13 11:42:14
回答 1查看 37关注 0票数 0

有人能帮我修复我的响应式导航吗?我需要什么样的CSS才能在移动模式下使导航向下滚动。此外,动画使导航很难点击,并且当我调整页面大小时,导航不会返回到正确的位置。请帮帮我。

https://codepen.io/patriciaworth/pen/OJRBEbX

HTML

代码语言:javascript
复制
<html>
<head>
    <link rel="stylesheet" href="sub-nav.css">
    <!--Jquery-->
    <script src="js/jquery-3.4.1.min.js"></script>
</head>
<body>
<div class="nav-container">
    <nav>
        <div class="container">
            <ul class="menu">
                <li><a href="index.php">Home</a></li>
                <li><a href="about.php">About</a></li>
                <li><a href="hosts.php">Hosts</a></li>
                <li><a href="membership.php">Membership</a></li>
                <li><a href="affiliations.php">Affiliations</a></li>
                <!--dropdown-->
                <li class="dropdown"><span class="sub-link">Shop</span>
                    <ul>
                        <li><a href="">Products</a></li>
                        <li><a href="">My Account</a></li>
                        <li><a href="">Cart</a></li>
                        <li><a href="">Checkout</a></li>
                    </ul>
                </li>
                <li><a href="contact.php">Contact Us</a></li>
            </ul>
            <div class="mobile-toggle">&#9776;</div>
        </div>
    </nav>
    <div class="nav-underline"></div>
</div>
<!--mobile nav toggle-->
<script>
    $(document).ready(function(){
        //mobile nav click event
        $(".mobile-toggle").click(function(){
            $(".menu").slideToggle();
        });
        $(".dropdown").click(function(){
            $(".dropdown ul").slideToggle();
        });
    });
</script>
</body>
</html>

CSS

代码语言:javascript
复制
@import url("https://fonts.googleapis.com/css2?family=Dosis:wght@200;300;400;500;600;700;800&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Titillium+Web:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Sansita+Swashed:wght@300;400;500;600;700;800;900&display=swap");
* {
  margin: 0;
  padding: 0;
  text-decoration: none; }

.container {
  min-width: 360px;
  max-width: 1080px; }

.nav-container {
  width: 100%;
  background: #183153;
  background-image: linear-gradient(to right, #183153, #2e5d9e, #2e5d9e, #6190d1);
  position: relative;
  z-index: 10; }

.nav-underline {
  height: 4px;
  background-image: linear-gradient(to right, #ffcb05, #ffcb05, #ffe066);
  animation-name: underlineToRight;
  animation-duration: 4s;
  animation-timing-function: ease-in;
  position: relative;
  z-index: 11; }

@keyframes underlineToRight {
  from {
    width: 0; }
  to {
    width: 100%; } }
nav ul {
  font-family: "Dosis", sans-serif;
  font-size: 16px;
  line-height: 25px;
  margin: 0;
  padding: 0;
  list-style: none;
  position: relative; }
  @media only screen and (max-width: 1024px) {
    nav ul {
      position: absolute;
      top: 74px;
      left: 0px;
      background: #2e5d9e;
      width: 100%;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      display: none; } }
nav ul > li {
  display: inline-block; }
  @media only screen and (max-width: 1024px) {
    nav ul > li {
      display: block;
      width: 100%;
      text-align: center; } }
nav ul > li > ul {
  display: none;
  position: absolute;
  width: 160px;
  background: #2e5d9e; }
  @media only screen and (max-width: 1024px) {
    nav ul > li > ul {
      width: 100%;
      position: relative; } }
nav ul > li > ul > li {
  width: 100%; }
nav ul > li:hover ul {
  display: block; }
  @media only screen and (max-width: 1024px) {
    nav ul > li:hover ul {
      display: none;
      width: 100%; } }
nav ul li a,
nav .sub-link {
  color: #ffffff;
  display: block;
  text-decoration: none;
  padding: 15px;
  box-sizing: border-box;
  text-transform: uppercase;
  transition: 0.3s;
  cursor: pointer; }
  nav ul li a:hover,
  nav .sub-link:hover {
    background: #c6c6cc;
    color: #183153; }
  @media only screen and (max-width: 1366px) {
    nav ul li a,
    nav .sub-link {
      font-size: 14px; } }
@media only screen and (max-width: 1024px) {
  nav ul > li {
    display: block;
    width: 100%;
    border-bottom: 1px solid rgba(204, 204, 204, 0.3); } }
nav .mobile-toggle {
  color: #fff;
  padding: 25px 0px;
  text-align: right;
  right: 20px;
  display: none;
  cursor: pointer;
  position: relative; }
  @media only screen and (max-width: 1024px) {
    nav .mobile-toggle {
      display: block;
      width: 100%; } }
EN

回答 1

Stack Overflow用户

发布于 2021-01-14 15:10:17

好的,经过深思熟虑,我找到了一个答案。请看这里:

https://codepen.io/patriciaworth/pen/rNMQyVx

HTML

代码语言:javascript
复制
<html>
<head>
    <link rel="stylesheet" href="sub-nav.css">
    <!--Jquery-->
    <script src="js/jquery-3.4.1.min.js"></script>
</head>
<body>
<div class="nav-container">
    <nav>
        <div class="container">
            <ul class="menu">
                <li><a href="">Home</a></li>
                <li><a href="">About</a></li>
                <li><a href="">Hosts</a></li>
                <li><a href="">Membership</a></li>
                <li><a href="">Affiliations</a></li>
                <!--dropdown-->
                <li class="dropdown"><span class="sub-link">Shop</span>
                    <ul>
                        <li><a href="">Products</a></li>
                        <li><a href="">My Account</a></li>
                        <li><a href="">Cart</a></li>
                        <li><a href="">Checkout</a></li>
                    </ul>
                </li>
                <li><a href="contact.php">Contact Us</a></li>
            </ul>
            <div class="mobile-toggle">&#9776;</div>
        </div>
    </nav>
    <div class="nav-underline"></div>
</div>
<!--mobile nav toggle-->
<script>
    $(document).ready(function(){
        //mobile nav click event
        $(".mobile-toggle").click(function(){
            $(".menu").slideToggle();
        });
        
        $(".dropdown").click(function(){
            $(".dropdown ul").slideToggle();
        });

        //window resize event
        $(window).resize(function(){
            var w = $(window).width();
            if(w > 1024 && $('.menu').is(':hidden'))
            {  
                $('.menu').removeAttr('style');  
            }  
        });  
        
    });
</script>
</body>
</html>

SCSS

代码语言:javascript
复制
//breakpoints
$mega: 1920px;
$xlarge: 1680px;
$large: 1366px;
$med: 1024px;
$tiny: 768px;

//colors
$blue: #183153;
$blue2: #2e5d9e;
$blue3: #6190d1;
$light-blue: #336699;
$yellow: #ffcb05;
$yellow2: #ffe066;
$light-grey: #90909c;
$med-grey: #434343;
$dark-grey: #363636;
$white: #ffffff;

//fonts
@import url('https://fonts.googleapis.com/css2?family=Dosis:wght@200;300;400;500;600;700;800&display=swap');
$heading-font: 'Dosis', sans-serif;
@import url('https://fonts.googleapis.com/css2?family=Titillium+Web:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700&display=swap');
$page-font: 'Titillium Web', sans-serif;
@import url('https://fonts.googleapis.com/css2?family=Sansita+Swashed:wght@300;400;500;600;700;800;900&display=swap');
$cursive: 'Sansita Swashed', cursive;

*
{
    margin:0;
    padding: 0;
    text-decoration: none;
}
.container
{
    min-width: 360px;
    max-width: 1080px;
}
.nav-container
{
    width: 100%;
    background: $blue;
    background-image: linear-gradient(to right, $blue, $blue2, $blue2, $blue3);
    position: relative;
    z-index: 10;
}
//animation
.nav-underline
{
    height: 4px;
    //background: $yellow;
    background-image: linear-gradient(to right, $yellow, $yellow, $yellow2);
    animation-name: underlineToRight;
    animation-duration: 4s;
    animation-timing-function: ease-in;
    position: relative;
    z-index: 11;
}
@keyframes underlineToRight
{
    from 
    {
        width: 0;
    }
    to 
    {
        width: 100%;
    }
}
nav
{
    //top ul
    .menu
    {
        font-family: $heading-font;
        font-size: 16px;
        line-height: 25px;
        margin: 0;
        padding: 0;
        list-style: none;
        position: relative;

        @media only screen and (max-width: $med)
        {
            position: absolute;
            top: 74px;
            left: 0px;
            background: $blue2;
            width: 100%;
            -webkit-box-sizing: border-box;
               box-sizing: border-box;
            display: none;
        }
    }
    //nav items
    ul>li
    {
        display: inline-block;
        position: relative;
        @media only screen and (max-width: $med)
        {
            display: block;
            width: 100%;
            text-align: center;
        }  
    }
    //dropdown
    ul>li>ul
    {
        display: none;
        position: absolute;
        width: 160px;
        background: #333;
        @media only screen and (max-width: $med) 
        {
            width: 100%;
            position: relative;
        }
    }
    //dropdown items
    ul>li>ul>li
    {
        width: 100%;
    }

    //top nav links
    a,
    .sub-link
    {
        display: block;
        text-decoration: none;
        padding: 15px;
        box-sizing: border-box;
        text-transform: uppercase;
        transition: 0.3s;
        cursor: pointer;

        @media only screen and (max-width: $large) 
        {
            font-size: 14px;
        }
    }
    //top nav link color
    .menu>li>a,
    .sub-link
    {
        color: $white;
        &:hover 
        {
            background: lighten($light-grey, 20%);
            color: $blue;
        }
    }
    //show arrow icon so user knows to click for dropdown
    .sub-link:after
    {
        content:" \25BC";
    }
    //dropdown link color
    .dropdown>ul>li>a
    {
        color: #fff;
        &:hover 
        {
            background: #ccc;
            color: $blue2;
        }
    }
    //mobile item display
    ul>li
    {
        @media only screen and (max-width: $med) 
        {
            display: block;
            width: 100%;
            border-bottom: 1px solid rgba(204, 204, 204, 0.3);
        }
    }
    //mobile icon
    .mobile-toggle
    {
        color: #fff;
        padding: 25px 0px;
        text-align: right;
        right: 20px;
        display: none;
        cursor: pointer;
        position: relative;

        @media only screen and (max-width: $med)
        {
            display: block;
            width: 100%;
        }
    }
    
}

CSS

代码语言:javascript
复制
@import url("https://fonts.googleapis.com/css2?family=Dosis:wght@200;300;400;500;600;700;800&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Titillium+Web:ital,wght@0,200;0,300;0,400;0,600;0,700;0,900;1,200;1,300;1,400;1,600;1,700&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Sansita+Swashed:wght@300;400;500;600;700;800;900&display=swap");
* {
  margin: 0;
  padding: 0;
  text-decoration: none; }

.container {
  min-width: 360px;
  max-width: 1080px; }

.nav-container {
  width: 100%;
  background: #183153;
  background-image: linear-gradient(to right, #183153, #2e5d9e, #2e5d9e, #6190d1);
  position: relative;
  z-index: 10; }

.nav-underline {
  height: 4px;
  background-image: linear-gradient(to right, #ffcb05, #ffcb05, #ffe066);
  animation-name: underlineToRight;
  animation-duration: 4s;
  animation-timing-function: ease-in;
  position: relative;
  z-index: 11; }

@keyframes underlineToRight {
  from {
    width: 0; }
  to {
    width: 100%; } }
nav .menu {
  font-family: "Dosis", sans-serif;
  font-size: 16px;
  line-height: 25px;
  margin: 0;
  padding: 0;
  list-style: none;
  position: relative; }
  @media only screen and (max-width: 1024px) {
    nav .menu {
      position: absolute;
      top: 74px;
      left: 0px;
      background: #2e5d9e;
      width: 100%;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      display: none; } }
nav ul > li {
  display: inline-block;
  position: relative; }
  @media only screen and (max-width: 1024px) {
    nav ul > li {
      display: block;
      width: 100%;
      text-align: center; } }
nav ul > li > ul {
  display: none;
  position: absolute;
  width: 160px;
  background: #333; }
  @media only screen and (max-width: 1024px) {
    nav ul > li > ul {
      width: 100%;
      position: relative; } }
nav ul > li > ul > li {
  width: 100%; }
nav a,
nav .sub-link {
  display: block;
  text-decoration: none;
  padding: 15px;
  box-sizing: border-box;
  text-transform: uppercase;
  transition: 0.3s;
  cursor: pointer; }
  @media only screen and (max-width: 1366px) {
    nav a,
    nav .sub-link {
      font-size: 14px; } }
nav .menu > li > a,
nav .sub-link {
  color: #ffffff; }
  nav .menu > li > a:hover,
  nav .sub-link:hover {
    background: #c6c6cc;
    color: #183153; }
nav .sub-link:after {
  content: " \25BC"; }
nav .dropdown > ul > li > a {
  color: #fff; }
  nav .dropdown > ul > li > a:hover {
    background: #ccc;
    color: #2e5d9e; }
@media only screen and (max-width: 1024px) {
  nav ul > li {
    display: block;
    width: 100%;
    border-bottom: 1px solid rgba(204, 204, 204, 0.3); } }
nav .mobile-toggle {
  color: #fff;
  padding: 25px 0px;
  text-align: right;
  right: 20px;
  display: none;
  cursor: pointer;
  position: relative; }
  @media only screen and (max-width: 1024px) {
    nav .mobile-toggle {
      display: block;
      width: 100%; } }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65695378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档