【问题标题】:CSS Backdrop-filter not functioning on child element in slideout menuCSS背景过滤器在滑出菜单中的子元素上不起作用
【发布时间】:2020-06-18 16:28:02
【问题描述】:

我正在尝试将backdrop-filter 效果应用于由两个嵌套divs 组成的滑出菜单。我已经设法解决了如何改变元素位置的问题,但是由于某种原因,只要菜单的底部嵌套在顶部的“选项卡”内,下部的过滤器就不会功能,如左图所示。如果将 only 应用于内部 div(如右图所示),它确实有效,但我希望对两者都有影响。如果它们没有嵌套,我不知道如何使悬停时的滑出过渡起作用。

我发现最接近这个问题的讨论是here,但我不确定它是如何应用的。

这是演示:

body {
  background: repeating-linear-gradient(
    45deg,
    #606dbc,
    #606dbc 10px,
    #465298 10px,
    #465298 20px);
  height: 100%;
  width: 100%;
  font-family: 'Work Sans', sans-serif;
  font-size: 1rem;
  text-align: center;
  color: #F0F8FF;
}

/* -------------------GLASS EFFECT------------------- */

.glass {
  backdrop-filter: contrast(130%) brightness(120%) blur(2px);
  background:
    radial-gradient(
      ellipse at 16.7% -10%,
      hsla(0, 0%, 100%, 0.45) 24%,
      hsla(0, 0%, 100%, 0.4) 25%,
      hsla(0, 0%, 100%, 0.2) 45%,
      hsla(0, 0%, 100%, 0.1)
    );
  background-size: 300% 100%;
  border-radius: 10px;
  box-shadow:
    0 2px 1px hsla(0, 0%, 100%, 0.5) inset, /* Highlight upper edge */
    0 -2px 1px hsla(250, 70%, 5%, 0.3) inset, /* Shade lower edge */
    0 -2px 6px hsla(0, 0%, 100%, 0.25); /* Imply light cast around the edges */
}
.frame {
  padding: 20px;
}
.centered {
  position: absolute;
  top: 30%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* -------------------Menu version------------------- */

.glassUpper {
  position: fixed;
  bottom: 0px;
  width: 35px;
  height: 30px;
  background:
    radial-gradient(
      ellipse at 16.7% -10%,
      hsla(0, 0%, 100%, 0.4) 24%,
      hsla(0, 0%, 100%, 0.3) 25%,
      hsla(0, 0%, 100%, 0.1) 45%,
      hsla(0, 0%, 100%, 0.05)
    );
  background-size: 300% 100%;
  padding: 5px 2px 0 2px;
  text-align: center; 
  border-radius: 8px 8px 0 0;
  box-shadow:
    0 2px 1px hsla(0, 0%, 100%, 0.5) inset, /* Highlight upper edge */
    0 -2px 1px hsla(250, 70%, 5%, 0.3) inset, /* Shade lower edge */
    0 -2px 6px hsla(0, 0%, 100%, 0.25); /* Imply light cast around the edges */
  transition: bottom 1s;
  transition-duration: 400ms ease-in;
}
#menuUpperRight {
    right: 40px;
}
#menuUpperLeft {
    left: 40px;
    backdrop-filter: contrast(130%) brightness(120%) blur(2px);
}
.glassLower {
  position: fixed;
  bottom: -120px;
  right: 40px;
  width: 200px;
  height: 110px;
  padding: 5px 0px;
  background:
    radial-gradient(
      ellipse at 16.7% -10%,
      hsla(0, 0%, 100%, 0.4) 24%,
      hsla(0, 0%, 100%, 0.3) 25%,
      hsla(0, 0%, 100%, 0.1) 45%,
      hsla(0, 0%, 100%, 0.05)
    );
  background-size: 300% 100%;
  text-align: center;
  opacity: 0.75;
  box-shadow:
    0 2px 1px hsla(0, 0%, 100%, 0.5) inset, /* Highlight upper edge */
    0 -2px 1px hsla(250, 70%, 5%, 0.3) inset, /* Shade lower edge */
    0 -2px 6px hsla(0, 0%, 100%, 0.25); /* Imply light cast around the edges */
  transition: bottom 1s;
  transition-duration: 400ms ease-in;
}
#menuLowerRight {
  right: 40px;
  border-radius: 10px 0 0 0;
  backdrop-filter: contrast(130%) brightness(120%) blur(2px);
}
#menuLowerLeft {
  left: 0px;
  border-radius: 0 10px 0 0;
  transform: translate3d(0, 0, 0);
  backdrop-filter: contrast(130%) brightness(120%) blur(2px);
}
.glassUpper:hover {
  bottom: 120px;
}
#menuUpperRight:hover #menuLowerRight {
  bottom: 0;
}
<!-- First -->
<div class="glass frame centered">
  <section class="content">
    <p>A light glass-effect frame.</p>
  </section>
