在任何非白色背景上将文本颜色更改为白色 [英] Change text color to white on any non-white background
问题描述
使用mix-blend-mode: difference
可以将背景颜色为黑色时将文本颜色更改为白色.将鼠标移到文本上可以查看效果:
Changing the text color to white when the background color is black works great using mix-blend-mode: difference
. Move the mouse to the text to see the effect:
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>
如果背景不是黑色,则不会产生白色文本,这是可以理解的:
This understandably doesn't result in white text if the background is anything other than black:
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>
有什么方法可以使背景一旦与白色不同,文本就会从黑色变为白色?
Is there any way to make it so the text changes color from black to white as soon as the background differs from white?
推荐答案
这是一个依靠背景颜色而不是mix-blend-mode
的想法.诀窍是要使用与图像相同尺寸的渐变,并以相同的方式模拟混合模式:
Here is an idea that rely on background coloration and not mix-blend-mode
. The trick is to have a gradient with the same dimension as the image that you move the same way to simulate the blend mode:
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")
}
<div class="wrapper">
<h1>Lorem Ipsum</h1>
</div>
<div class="box"></div>
我已经考虑过background-attachment:fixed
相对于视口放置渐变,因为您的元素是position:absolute
,没有祖先放置,因此它也相对于视口放置.
I have considered background-attachment:fixed
to place the gradient relatively to the viewport since your element is position:absolute
with no ancestor positioned so it's also positioned relatively to the viewport.
这篇关于在任何非白色背景上将文本颜色更改为白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!