当背景颜色为黑色时,将文本颜色更改为白色效果很好mix-blend-mode: difference
。将鼠标移到文字上查看效果:
const blackBox = document.querySelector(".black-box");
window.addEventListener('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>
如果背景不是黑色,这可以理解不会导致白色文本:
const box = document.querySelector(".box");
window.addEventListener('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>
有什么方法可以使文本在背景与白色不同时立即从黑色变为白色?