我正在try 使用html、css和js制作一个汉堡菜单滑入/滑出动画,但侧边栏的起点在屏幕外(和html正文之外),这会留下一个水平滚动.我试着不用‘位置:绝对’,但它似乎不起作用.

以下是这个问题的一张图片:

Look at the horizontal scroll

动画幻灯片运行得很好,问题是这个水平滚动.还有:我不知道为什么滑出动画不起作用.

function hamburguer_menu() {
  const botao_hamburguer = document.querySelector('#botao_hamburguer')
  const sidebar = document.querySelector('.sidebar')
  
  if (sidebar.style.opacity == 0) {
    sidebar.style.opacity = 1
    sidebar.style.translate = '0'
    sidebar.style.transition = 'translate ease-in 0.5s'
  } else {
    sidebar.style.opacity = 0
    sidebar.style.translate = '100%'
    sidebar.style.transition = 'translate ease-in 0.5s'
  }
}
html,
body {
  min-height: 100dvh;
  max-width: 100dvw;
  padding: 0;
  margin: 0;
}

#layout_bar {
  background-color: black;
  height: var(--heading_height);
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  box-shadow: #000000a1 1px 0px 9px 0px;
}

.botao_hamburguer {
  background-color: pink; /* transparent; */
  border: none;
  z-index: 2;
}

.sidebar {
  opacity: 0;
  transition: translate ease-in 0.5s;
  text-align: center;
  margin: 0;
  padding: 0;
  right: 0%;
  z-index: 1;
  position: absolute;
  translate: 100%;
  top: 0%;
  background-color: rgb(27, 27, 27);
  color: white;
  width: 300px;
  min-height: 100dvh;
}
<body>
  <!-- script src="/static/fit/js.js"></script -->
  
  <heading id="layout_bar">
    <h1 id="logo"><a href=""><span class="bigger">B</span>e<span class="bigger">B</span>etter</a></h1>
    <button type="button" onclick="hamburguer_menu()" id="botao_hamburguer" class="botao_hamburguer">
      <span class="material-symbols-outlined icon-branco">menu</span>
      <span class="material-symbols-outlined icon-branco" style="display: none;"></span>
    </button>
  </heading>

  <aside class="sidebar">
    <ul style="list-style-type: none;">
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
    </ul>
  </aside>

</body>

推荐答案

问题是,翻译后的元素会留下它们的原始框.你需要处理好额外的空间.我已经在您的侧边栏周围放置了一个带有隐藏溢出的框.

也请注意我对你 playbook 的修改.最好不要在脚本中手动应用样式,而是更改CSS类.

我还从侧边栏中删除了一些不需要的css属性,零值不需要单位.侧边栏还需要隐藏溢出以正确包含列表.

function hamburguer_menu() {
  document.querySelector('.sidebar').classList.toggle('in');
}
html,
body {
  min-height: 100dvh;
  max-width: 100dvw;
  padding: 0;
  margin: 0;
}

#layout_bar {
  background-color: black;
  height: var(--heading_height);
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  box-shadow: #000000a1 1px 0px 9px 0px;
}

.botao_hamburguer {
  background-color: pink;
  border: none;
  z-index: 2;
}

.sidebar-box {
  overflow: hidden;
  position: absolute;
  right: 0;
  top: 0;
}

.sidebar {
  overflow: hidden;
  opacity: 0;
  transition: all ease 0.5s;
  text-align: center;
  z-index: 1;
  margin-right: -100%;
  background-color: rgb(27, 27, 27);
  color: white;
  translate: 300px;
  width: 300px;
  min-height: 100dvh;
}

.sidebar.in {
  opacity: 1;
  margin-right: 0;
  translate: 0;
}
<body>
  <!-- script src="/static/fit/js.js"></script -->

  <heading id="layout_bar">
    <h1 id="logo"><a href=""><span class="bigger">B</span>e<span class="bigger">B</span>etter</a></h1>

    <button type="button" onclick="hamburguer_menu()" id="botao_hamburguer" class="botao_hamburguer">
      <span class="material-symbols-outlined icon-branco">menu</span>
      <span class="material-symbols-outlined icon-branco" style="display: none;"></span>
    </button>
  </heading>

  <div class="sidebar-box">
    <aside class="sidebar">
      <ul style="list-style-type: none;">
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
      </ul>
    </aside>
  </div>
</body>

Javascript相关问答推荐

为什么新的Promises会在不需要的情况下被用来等待?

TypScript:根据共享属性对项目进行分组并为其分配groupID

如何使用3个部件之间的路由?

在JavaScript中逐一播放随机生成的音频文件数组

TypScript界面中的Infer React子props

按钮未放置在html dis位置

如何在Connect 4游戏中 for each 玩家使用位板寻找7形状?

从WooCommerce Checkout Country字段重新排序国家,保持国家同步

函数返回与输入对象具有相同键的对象

WebGL 2.0无符号整数输入变量

为什么我的导航条打开状态没有在窗口addeventlistener(Reaction Js)中更新?

对网格项目进行垂直排序不起作用

禁用.js文件扩展名并从目录导入隐式根index.js时,找不到NodeJS导入模块

搜索功能不是在分页的每一页上进行搜索

Reaction-SWR-无更新组件

try 将Redux工具包与MUI ToggleButtonGroup组件一起使用时出错

FireBase云函数-函数外部的ENV变量

WebSocketException:远程方在未完成关闭握手的情况下关闭了WebSocket连接.&#三十九岁;

如何使本地html页面在重新加载时保持当前可隐藏部分的打开状态?

使用RxJS from Event和@ViewChild vs KeyUp事件和RxJS主题更改输入字段值