混合模式不影响任何组件

问题描述

我尝试在网上搜索各种答案并尝试解决其他人问题的所有方法,但我的 CSS 就是不想合作。

Here 你可以找到一个 JSfiddle,我在那里重新创建了这个问题:我希望侧面导航也能在黑色部分可见。

HTML
<div class="sidenav">
  <a href="#" class="selected">Section 1</a>
  <a href="#">Section 2</a>
  <a href="#">Section 3</a>
</div>

<div class="content">
  <div>
    <p>Some text<p>
  </div>
  <div class="blk">
    <p>Some text<p>
  </div>
  <div>
    <p>Some text<p>
  </div>
</div>


CSS
.sidenav {
  width: auto;
  position: fixed;
  z-index: 1;
  top: 150px;
  padding: 8px 0;
}

.sidenav a {
  clear: both;
  float: left;
  position: relative;
  left: -20%;
  padding: 10px;
  margin-bottom: 15px;
  transition: 0.3s;

  text-decoration: none;
  text-align: right;
  font-size: 24px;

  border-style: solid;
  border-width: 2px;
  border-color: #0D0D0D;
  border-radius: 0 5px 5px 0;

  color: #0D0D0D;
  mix-blend-mode: difference;
}

.blk {
  background-color: #0d0d0d;
  color: #ffffff;
}

解决方法

你想要这样的东西吗?

html {
  scroll-behavior: smooth;
  background: #fff;
}

body {
  margin: 0;
  padding: 0;
  width: 100%;
  max-width: 100%;

  background-color: #ffffff;
  color: #0d0d0d;
}

/**********************************************/

.section {
  width: 100vw;
  max-width: 100%;
  background-color: #ffffff;
  text-align: center;

  margin: 0;
  padding: 15vh 0;
  position: relative;
}

.blk {
  background-color: #0d0d0d;
  color: #ffffff;
}

/**********************************************/

.sidenav {
  width: auto;
  position: fixed;
  z-index: 1;
  top: 150px;
  padding: 8px 0;
  mix-blend-mode: difference;
}

.sidenav a {
  clear: both;
  float: left;
  position: relative;
  left: -20%;
  padding: 10px;
  margin-bottom: 15px;
  transition: 0.3s;

  text-decoration: none;
  text-align: right;
  font-size: 24px;

  border-style: solid;
  border-width: 2px;
  border-color: #fff;
  border-radius: 0 5px 5px 0;

  color: #fff;
  background-color: #000;

}

.sidenav a:hover {
  left: -2px;
}

.sidenav a.selected {
  left: -2px;
  color: #000;
  background-color: #fff;
  cursor: default;
}
<!doctype html>
<html>
<head></head>

  <body>

    <div class="sidenav">
      <a href="#" class="selected">&emsp;&emsp;Section 1</a>
      <a href="#">&emsp;&emsp;Another Section</a>
      <a href="#">&emsp;&emsp;Last One</a>
    </div>

    <div class="content" onclick="closeMenu()">
      <div class="section" id="works">
        <p>Some text<p>
      </div>
      <div class="section blk" id="works">
        <p>Some text<p>
      </div>
      <div class="section" id="works">
        <p>Some text<p>
      </div>
      <div class="section blk" id="works">
        <p>Some text<p>
      </div>
      <div class="section" id="works">
        <p>Some text<p>
      </div>
    </div>
  </body>
</html>

您需要将 mix-blend-mode 添加到 sidenav 等父元素,而不是更改 a 中的背景、颜色和边框颜色