css - 如何使用混合混合模式更改动画背景上的文本颜色

标签 css frontend mix-blend-mode

我有一个带有 4 个盒子的 codepen,

每个框在移动的背景 svg 顶部都有一个文本。我正在使用混合混合模式在背景更改时反转文本的颜色。例如,如果背景是红色,则文本应该是黄色,如果背景是黄色,则文本应该是红色。

我的问题:

  1. 该技术似乎不适用于 Chrome
  2. 该技术似乎不适用于十六进制颜色。我是否遗漏了一点,或者这是一个错误?

工作代码笔:https://codepen.io/nicopoggenburg/pen/xxmaeGw

Left side chrome, right side firefox

Mac Firefox:否,Mac Chrome:否

.box-1 {
  --light: #FCFAF0;
  --dark: #082605;
}

Mac Firefox:,Mac Chrome:否

.box-2 {
  --light: yellow;
  --dark: red;
}

Mac Firefox:否,Mac Chrome:否

.box-3 {
  --light: #66CDAA;
  --dark: #FFA500;
}

Mac Firefox:,Mac Chrome:否

.box-4 {
  --light: green;
  --dark: purple;
}

HTML:

<div class="box box-1">
  <div class="svgs">
    <div class="svg-container">
      <svg class="svg svg-1" viewBox="0 0 1024 244" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M1029 59.77c-258.25 0-258.25 124.62-516.5 124.62S254.25 59.77-4 59.77" stroke="#082605" stroke-width="118.634" stroke-miterlimit="10"/>
      </svg>
    </div>
    <div class="svg-container">
      <svg class="svg svg-2" viewBox="0 0 1024 244" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M1029 59.77c-258.25 0-258.25 124.62-516.5 124.62S254.25 59.77-4 59.77" stroke="#082605" stroke-width="118.634" stroke-miterlimit="10"/>
      </svg>
    </div>
  </div>
  <h1 class="text" data-text="Do reinvent the wheel">Do reinvent<br>the wheel</h1>
</div>

<div class="box box-2">
  <div class="svgs">
    <div class="svg-container">
      <svg class="svg svg-1" viewBox="0 0 1024 244" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M1029 59.77c-258.25 0-258.25 124.62-516.5 124.62S254.25 59.77-4 59.77" stroke="red" stroke-width="118.634" stroke-miterlimit="10"/>
      </svg>
    </div>
    <div class="svg-container">
      <svg class="svg svg-2" viewBox="0 0 1024 244" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M1029 59.77c-258.25 0-258.25 124.62-516.5 124.62S254.25 59.77-4 59.77" stroke="red" stroke-width="118.634" stroke-miterlimit="10"/>
      </svg>
    </div>
  </div>
  <h1 class="text" data-text="Do reinvent the wheel">Do reinvent<br>the wheel</h1>
</div>

<div class="box box-3">
  <div class="svgs">
    <div class="svg-container">
      <svg class="svg svg-1" viewBox="0 0 1024 244" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M1029 59.77c-258.25 0-258.25 124.62-516.5 124.62S254.25 59.77-4 59.77" stroke="#FFA500" stroke-width="118.634" stroke-miterlimit="10"/>
      </svg>
    </div>
    <div class="svg-container">
      <svg class="svg svg-2" viewBox="0 0 1024 244" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M1029 59.77c-258.25 0-258.25 124.62-516.5 124.62S254.25 59.77-4 59.77" stroke="#FFA500" stroke-width="118.634" stroke-miterlimit="10"/>
      </svg>
    </div>
  </div>
  <h1 class="text" data-text="Do reinvent the wheel">Do reinvent<br>the wheel</h1>
</div>

<div class="box box-4">
  <div class="svgs">
    <div class="svg-container">
      <svg class="svg svg-1" viewBox="0 0 1024 244" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M1029 59.77c-258.25 0-258.25 124.62-516.5 124.62S254.25 59.77-4 59.77" stroke="purple" stroke-width="118.634" stroke-miterlimit="10"/>
      </svg>
    </div>
    <div class="svg-container">
      <svg class="svg svg-2" viewBox="0 0 1024 244" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M1029 59.77c-258.25 0-258.25 124.62-516.5 124.62S254.25 59.77-4 59.77" stroke="purple" stroke-width="118.634" stroke-miterlimit="10"/>
      </svg>
    </div>
  </div>
  <h1 class="text" data-text="Do reinvent the wheel">Do reinvent<br>the wheel</h1>
