Invert colored text related background

后端 未结 2 356
遥遥无期
遥遥无期 2020-12-02 00:57

I want to invert text color based on the background of the text, like in this image: .

So I\'ve tried the following code, but it didn\'t work:

2条回答
  •  自闭症患者
    2020-12-02 01:35

    If you just add the same text to the tri layer, it works:

    Edit: In order to allow selecting the text, I've added another transparent layer that wraps all the text as one unit. It does, however, makes updates more repetitive. As a solution, I've added the attached JavaScript code (which isn't necessary).

    var text = "TEXT";
    var textElements = document.getElementsByTagName("h1");
    for (var i=0; i
    #warp,
    #text,
    #tri,
    #selectable {
      position: absolute;
      top: 0;
      left: 0;
      width: 150px;
      height: 150px;
    }
    
    #warp {
      background-color: orange;
    }
    
    #text {
      text-align: center;
      z-index: 1;
    }
    
    h1 {
      margin: 0;
      line-height: 150px;
      mix-blend-mode: difference;
    }
    
    #tri {
      background-color: black;
      clip-path: polygon(0 0, 0 100%, 99.8% 150px);
      -webkit-clip-path: polygon(0 0, 0 100%, 99.8% 150px);
      z-index: 2;
      color: blue;
      text-align: center;
    }
    
    #selectable {
      text-align: center;
      z-index: 5;
      color: transparent;
    }

    TEXT

    TEXT

    TEXT

提交回复
热议问题