標題:在白色以外的任何顏色的背景上實現白色文字可見性
P粉744691205
P粉744691205 2023-11-10 16:59:01
0
1
763

當背景顏色為黑色時,使用 mix-blend-mode: Difference 將文字顏色變更為白色效果很好。將滑鼠移至文字處即可查看效果:


#
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>


#

如果背景不是黑色,這不會產生白色文本,這是可以理解的:


#
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>


#

有什麼方法可以讓背景與白色不同時文字顏色從黑色變成白色嗎?

P粉744691205
P粉744691205

全部回覆(1)
P粉445714413

這是一個依賴背景顏色而不是混合混合模式的想法。訣竅是使用與影像相同尺寸的漸變,以相同的方式移動以模擬混合模式:

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板