网站导航栏无法在较小的屏幕上播放动画

时间:2019-07-31 11:46:18

标签: javascript html css sass dom-events

我正在按照教程制作网站导航栏。在处理移动菜单的响应性时,我制作了一个.burger类,其中包含三个栏来代表较小屏幕的菜单图标。我做了这样的设置,以便在超过775px的屏幕上将其display属性设置为none。并使用媒体查询进行设置,以便在较小的屏幕上加载我的网站时显示菜单图标。使用一些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>

1 个答案:

答案 0 :(得分:0)

对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>