【问题标题】:Triangle after header that matches header background gradient与标题背景渐变匹配的标题后的三角形
【发布时间】:2018-09-11 21:41:57
【问题描述】:

我必须制作这样的标题:

我用这个渐变制作标题没有问题。我也知道如何用::after 定位一个三角形。但是,如何使三角形颜色与标题背景相匹配?问题是梯度不是静态的。例如,如果我在智能手机中打开页面,由于屏幕较短,渐变会比在显示器中打开时更“紧凑”。所以箭头颜色不能是静态的。我不知道我该怎么做,有人可以帮助我吗?谢谢。

【问题讨论】:

    标签: css linear-gradients


    【解决方案1】:

    您可以使用clip-path 来屏蔽标题:

    header {
      height: 70px;
      width: 100%;
      clip-path: polygon(0 0, 100% 0, 100% 60px, calc(100% - 40px) 60px, calc(100% - 60px) 70px, calc(100% - 80px) 60px, 0 60px);
      background-image: linear-gradient(to right, #3f5efb, #fc466b);
    }
    <header></header>

    使用此选项,您将不得不为不支持 clip-path 的浏览器提供备用。

    【讨论】:

      【解决方案2】:

      这是另一个在不使用clip-path 的情况下更受支持的想法:

      .box {
        height:50px;
        background:linear-gradient(to right,purple,red);
        background-attachment:fixed; /*This will do the magic*/
        position:relative;
      }
      .box:before {
        content:"";
        position:absolute;
        width:20px;
        height:20px;
        background:linear-gradient(to right,purple,red);
        background-attachment:fixed; /*This will do the magic*/
        transform:rotate(45deg);
        right:20px;
        bottom:-10px;
        animation:slide 5s infinite alternate linear;
      }
      
      @keyframes slide {
        from {
           right:20px;
        }
        to {
          right:calc(100% - 40px);
        }
      }
      <div class="box"></div>

      更新

      由于background-attachment:fixed 的错误和transform 的使用,上述解决方案不适用于Firefox。这是另一个应该可行的想法:

      .box {
        height:50px;
        background:linear-gradient(to right,purple,red);
        background-attachment:fixed; /*This will do the magic*/
        position:relative;
      }
      .box:before {
        content:"";
        position:absolute;
        width:20px;
        height:10px;
        background:
        linear-gradient(to right,purple,red);
        background-attachment:fixed; /*This will do the magic*/
        right:20px;
        bottom:-10px;
        animation:slide 5s infinite alternate linear;
      }
      .box:after {
        content:"";
        position:absolute;
        z-index:2;
        width:20px;
        height:10px;
        background:
        linear-gradient(to bottom right,transparent 50%,white 51%)100% 0/50% 100% no-repeat,
        linear-gradient(to bottom left,transparent 50%,white 51%)0 0/50% 100% no-repeat;
        right:20px;
        bottom:-10px;
        animation:slide 5s infinite alternate linear;
      }
      
      @keyframes slide {
        from {
           right:20px;
        }
        to {
          right:calc(100% - 40px);
        }
      }
      <div class="box"></div>

      【讨论】:

      • 好吧,这里没用。钻石渐变是静态的。
      • @MateusFelipe 你在用什么?
      • 火狐 59.0.2.
      • @MateusFelipe true ...在 chrome 中它工作正常...也许我们发现了一个错误 :) 会调查它...它似乎与变换有关
      • @MateusFelipe 好吧,这似乎是一个已知问题stackoverflow.com/questions/39633745/… ...最后我的解决方案没有得到更多支持:p ...但我会找到解决方法;)跨度>
      猜你喜欢
      • 1970-01-01
      • 2017-05-07
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      • 2013-03-04
      相关资源
      最近更新 更多