javascript - 根据背景更改固定位置div的背景和字体颜色

标签 javascript html css mix-blend-mode

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
    <div class="background1">Div1</div>
    <div class="background2">Div2</div>
    <div class="background3">Div3</div>
    <div class="background4">Div4</div>
    <div class="fixed-background">This should change color based on background</div>

</body>
</html>




div {
    height: 100vh;
}

.background1 {
    background: gray;
}

.background2 {
    background: black;
}

.background3 {
    background: blue;
}

.background4 {
    background: tomato;
}

.fixed-background {
    position: fixed;
    top: 10px;
    left: 10px;
    width: 100px;
    height: 100px;
    mix-blend-mode: difference;
}

大家好,我尝试根据另一个div的背景使用fixed-background类来更改div的背景和颜色。我遇到了 mix-blend-mode CSS 属性,但它似乎不起作用。

我到目前为止还不是 CSS 专家,有人可以帮助我让它工作吗?如果我让它与 JS 函数一起工作,我不会不高兴。

最佳答案

我一直在尝试mix-blend-mode,也许我已经得到了你需要的东西。请检查一下。重要的是您必须检查浏览器的兼容性。

https://developer.mozilla.org/en-US/docs/Web/CSS/mix-blend-mode

body {
  margin-bottom: 50vh;
}

.background1,
.background2,
.background3,
.background4 {
  height: 50vh;
}

.background1 {
  background: gray;
}

.background2 {
  background: black;
}

.background3 {
  background: blue;
}

.background4 {
  background: tomato;
}

.fixed {
  position: fixed;
  top: 10px;
  right: 10px;
  width: 100px;
  height: 100px;
  mix-blend-mode: screen;
}

.fixed-text {
  background: green;
  color: maroon;
  mix-blend-mode: difference;
  z-index: 3;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
<div class="background1">Div1</div>
<div class="background2">Div2</div>
<div class="background3">Div3</div>
<div class="background4">Div4</div>
<div class="fixed">
  <div class="fixed-text">
    This should change color based on background
  </div>
</div>

关于javascript - 根据背景更改固定位置div的背景和字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60834601/

相关文章:

javascript - 将填充顶部添加到 Cordova 状态栏

javascript - 添加加载动画,直到特定的 JavaScript 完成加载

html - 背景颜色停在 100%,但当屏幕必须向右滚动时则不会。任何想法如何解决

javascript - 如何在nodejs中获取项目的单独id

javascript - 图表 js。如何通过工具提示的中心对齐文本?

html - 左侧导航栏过度折叠时如何解决设计问题

html - 在菜单项之间留出更大的空间 (nav)

html - 使用具有 100% 高度的颜色叠加的背景视频

jquery - Bootstrap 轮播过渡故障

html - 如何消除网页上 iPhone 缺口处的空白