</div>

CSS:

@keyframes moveWave {
  to {
    transform: translateX(-50%);
  }
}


.box-1 {
  --light: #FCFAF0;
  --dark: #082605;
}
.box-2 {
  --light: yellow;
  --dark: red;
}
.box-3 {
  --light: #66CDAA;
  --dark: #FFA500;
}
.box-4 {
  --light: green;
  --dark: purple;
}

.box {
  position: relative;
  overflow: hidden;
  background: var(--light);
  color: var(--light);
  padding: 20px 0;
  isolation: isolate;
}
.svgs {
  display: flex;
  width: 200%;
  animation: moveWave 5s linear infinite;
  display: flex;
  align-items: center;
}
.svg-container {
  width: 100%;
}
.svg {
  width: 100%;
}
.text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  font-size: 90px;
  text-transform: uppercase;
  margin: 0;
  mix-blend-mode: difference;
  font-weight: 800;
  width: fit-content;
  color: var(--dark);
}

.text:after {
  content: attr(data-text);
  mix-blend-mode: difference;
  position: absolute;
  left: 0;
  top: 0;
  color: var(--light);
}

最佳答案

需要再次使用第二种颜色显示文本(例如,使用.text:after)并应用mix-blend-mode: Difference; :

@keyframes moveWave {
  to {
    transform: translateX(-50%);
  }
}

:root {
  --light: yellow;
  --dark: red;
}
.box {
  position: relative;
  overflow: hidden;
  background: var(--light);
  color: var(--light);
  padding: 20px 0;
}
.svgs {
  display: flex;
  width: 200%;
  animation: moveWave 5s linear infinite;
  display: flex;
  align-items: center;
}
.svg-container {
  width: 100%;
}
.svg {
  width: 100%;
}
.text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  font-size: 90px;
  text-transform: uppercase;
  margin: 0;
  mix-blend-mode: difference;
  font-weight: 800;
  width: fit-content;
  color: var(--dark);
}

.text:after {
  content: attr(data-text);
  mix-blend-mode: difference;
  position: absolute;
  left: 0;
  top: 0;
  color: var(--light);
}
<div class="box">
  <div class="svgs">
    <div class="svg-container">
      <svg class="svg svg-1" viewBox="0 0 1024 244" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M1029 59.77c-258.25 0-258.25 124.62-516.5 124.62S254.25 59.77-4 59.77" stroke="red" stroke-width="118.634" stroke-miterlimit="10"/>
      </svg>
    </div>
    <div class="svg-container">
      <svg class="svg svg-2" viewBox="0 0 1024 244" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M1029 59.77c-258.25 0-258.25 124.62-516.5 124.62S254.25 59.77-4 59.77" stroke="red" stroke-width="118.634" stroke-miterlimit="10"/>
      </svg>
    </div>
  </div>
  <h1 class="text" data-text="Do reinvent the wheel">Do reinvent<br>the wheel</h1>
</div>

关于css - 如何使用混合混合模式更改动画背景上的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77190919/

相关文章:

javascript - 在 JS 中的正方形内绘制正方形

web-services - 构建丰富的Web "desktop"环境

css - 如何在不使用 CSS 属性 mix-blend-mode 的情况下获得混合模式效果 : multiply

css - 是否可以将 css 混合模式应用于不同 div 中的元素?

html - 影响 Mac Chrome 上其他元素的 CSS mix-blend-mode

css - 如何隔离 Vuetify 全局样式

html - CSS:创建一个带有边界半径的时钟

javascript - HTML 和 CSS : How to lay out a div tag with width:100% and left:300px

javascript - 你能在 jade 中引用带有破折号的变量吗?

css - CSS 中的内联元素