【问题标题】:Site Navbar doesn't play animation on smaller screens站点导航栏不在较小的屏幕上播放动画
【发布时间】:2019-08-05 19:52:47
【问题描述】:

我正在按照教程为我的网站制作导航栏。在处理我的移动菜单响应时,我创建了一个.burger 类,其中包含三个条形来表示较小屏幕的菜单图标。我这样做是为了在超过 775px 的屏幕上将其显示属性设置为无。并使用媒体查询进行设置,以便当我的网站加载到较小的屏幕上时,它会显示菜单图标。使用一些 JavaScript,我为单击添加了一个事件侦听器,以显示所有链接并播放流畅的动画。出于某种原因,当用户点击该图标时,网站并没有改变。

代码:https://gist.github.com/VlatkoStojkoski/7619ece485f6a5804642b5dd8f082b9d

const navSlide = () => {
  const burger = document.querySelector('.burger');
  const nav = document.querySelector('#nav');

  burger.addEventListener('click', () => {
    console.log(nav.classList.toggle('nav-active'));
  });
};

navSlide();
@import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro&display=swap');
$text-color: whitesmoke;
$font-stack: 'Source Sans Pro', sans-serif;
$nav-color: rgba(79, 91, 102, 0.8);

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

nav {
  display: flex;
  justify-content: space-around;
  align-items: center;
  min-height: 12vh;
  font-family: $font-stack;
  background-color: $nav-color;
  h1 {
    letter-spacing: 5px;
    font-size: 30px;
    color: $text-color;
  }
  img {
    width: 64px;
  }
  ul {
    display: flex;
    width: 35%;
    justify-content: space-around;
    a {
      font-size: 21px;
      color: $text-color;
      text-decoration: none;
    }
    li {
      list-style: none;
    }
  }
  .burger {
    display: none;
    div {
      width: 30px;
      height: 4px;
      background-color: $text-color;
      margin: 5px;
    }
  }
}

@media (max-width: 1024px) {
  nav ul {
    width: 50%;
  }
}

@media (max-width: 775px) {
  body {
    overflow-x: hidden;
  }
  nav {
    ul {
      position: absolute;
      right: 0px;
      height: 88vh;
      top: 12vh;
      background-color: $nav-color;
      display: flex;
      flex-direction: column;
      align-items: center;
      width: 50%;
      transform: translateX(100%);
      transition: transform 0.5s ease-in;
      li {
        opacity: 0;
      }
    }
    .burger {
      display: block;
      cursor: pointer;
    }
  }
}

.nav-active {
  transform: translateX(0%);
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Home</title>
    <link rel="stylesheet" href="styles/style.css" />
  </head>
  <body>
    <nav id="nav">
      <div class="logo">
        <h1>hello</h1>
      </div>
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
      <div class="burger">
        <div class="line1"></div>
        <div class="line2"></div>
        <div class="line3"></div>
      </div>
    </nav>
    <script src="main.js"></script>
  </body>
</html>

【问题讨论】:

    标签: javascript html css sass dom-events


    【解决方案1】:

    对您的 css 进行以下更改:

    改变这个

    .nav-active {
      transform: translateX(0%);
    }
    

    到下面为包含ul

    .nav-active ul{
          transform: translateX(0%);
     }
    

    const navSlide = () => {
      const burger = document.querySelector('.burger');
      const nav = document.querySelector('#nav');
    
      burger.addEventListener('click', () => {
        console.log(nav.classList.toggle('nav-active'));
      });
    };
    
    navSlide();
    @import url('https://fonts.googleapis.com/css?family=Source+Sans+Pro&display=swap');
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    nav {
      display: flex;
      justify-content: space-around;
      align-items: center;
      min-height: 12vh;
      font-family: 'Source Sans Pro', sans-serif;
      background-color: rgba(79, 91, 102, 0.8);
    }
    
    nav h1 {
      letter-spacing: 5px;
      font-size: 30px;
      color: whitesmoke;
    }
    
    nav img {
      width: 64px;
    }
    
    nav ul {
      display: flex;
      width: 35%;
      justify-content: space-around;
    }
    
    nav ul a {
      font-size: 21px;
      color: whitesmoke;
      text-decoration: none;
    }
    
    nav ul li {
      list-style: none;
    }
    
    nav .burger {
      display: none;
    }
    
    nav .burger div {
      width: 30px;
      height: 4px;
      background-color: whitesmoke;
      margin: 5px;
    }
    
    @media (max-width: 1024px) {
      nav ul {
        width: 50%;
      }
    }
    
    @media (max-width: 775px) {
      body {
        overflow-x: hidden;
      }
      nav ul {
        position: absolute;
        right: 0px;
        height: 88vh;
        top: 12vh;
        background-color: rgba(79, 91, 102, 0.8);
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 50%;
        transform: translateX(100%);
        transition: transform 0.5s ease-in;
      }
      nav ul li {
        opacity: 0;
      }
      nav .burger {
        display: block;
        cursor: pointer;
      }
    }
    
    .nav-active ul {
      transform: translateX(0%);
    }
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Home</title>
      <link rel="stylesheet" href="styles/style.css">
    </head>
    
    <body>
      <nav id="nav">
        <div class="logo">
          <h1>hello</h1>
        </div>
        <ul>
          <li><a href="#">Home</a></li>
          <li><a href="#">About</a></li>
          <li><a href="#">Services</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
        <div class="burger">
          <div class="line1"></div>
          <div class="line2"></div>
          <div class="line3"></div>
        </div>
      </nav>
    
    
    </body>
    
    </html>

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-13
      • 2015-02-28
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多