我如何切换菜单onclick

时间:2018-08-13 10:20:56

标签: javascript html css

我是JS和编码的新手,只是不知道该怎么做?首先是我的代码(我仍然需要清理一下代码:P)

 .header {
 position: relative;
 height: 100vh;
 width: 100vw;
}

.hero-image {
  position: relative;
  background: url("../img/background-image.jpg");
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  width: 100vw;
  height: 100vh;
}

.hero-text {
  text-align: center;
  color: black;
}

.hero-text button {
  border: none;
  outline: 0;
  display: inline-block;
  padding: 10px 25px;
  color: black;
  background-color: #ddd;
  text-align: center;
  cursor: pointer;
}

.hero-text button:hover {
  background-color: #555;
  color: white;
}

/* MENU */
.header__menu {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: #000;
  z-index: -1;
}

.hero-text {
  width: 100vw;
  height: 100vh;
}

.hero-menu_toggle {
  justify-content: center;

}

.hero-menu_scroll {
 justify-content: center;

}

.header__menu.is-expanded {
 z-index: 99;
}

.beige {
 background-color: #ffd39b;
}

.pink {
 background-color: #d0b2e4;
}

.yellow {
  background-color: #FFDE03;
}

.blue {
  background-color: #00B8D4;
} 

.green {
  background-color: #1DE9B6;
}

.black {
 background-color: #414141;
}
  <header class="header">
  <div class="hero-image">
    <div class="hero-text">
      <button onclick="toggleMenu" class="hero-menu_toggle" id="hero-menu-toggle">Menu</button>
      <h1> Berlin From Below </h1>
      <button class="hero-menu_scroll" id="hero-menu-scroll"> see more </button>
    </div>
  </div>
  <div class="header__menu">
    <nav class="nav">
      <ul class="menu-list">
        <li class="pink menu-list_item"> <a href="gallery.html">Gallery</a> </li>
        <li class="yellow menu-list_item"> <a href="people-of-berlin.html">People Of Berlin</a> </li>
        <li class="blue menu-list_item"> <a href="contact.html">Contact</a> </li>
        <li class="green menu-list_item"> <a href="news.html">News</a> </li>
      </ul>
    </nav>
  </div>
</header>

这就是我想做的; 当用户访问该网站时,他看到的只是一张背景图片和两个按钮(“菜单”和“查看更多”)。

  • 如果用户单击菜单按钮,我希望我的菜单弹出并覆盖整个屏幕,理想情况下,过渡效果是颜色逐渐填满屏幕

我希望这是可以理解的。

1 个答案:

答案 0 :(得分:1)

您可以在W3Schools网站here上看到不同菜单样式的示例。

请参阅W3Schools附带的代码段:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
    font-family: "Lato", sans-serif;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.sidenav a:hover {
    color: #f1f1f1;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
</style>
</head>
<body>

<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<h2>Animated Sidenav Example</h2>
<p>Click on the element below to open the side navigation menu.</p>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>

<script>
function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
}
</script>
     
</body>
</html>