我试图找出如何让这个侧边栏工作,但当我在一个较小的屏幕上通过汉堡包菜单打开它时,侧边栏会在内容下方打开.我怎样才能解决这个问题?我试着按照教程创建这个侧边栏,但显然,当我try 添加自己的东西时,我搞砸了,我不知道该怎么做.我一直在玩弄CSS中的元素,但结果仍然是一样的.

Screenshot of sidebar issue

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Fire Sans", sans-serif;
}

.app {
  display: flex;
  min-height: 100vh;
  background-image: url(../site/images/bg2.jpeg);
}

.sidebar {
  flex: 1 1 0;
  max-width: 300px;
  padding: 2rem 1rem;
  background-color: #053853;
}

.sidebar h3 {
  color: #f6da9b;
  font-size: 0.75 rem;
  text-transform: uppercase;
  margin-bottom: 0.5em;
}

.sidebar .menu {
  margin: 0 -1rem;
}

.sidebar .menu .menu-item {
  display: block;
  padding: 1em;
  color: #fff;
  text-decoration: none;
  transition: 0.2 linear;
}

.sidebar .menu .menu-item:hover,
.sidebar .menu .menu-item.is-active {
  color: #3bba9c;
  border-right: 5px solid #3bba9c;
}

.content {
  flex: 1 1 0;
  padding: 2rem;
  margin: 4rem;
  width: 60%;
  border: 3px solid #e9d397;
  text-align: center;
  border-radius: 10px;
  backdrop-filter: blur(10px);
}

.content h1 {
  color: #fff;
  font-size: 2.5rem;
  margin-bottom: 1rem;
}

.content p {
  color: #fff;
}

.menu-toggle {
  display: none;
  position: fixed;
  top: 2rem;
  right: 2rem;
  width: 60px;
  height: 60px;
  border-radius: 99px;
  background-color: #2e3047;
  cursor: pointer;
}

.hamburger {
  position: relative;
  top: calc(50% - 2px);
  left: 50%;
  transform: translate(-50%, -50%);
  width: 32px;
}

.hamburger > span,
.hamburger > span::before,
.hamburger > span::after {
  display: block;
  position: absolute;
  width: 100%;
  height: 4px;
  border-radius: 99px;
  background-color: #fff;
  transition-duration: 0.25s;
}

.hamburger > span::before {
  content: "";
  top: -8px;
}

.hamburger > span::after {
  content: "";
  top: 8px;
}

.menu-toggle.is-active .hamburger > span {
  transform: rotate(45deg);
}
.menu-toggle.is-active .hamburger > span::before {
  top: 0;
  transform: rotate(0deg);
}
.menu-toggle.is-active .hamburger > span::after {
  top: 0;
  transform: rotate(90deg);
}

@media (max-width: 1024px) {
  .sidebar {
    max-width: 200px;
  }
}

@media (max-width: 768px) {
  .menu-toggle {
    display: block;
  }
  .content {
    padding: 2rem;
    margin-top: 8rem;
  }
  .sidebar {
    position: fixed;
    top: 0;
    left: -300px;
    height: 100vh;
    width: 100%;
    max-width: 300px;
    transition: 0.2 linear;
  }
  .sidebar.is-active {
    left: 0;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Sidebar</title>
  <link rel="stylesheet" href="style3.css" />
</head>

<body>
  <div class="app">

    <div class="menu-toggle">
      <div class="hamburger">
        <span></span>
      </div>
    </div>

    <aside class="sidebar">
      <h3>Javascript</h3>
      <nav class="menu">
        <a href="#" class="menu-item is-active">Home</a>
        <a href="#" class="menu-item">Week 1</a>
        <a href="#" class="menu-item">Week 2</a>
        <a href="#" class="menu-item">Week 3</a>
        <a href="#" class="menu-item">Week 4</a>
        <a href="#" class="menu-item">Week 5</a>
        <a href="#" class="menu-item">Week 6</a>
        <a href="#" class="menu-item">Week 7</a>
        <a href="#" class="menu-item">Week 8</a>
        <a href="#" class="menu-item">Week 9</a>
        <a href="#" class="menu-item">Week 10</a>
      </nav>
    </aside>

    <main class="content">
      <h1>Welcome, Human!</h1>
      <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolores aliquid impedit tenetur nam, vitae fugiat doloremque ut incidunt inventore quam qui laboriosam! Esse recusandae a inventore aliquam! Distinctio, expedita hic?</p>
    </main>
  </div>

  <script>
    const menu_toggle = document.querySelector('.menu-toggle');
    const sidebar = document.querySelector('.sidebar');
    menu_toggle.addEventListener('click', () => {
      menu_toggle.classList.toggle('is-active');
      sidebar.classList.toggle('is-active');
    })
  </script>

</body>

</html>

推荐答案

你能试着给你的侧边栏比内容高出z-index吗?

.sidebar {
   z-index: 2;
}

.content {
   z-index: 1;
}

Javascript相关问答推荐

为什么!逗号和空格不会作为输出返回,如果它在参数上?

cypress中e2e测试上的Click()事件在Switch Element Plus组件上使用时不起作用

如何避免页面第一次加载时由于CSS样式通过JavaScript更改而出现闪烁

从Nextjs中的 Select 项收集值,但当单击以处理时,未发生任何情况

OnClick更改Json数组JSX中的图像源

当我点击一个按钮后按回车键时,如何阻止它再次被点击

Node.js错误: node 不可单击或不是元素

select 2-删除js插入的项目将其保留为选项

表单数据中未定义的数组键

如何使用Cypress在IFRAME中打字

Django模板中未加载JavaScript函数

需要刷新以查看Mern堆栈应用程序中的更改

如何从嵌套的json中获取最大值

CSS网格使页面自动滚动

Reaction:从子组件调用父组件中的函数

如何在chartjs-plugin-data Labels v2.1.0插件中设置照片?

阻止Electron 打开多个应用程序实例

在Reaction-Router-DOM v6中使用菜单的共享布局时出现问题

使用new关键字或createElement()创建的自定义元素没有is:属性,为什么?

如何在原生JavaScript中阻止精灵从画布上移开?