</div>

<!-- Menus! -->
  <div class="glassUpper" id="menuUpperRight">
     <svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="24px" height="24px" viewBox="0 0 24 24" fill="none" stroke="rgba(240, 248, 255, 0.75)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
      <circle cx="12" cy="12" r="10"/>
      <line x1="12" y1="17.291" x2="12" y2="10.708"/>
      <line x1="12" y1="6.708" x2="12.01" y2="6.708"/>
    </svg>
    <div class="glassLower" id="menuLowerRight">
        <p>Working when applied to child dev only!</p>
      <!-- custom play and pause buttons -->
      <button class="radioButton" id="play">Play</button>
      <button class="radioButton" id="pause">Pause</button>
      <!-- Credits -->
      <button class="bigButton" id="credits">Credits</button>
    </div>
  </div> 

  <div class="glassUpper" id="menuUpperLeft">
     <svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="24px" height="24px" viewBox="0 0 24 24" fill="none" stroke="rgba(240, 248, 255, 0.75)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
      <circle cx="12" cy="12" r="10"/>
      <line x1="12" y1="17.291" x2="12" y2="10.708"/>
      <line x1="12" y1="6.708" x2="12.01" y2="6.708"/>
    </svg>

    <div class="glassLower" id="menuLowerLeft">
        <p>Backdrop Filters broken on child div???</p>
      <!-- custom play and pause buttons -->
      <button class="radioButton" id="play">Play</button>
      <button class="radioButton" id="pause">Pause</button>
      <!-- Credits -->
      <button class="bigButton" id="credits">Credits</button>
    </div>
  </div> 

