【问题标题】:CSS - Change text color depending on backgroundCSS - 根据背景更改文本颜色
【发布时间】:2019-01-08 20:45:35
【问题描述】:

我有一个简单的布局,由三个全高 div 和一个固定的标题栏组成。

body,html {
  padding:0;
  margin:0;
}

.header {
  position:fixed;
  width:100%;
  height:50px;
  color:white;
  margin:20px;
}

.section1 {
  background:black;
  height:100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color:white;
}

.section2 {
  background:white;
  height:100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.section3 {
  background:black;
  height:100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  color:white;
}
<div class="header">
  Header Content
</div>
<div class="wrapper">
  <div class="section1">
    Section One Content
  </div>
  <div class="section2">
    Section Two Content
  </div>
  <div class="section3">
    Section Three Content
  </div>
</div>

我正在尝试在标题栏文本滚动到具有白色背景的部分时将其颜色更改为黑色。

我已经看到了几个 jQuery 插件,它们应该可以实现这一点,但我试图避免任何不必要的脚本。

CSS mix-blend-mode 函数可以让我实现这个吗?有没有人可以举个例子来说明正在实现的类似目标?

【问题讨论】:

    标签: jquery html css


    【解决方案1】:

    你可以用jquery来做,当有事情发生时,做:

    $('yourdiv1').css({"color": "black"})
    $('yourdiv2').css({"background-color": "white"})
    

    要将其设置为默认值:

    $('yourdiv1').css({"color": ""})
    $('yourdiv2').css({"background-color": ""})
    

    还可以添加transitions 使其看起来非常漂亮。

    【讨论】:

      【解决方案2】:

      您可以使用mix-blend-mode:difference

      body,
      html {
        padding: 0;
        margin: 0;
      }
      
      .header {
        position: fixed;
        width: 100%;
        height: 50px;
        margin: 20px;
        color:#fff;
        mix-blend-mode: difference;
      }
      
      .section1,
      .section2,
      .section3{
        background: black;
        height: 100vh;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
      }
      
      .section2 {
        background: white;
        color:#000;
      }
      <div class="header">
        Header Content
      </div>
      <div class="wrapper">
        <div class="section1">
          Section One Content
        </div>
        <div class="section2">
          Section Two Content
        </div>
        <div class="section3">
          Section Three Content
        </div>
      </div>

      【讨论】:

      • 值得注意的是,IE 和 Edge 目前不支持此功能,因此如果您希望它在这些浏览器中工作,您将需要某种回退。
      • @HiddenHobbes 这并不奇怪,这样的事情永远不会也永远不会在 IE 和 edge 上运行,老实说我不在乎......如果你想使用 IE 或 Edge,那就不要使用幻想用 JS 让事情变得复杂
      • 这并不奇怪,但有必要考虑到访问该网站的用户可能会使用这些浏览器。
      • 根据这个答案:stackoverflow.com/questions/32613896/… 如果浏览器不支持mix-blend-mode,OP 可以使用一些不同的color(如:grey 等)
      • 谢谢大家,完美的例子,超级简单!
      猜你喜欢
      • 2016-06-02
      • 2019-06-20
      • 2015-12-31
      • 2012-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      相关资源
      最近更新 更多