You need to enable JavaScript to run this app. 侧边栏展开时如何将背景设为半透明且锁定不可交互
优惠活动
大模型
产品
解决方案
定价
更多

侧边栏展开时如何将背景设为半透明且锁定不可交互

修改方案

核心修改点

  • 拆分半透明遮罩样式为独立激活类,默认不生效,解决页面加载就半透明的问题
  • 给激活后的内容区添加pointer-events: none属性,锁定所有背景交互
  • 调整侧边栏与内容区的z-index层级,保证侧边栏操作不受影响

修改说明

  1. 移除了原来默认挂载在#main上的outer-modal类,把半透明效果抽到overlay-active独立类中,默认不生效,解决了页面加载就半透明的问题
  2. overlay-active类添加了pointer-events: none属性,会使整个内容区的所有元素都无法响应鼠标、触摸等交互,自然就无法再次点击打开按钮
  3. 调高了侧边栏的z-index到999,保证侧边栏始终在内容区上方,关闭按钮等交互不受内容区锁定的影响

完整可运行代码

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body {
      font-family: "Lato", sans-serif;
      margin: 0;
    }
    
    .sidebar {
      height: 100%;
      width: 0;
      position: fixed;
      /* 侧边栏层级设为最高,保证可正常点击关闭 */
      z-index: 999;
      top: 0;
      left: 0;
      background-color: #111;
      overflow-x: hidden;
      transition: 0.5s;
      padding-top: 60px;
    }
    
    .sidebar a {
      padding: 8px 8px 8px 32px;
      text-decoration: none;
      font-size: 25px;
      color: #818181;
      display: block;
      transition: 0.3s;
    }
    
    .sidebar a:hover {
      color: #f1f1f1;
    }
    
    .sidebar .closebtn {
      position: absolute;
      top: 0;
      right: 25px;
      font-size: 36px;
      margin-left: 50px;
    }
    
    .openbtn {
      font-size: 20px;
      cursor: pointer;
      background-color: #111;
      color: white;
      padding: 10px 15px;
      border: none;
    }
    
    .openbtn:hover {
      background-color: #444;
    }
    
    #main {
      transition: margin-left .5s, background-color .5s;
      padding: 16px;
      min-height: 100vh;
    }

    /* 侧边栏打开时才激活的遮罩样式 */
    #main.overlay-active {
      background-color: rgba(0, 0, 0, 0.4);
      /* 锁定所有背景交互,无法点击按钮、文字等内容 */
      pointer-events: none;
    }

    /* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
    @media screen and (max-height: 450px) {
      .sidebar {
        padding-top: 15px;
      }
      .sidebar a {
        font-size: 18px;
      }
    }
  </style>
</head>
<body>

  <div id="mySidebar" class="sidebar">
    <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
  </div>

  <div id="main">
    <button class="openbtn" onclick="openNav()">☰ Open Sidebar</button>
    <h2>Collapsed Sidebar</h2>
    <p>Click on the hamburger menu/bar icon to open the sidebar, and push this content to the right.</p>
  </div>

  <script>
    const mainEl = document.getElementById("main")
    const sidebarEl = document.getElementById("mySidebar")

    function openNav() {
      sidebarEl.style.width = "250px";
      mainEl.style.marginLeft = "250px";
      // 激活遮罩和交互锁定
      mainEl.classList.add("overlay-active")
    }

    function closeNav() {
      sidebarEl.style.width = "0";
      mainEl.style.marginLeft = "0";
      // 移除遮罩和交互锁定
      mainEl.classList.remove("overlay-active")
    }
  </script>

</body>
</html>

内容的提问来源于stack exchange,提问作者sfdc fox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 05:15:00