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

Safari使用白色/淡白色渲染透明线性渐变

  •  0
  • gr4viton  · 技术社区  · 6 年前

    我想创建一个从左到右的线性渐变,以透明开始,以黑色结束。

    .box1 {
      border: solid 1px #ccc;
      margin: 10px;
      max-width: 500px;
      height: 40px;
      position: relative;
      font-size: 30px;
      white-space: nowrap;
      overflow: hidden;
      background-color: #2d2d2d;
      color: red;
    }
    .box1::before {
      content: "";
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      background-image: linear-gradient(
        to right, rgba(255,255,255,0.01), #2d2d2d
      );
    }
    
    .box2 {
      border: solid 1px #ccc;
      margin: 10px;
      max-width: 500px;
      height: 40px;
      position: relative;
      font-size: 30px;
      white-space: nowrap;
      overflow: hidden;
      background-color: #2d2d2d;
      color: red;
    }
    .box2::before {
      content: "";
      position: absolute;
      top: 0;
      bottom: 0;
      right: 0;
      width: 100px;
      background-image: linear-gradient(
        to right, rgba(255,255,255,0), #2d2d2d
      );
    }
    
    .box3 {
      border: solid 1px #ccc;
      margin: 10px;
      max-width: 500px;
      height: 40px;
      position: relative;
      font-size: 30px;
      white-space: nowrap;
      overflow: hidden;
      background-color: rgba(45,45,45,1);
      color: red;
    }
    .box3::before {
      content: "";
      position: absolute;
      top: 0;
      bottom: 0;
      right: 0;
      width: 100px;
      background: linear-gradient(to right, rgba(255,255,255,0) 0%,rgba(45,45,45,1) 100%);
    }
    
    .fade1 {
      position: absolute;
      top: 0;
      bottom: 0;
      right: 0;
      width: 75px;
      background: linear-gradient(
        to right, rgba(255,255,255,0), black
      );
    }
    
    .box {
      border: solid 1px #ccc;
      margin: 10px;
      max-width: 500px;
      height: 40px;
      position: relative;
      font-size: 30px;
      white-space: nowrap;
      overflow: hidden;
      background-color: black;
      color: white;
    }
    <div class="box1">
    </div>
    <div class="box2">
    </div>
    <div class="box3">
    </div>
    
    <div class="box">
      <span class="fade1"></span>
    </div>

    这里还有一个 codepen 为你。

    Chrome以我想要的方式渲染它,但是在Safari上结果不一样。它没有透明的“颜色”,而是白色或淡白色。

    enter image description here

    我知道transparent关键字在Safari上有不同的行为,所以我根本不使用它。

    你们有办法吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   gr4viton    6 年前

    可以。结果在Safari上,透明色应该和结束色有相同的rgb。

    所以:

    • 如果结束颜色为黑色,则开始颜色应为rgba(0,0,0,0)
    • 如果结束颜色为红色,则开始颜色应为rgba(255,0,0,0)

    .fade1 {
      position: absolute;
      top: 0;
      bottom: 0;
      right: 0;
      width: 75px;
      background: linear-gradient(
        to right, rgba(0,0,0,0), black
      );
    }
    
    .box {
      border: solid 1px #ccc;
      margin: 10px;
      max-width: 500px;
      height: 40px;
      position: relative;
      font-size: 30px;
      white-space: nowrap;
      overflow: hidden;
      background-color: black;
      color: white;
    }
    <div class="box">
      adsfasdfsdafasdfdasfdasfasdfdasfdasfdsfdasf
      <span class="fade1"></span>
    </div>