You need to enable JavaScript to run this app. Divi全屏菜单:点击汉堡图标打开时无法将透明背景改为黑色求助
优惠活动
大模型
产品
解决方案
定价
更多

Divi全屏菜单:点击汉堡图标打开时无法将透明背景改为黑色求助

Divi 全屏菜单背景无法切换为纯黑的问题排查与修正方案

核心问题定位

  • 类选择器语法错误:原代码中写的section-transform{cursor: pointer;}是标签选择器,无法匹配自定义的class="section-transform"元素,类选择器必须加.前缀
  • 触发逻辑完全割裂:三套独立的交互逻辑没有做状态同步:
    1. 汉堡动画依赖原生checkbox的:checked状态驱动样式
    2. 原JS绑定#menu-open点击为全屏菜单加激活类
    3. 单独为#hamburger元素绑定点击加active类
      实际点击汉堡时,三类触发元素没有关联,激活类根本无法稳定加到全屏菜单容器上
  • CSS语法疏漏:.bar样式块中width 40px;缺失冒号,导致对应样式不生效
  • 脚本执行时机错误:单独编写的$('#hamburger').click()没有包裹DOM就绪钩子,若脚本在元素加载完成前执行,会直接匹配不到元素导致交互失效
  • 第三方样式覆盖:Divi自带的头部区域存在高优先级的预设背景、透明度、z-index样式,会直接覆盖编写的菜单背景规则

分步修正方法

1. 修正CSS语法错误,补全类选择器规则

/* 修正类选择器,补全前置. */
.section-transform {
  cursor: pointer;
  /* 保留原有过渡动画,补全初始定位状态 */
  position: fixed;
  top: 0;
  left: 0;
  height: 0;
  width: 100%;
  background-color: transparent !important;
  z-index: 9999;
  overflow: hidden;
  -webkit-transition: all 0.5s ease !important;
  -moz-transition: all 0.5s ease !important;
  -o-transition: all 0.5s ease !important;
  -ms-transition: all 0.5s ease !important;
  transition: all 0.5s ease !important;
}

.section-transform-active {
  height: 100vh !important;
  width: 100% !important;
  z-index: 99999 !important;
  /* 强制覆盖Divi的默认透明度与背景 */
  background: #000000 !important;
  opacity: 1 !important;
}

/* 修正.bar的属性语法缺失 */
.bar {
  width: 40px; /* 补全原代码缺失的冒号 */
  height: 4px;
  margin-bottom: 9.3333333333333333px;
  background: #fff;
  transition: transform 0.2s ease-in-out, color 0.5s linear;
}

2. 统一交互逻辑,解决触发割裂问题

把分散的脚本统一包裹在DOM就绪钩子内,同步所有关联元素的状态,避免逻辑冲突:

jQuery(function($){
  // 统一所有可能的汉堡触发元素
  const $toggleTriggers = $('#menu-open, #smallWrapper input, #hamburger');
  const $fullScreenMenu = $('.section-transform');

  $toggleTriggers.on('click', function(e){
    e.preventDefault();
    // 切换全屏菜单激活状态
    $fullScreenMenu.toggleClass('section-transform-active');
    // 同步汉堡按钮的active类状态
    $('#hamburger').toggleClass('active');
    // 同步checkbox勾选状态,保证汉堡变形动画正常运行
    $('#smallWrapper input').prop('checked', $fullScreenMenu.hasClass('section-transform-active'));
  });

  // 保留点击菜单项关闭菜单的逻辑,同步重置所有状态
  $('.menu-item-click').on('click', function() {
    $fullScreenMenu.removeClass('section-transform-active');
    $('#hamburger').removeClass('active');
    $('#smallWrapper input').prop('checked', false);
  });
});

3. 针对Divi预设样式的兜底处理

如果应用上述代码后,菜单仍有透明残留,可在Divi主题选项的头部构建器设置中,找到全屏菜单容器的配置项,将默认背景透明度调整为100%(不透明),从主题层面移除默认的透明属性,避免自定义样式被内联样式覆盖。


内容的提问来源于stack exchange,提问作者Nick Reijrink

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.23 16:24:05