【问题讨论】:

    标签: html css css-filters


    【解决方案1】:

    使用额外的包装器可以解决这个问题,然后您可以在内部将元素上/下分开:

    body {
      background: repeating-linear-gradient(
        45deg,
        #606dbc,
        #606dbc 10px,
        #465298 10px,
        #465298 20px);
      height: 100%;
      width: 100%;
      font-family: 'Work Sans', sans-serif;
      font-size: 1rem;
      text-align: center;
      color: #F0F8FF;
    }
    
    /* -------------------GLASS EFFECT------------------- */
    
    .glass {
      backdrop-filter: contrast(130%) brightness(120%) blur(2px);
      background:
        radial-gradient(
          ellipse at 16.7% -10%,
          hsla(0, 0%, 100%, 0.45) 24%,
          hsla(0, 0%, 100%, 0.4) 25%,
          hsla(0, 0%, 100%, 0.2) 45%,
          hsla(0, 0%, 100%, 0.1)
        );
      background-size: 300% 100%;
      border-radius: 10px;
      box-shadow:
        0 2px 1px hsla(0, 0%, 100%, 0.5) inset, /* Highlight upper edge */
        0 -2px 1px hsla(250, 70%, 5%, 0.3) inset, /* Shade lower edge */
        0 -2px 6px hsla(0, 0%, 100%, 0.25); /* Imply light cast around the edges */
    }
    .frame {
      padding: 20px;
    }
    .centered {
      position: absolute;
      top: 30%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    
    /* -------------------Menu version------------------- */
    
    .glassUpper {
      width: 35px;
      height: 30px;
      background:
        radial-gradient(
          ellipse at 16.7% -10%,
          hsla(0, 0%, 100%, 0.4) 24%,
          hsla(0, 0%, 100%, 0.3) 25%,
          hsla(0, 0%, 100%, 0.1) 45%,
          hsla(0, 0%, 100%, 0.05)
        );
      background-size: 300% 100%;
      padding: 5px 2px 0 2px;
      text-align: center; 
      border-radius: 8px 8px 0 0;
      box-shadow:
        0 2px 1px hsla(0, 0%, 100%, 0.5) inset, /* Highlight upper edge */
        0 -2px 1px hsla(250, 70%, 5%, 0.3) inset, /* Shade lower edge */
        0 -2px 6px hsla(0, 0%, 100%, 0.25); /* Imply light cast around the edges */
      transition: bottom 1s;
      transition-duration: 400ms ease-in;
    }
    
    #menuUpperLeft {
        backdrop-filter: contrast(130%) brightness(120%) blur(2px);
    }
    .glassLower {
      width: 200px;
      height: 110px;
      padding: 5px 0px;
      background:
        radial-gradient(
          ellipse at 16.7% -10%,
          hsla(0, 0%, 100%, 0.4) 24%,
          hsla(0, 0%, 100%, 0.3) 25%,
          hsla(0, 0%, 100%, 0.1) 45%,
          hsla(0, 0%, 100%, 0.05)
        );
      background-size: 300% 100%;
      text-align: center;
      opacity: 0.75;
      box-shadow:
        0 2px 1px hsla(0, 0%, 100%, 0.5) inset, /* Highlight upper edge */
        0 -2px 1px hsla(250, 70%, 5%, 0.3) inset, /* Shade lower edge */
        0 -2px 6px hsla(0, 0%, 100%, 0.25); /* Imply light cast around the edges */
      transition: bottom 1s;
      transition-duration: 400ms ease-in;
    }
    
    #menuLowerLeft {
      border-radius: 0 10px 0 0;
      transform: translate3d(0, 0, 0);
      backdrop-filter: contrast(130%) brightness(120%) blur(2px);
    }
    
    .extra{
      position: fixed;
      top: calc(100% - 35px);
      left: 40px;
      transition:0.5s;
      pointer-events:none;
    }
    .extra * {
      pointer-events:initial;
    }
    .extra:hover {
      top: calc(100% - 155px);
    }
    <!-- First -->
    <div class="glass frame centered">
      <section class="content">
        <p>A light glass-effect frame.</p>
      </section>
    </div>
    
    <!-- Menus! -->
    
      <div class="extra">
      <div class="glassUpper" id="menuUpperLeft">
         <svg xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" width="24px" height="24px" viewBox="0 0 24 24" fill="none" stroke="rgba(240, 248, 255, 0.75)" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
          <circle cx="12" cy="12" r="10"/>
          <line x1="12" y1="17.291" x2="12" y2="10.708"/>
          <line x1="12" y1="6.708" x2="12.01" y2="6.708"/>
        </svg>
      </div>
        <div class="glassLower" id="menuLowerLeft">
            <p>Backdrop Filters broken on child div???</p>
          <!-- custom play and pause buttons -->
          <button class="radioButton" id="play">Play</button>
          <button class="radioButton" id="pause">Pause</button>
          <!-- Credits -->
          <button class="bigButton" id="credits">Credits</button>
        </div>
        </div>

    【讨论】:

    • 哦,嘿,我想这确实使它工作......它将菜单的悬停区域扩展到可见选项卡的一侧,但我想这是现​​在的权衡。谢谢!
    • @karmicRetribution 我们可以轻松解决此问题;)检查更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    相关资源
    最近更新 更多