javascript - 如何切换菜单 onclick

标签 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>

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

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

我希望它能理解我想要它做什么。

最佳答案

您可以在 W3School 网站上查看不同菜单样式的示例 here .

请参阅 W3School 的附件片段:

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

关于javascript - 如何切换菜单 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51820084/

相关文章:

javascript - Material Design Lite 导航内页

css - 网格内溢出自动内的粘性表格标题

html - HTML 格式的按钮定位

javascript - Wordpress 中的 SVG 图像映射 - 超链接不起作用

javascript - 访问 jade 模板中的数据(制作静态 html)

html - CSS - 将页脚刷新到页面底部

html - flexbox 中文本对齐的媒体查询

javascript - jquery modal 防止滚动并防止在模式打开时返回页面顶部

javascript - 如何将具有相同属性值的对象值推送到 Javascript 中的同一个子数组?

javascript - 无法将数据多次放入本地存储