代码之家  ›  专栏  ›  技术社区  ›  fightstarr20

CSS-根据背景更改文本颜色

  •  2
  • fightstarr20  · 技术社区  · 6 年前

    我有一个简单的布局包括三个全高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 功能允许我实现这个吗?有没有人能给我举个类似的例子?

    2 回复  |  直到 6 年前
        1
  •  6
  •   Temani Afif    6 年前

    你可以用 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>
        2
  •  0
  •   Duma    6 年前

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

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

    要将其设置为默认值,请执行以下操作:

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

    同时添加 transitions 让它看起来很漂亮。