当我将屏幕缩小到768px以下时,切换图标菜单不显示。仅显示“网站标题”

问题描述 投票:0回答:1

当我将屏幕缩小到768px以下时,切换图标菜单不显示。我只能看到'网站标题'。有任何想法吗?谢谢。

<!DOCTYPE html>
<html lang="en">

<head>

  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">

  <title>Flexbox Dropdown Menu</title>

  <style>
    .Navbar {
      background-color: #46ACC2;
      display: flex;
      padding: 16px;
      font-family: sans-serif;
      color: white;
    }

    .Navbar__Link {
      padding-right: 8px;
    }

    .Navbar__Items {
      display: flex;
    }

    .Navbar__Items--right {
      margin-left: auto;
    }

    .Navbar__Link-toggle {
      display: none;
    }

    @media only screen and (max-width: 768px) {
      .Navbar__Items,
      .Navbar {
        flex-direction: column;
      }
      .Navbar__Items {
        display: none;
      }
      .Navbar__Items--right {
        margin-left: 0;
      }
      .Navbar__ToggleShow {
        display: flex;
      }
      .Navbar__Link-toggle {
        align-self: flex-end;
        display: initial;
        position: absolute;
        cursor: pointer;
      }
    }
  </style>
</head>
<body>
  <div class="Navbar">
    <div class="Navbar__Link Navbar__Link-brand">
      Website title
    </div>
    <div class="Navbar__Link Navbar__Link-toggle">
      <i class="fas fa-bars"></i>
    </div>
    <nav class="Navbar__Items">
      <div class="Navbar__Link">
        Longer Link
      </div>
      <div class="Navbar__Link">
        Longer Link
      </div>
      <div class="Navbar__Link">
        Link
      </div>
    </nav>
    <nav class="Navbar__Items Navbar__Items--right">
      <div class="Navbar__Link">
        Link
      </div>
      <div class="Navbar__Link">
        Link
      </div>
    </nav>
  </div>


</body>

<script>
  function classToggle() {
    const navs = document.querySelectorAll('.Navbar__Items')

    navs.forEach(nav => nav.classList.toggle('Navbar__ToggleShow'));
  }
  document.querySelector('.Navbar__Link-toggle')
    .addEventListener('click', classToggle);
</script>
css html5
1个回答
1
投票

您还应该在媒体查询中定义.Navbar__Link-toggle,以便像这样显示切换图标

@media only screen and (max-width: 768px) {
 .Navbar__Link-toggle {
    display: block;
  }
}

并将其包含在<head>标签<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

© www.soinside.com 2019 - 2024. All rights reserved.