Heim  >  Fragen und Antworten  >  Hauptteil

Titel: Erzielen Sie die Sichtbarkeit von weißem Text auf jedem farbigen Hintergrund außer Weiß

Die Verwendung von mix-blend-mode: Difference zum Ändern der Textfarbe in Weiß funktioniert gut, wenn die Hintergrundfarbe schwarz ist. Bewegen Sie die Maus über den Text, um den Effekt zu sehen:


const blackBox = document.querySelector(".black-box");
window.addEventLis tener('mousemove', function(event) {
  blackBox.style.left = `${event.pageX - 50}px`;
  blackBox.style.top = `${event.pageY - 50}px`;
});
.wrapper {
  background-color: white;
}
h1 {
  position: relative;
  z-index: 2;
  color: white;
  mix-blend-mode: difference;
}

.black-box {
  width: 100px;
  height: 100px;
  position: absolute;
  z-index: 1;
  background-color: black;
}
<div class="wrapper">
  <h1>Lorem Ipsum</h1>
</div>
<div class="black-box"></div>


Dadurch wird kein weißer Text erzeugt, wenn der Hintergrund nicht schwarz ist, was verständlich ist:


const box = document.querySelector(".box");
window.addEventList ener('mousemove', function(event) {
  box.style.left = `${event.pageX - 50}px`;
  box.style.top = `${event.pageY - 50}px`;
});
.wrapper {
  background-color: white;
}
h1 {
  position: relative;
  z-index: 2;
  color: white;
  mix-blend-mode: difference;
}

.box {
  width: 100px;
  height: 100px;
  position: absolute;
  z-index: 1;
  background-image: url("https://placekitten.com/100/100")
}
<div class="wrapper">
  <h1>Lorem Ipsum</h1>
</div>
<div class="box"></div>


Gibt es eine Möglichkeit, die Textfarbe von Schwarz auf Weiß zu ändern, wenn der Hintergrund nicht weiß ist?

P粉744691205P粉744691205313 Tage vor701

Antworte allen(1)Ich werde antworten

  • P粉445714413

    P粉4457144132023-11-11 00:56:49

    这是一个依赖于背景颜色而不是混合混合模式的想法。诀窍是使用与图像相同尺寸的渐变,以相同的方式移动以模拟混合模式:

    const box = document.querySelector(".box");
    const h1 = document.querySelector("h1");
    window.addEventListener('mousemove', function(event) {
      box.style.left = `${event.pageX - 50}px`;
      box.style.top = `${event.pageY - 50}px`;
      
      h1.style.backgroundPosition = `${event.pageX - 50}px ${event.pageY - 50}px`;
    });
    .wrapper {
      background-color: white;
    }
    h1 {
      position: relative;
      z-index: 2;
      color: white;
      background: 
        /*gradient                   position   /    size  */
        linear-gradient(#fff,#fff) -100px -100px/100px 100px fixed no-repeat,
        #000;
      -webkit-background-clip: text;
      background-clip: text;
      -webkit-text-fill-color: transparent;
      color: transparent;
    }
    
    .box {
      width: 100px;
      height: 100px;
      position: absolute;
      z-index: 1;
      background-image: url("https://placekitten.com/100/100")
    }

    Lorem Ipsum

    Antwort
    0
  • StornierenAntwort