【问题标题】:background image, linear gradient jagged edged result needs to be smooth edged背景图像,线性渐变锯齿状边缘结果需要平滑边缘
【发布时间】:2016-01-10 12:55:53
【问题描述】:

我正在尝试使图像的底部变尖。我试图通过在底部生成两个三角形来获得这种效果。他们必须反应灵敏。在互联网上搜索了很多不符合我要求的示例之后,这是迄今为止我设法制作的最好的示例。

body,
html {
  height: 100%
}
.image {
  width: 1410px;
  margin-right: auto;
  margin-left: auto;
  height: 500px;
  overflow: hidden;
  position: relative;
}
.pointer {
  height: 50px;
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
}
.triangleWrapper {
  width: 50%;
  height: 50px;
  float: left;
}
.lefttriangle {
  width: 100%;
  height: 10px;
  left: 0px;
  top: 0px;
  background-image: linear-gradient(to right top, #ffffff 50%, transparent 50%);
}
.righttriangle {
  width: 100%;
  height: 10px;
  right: 0px;
  top: 0px;
  background: linear-gradient(to left top, #ffffff 50%, transparent 50%)
}
<div class="image">
  <img src="http://placekitten.com/1410/500">
  <div class="pointer">
    <div class="triangleWrapper">
      <div style="height: 100%;" class="lefttriangle"></div>
    </div>
    <div class="triangleWrapper">
      <div style="height: 100%;" class="righttriangle"></div>
    </div>
  </div>
</div>

CodePen Demo

它完全按照我想要的方式工作,因为它无需媒体查询即可响应。但是它在不是 90 度的三角形线上有一个锯齿状边缘。

如何在大多数(如果不是所有)现代浏览器中生成流畅的线条?我不是要求向后兼容。

非常感谢任何帮助!

【问题讨论】:

    标签: css responsive-design css-shapes linear-gradients


    【解决方案1】:

    不幸的是,当我们使用有角度的 linear-gradient 图像时总是会发生这种情况,而目前克服这种行为的唯一方法似乎是避免硬停止颜色(也就是说,不要将一种颜色的停止点作为下一种颜色的起点)。让第二个颜色开始离第一个颜色的停止点稍远会产生一个模糊的区域,使它看起来更平滑。这仍然不是 100% 完美,但比锯齿状边缘要好。

    .lefttriangle {
      width: 100%;
      height: 10px;
      left: 0px;
      top: 0px;
      background-image: linear-gradient(to right top, #ffffff 48%, transparent 50%); /* note the change of stop and start points */
    }
    .righttriangle {
      width: 100%;
      height: 10px;
      right: 0px;
      top: 0px;
      background: linear-gradient(to left top, #ffffff 48%, transparent 50%);  /* note the change of stop and start points */
    }
    

    body,
    html {
      height: 100%
    }
    .image {
      width: 1410px;
      margin-right: auto;
      margin-left: auto;
      height: 500px;
      overflow: hidden;
      position: relative;
    }
    .pointer {
      height: 50px;
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
    }
    .triangleWrapper {
      width: 50%;
      height: 50px;
      float: left;
    }
    .lefttriangle {
      width: 100%;
      height: 10px;
      left: 0px;
      top: 0px;
      background-image: linear-gradient(to right top, #ffffff 48%, transparent 50%);
    }
    .righttriangle {
      width: 100%;
      height: 10px;
      right: 0px;
      top: 0px;
      background: linear-gradient(to left top, #ffffff 48%, transparent 50%);
    }
    <div class="image">
      <img src="http://placekitten.com/1410/500">
      <div class="pointer">
        <div class="triangleWrapper">
          <div style="height: 100%;" class="lefttriangle"></div>
        </div>
        <div class="triangleWrapper">
          <div style="height: 100%;" class="righttriangle"></div>
        </div>
      </div>
    </div>

    替代实现:

    剪辑路径:您也可以使用clip-path 功能来产生类似的效果。使用clip-path 的优势在于它既反应灵敏,又能产生透明切割。 The SVG based clip-path has better browser support than the CSS version。但 IE 尚不支持此功能。

    body,
    html {
      height: 100%
    }
    .image {
      width: 1410px;
      margin-right: auto;
      margin-left: auto;
      height: 500px;
      overflow: hidden;
      position: relative;
    }
    .css-clip {
      -webkit-clip-path: polygon(0% 0%, 0% 90%, 50% 100%, 100% 90%, 100% 0%);
      clip-path: polygon(0% 0%, 0% 90%, 50% 100%, 100% 90%, 100% 0%);
    }
    .svg-clip {
      -webkit-clip-path: url(#clipper);
      -moz-clip-path: url(#clipper);
      clip-path: url(#clipper);
    }
    <!-- CSS Clip-path - Lower browser support -->
    <div class="image css-clip">
      <img src="http://placekitten.com/1410/500">
    </div>
    
    <!-- SVG Clip-path - Better browser support -->
    
    <svg width="0" height="0">
      <defs>
        <clipPath clipPathUnits="objectBoundingBox" id="clipper">
          <path d="M0,0 0,0.9 0.5,1 1,0.9 1,0z" />
        </clipPath>
      </defs>
    </svg>
    <div class="image svg-clip">
      <img src="http://placekitten.com/1410/500">
    </div>

    使用 CSS 变换:您也可以尝试使用this answer 中提到的方法。它在左侧实现了尖头效果,但应该很容易调整它以在底部创建尖头效果。

    body,
    html {
      height: 100%
    }
    .image {
      width: 1410px;
      margin-right: auto;
      margin-left: auto;
      height: 500px;
      overflow: hidden;
      position: relative;
    }
    .top-container,
    .bottom-container {
      position: absolute;
      bottom: 0px;
      height: 100%;
      width: 50%;
      overflow: hidden;
      backface-visibility: hidden;
    }
    .top-container {
      left: 0px;
      transform-origin: right bottom;
      transform: skewY(10deg);
    }
    .bottom-container {
      right: 0px;
      transform-origin: left bottom;
      transform: skewY(-10deg);
      background-position: 0% 100%;
    }
    .top-container:after,
    .bottom-container:after {
      position: absolute;
      content: '';
      height: 100%;
      width: 100%;
      bottom: -62px;  /* tan(10) * (width/2) / 2 */
      background: url(http://placekitten.com/1410/500);
      background-size: 200% 100%;
    }
    .top-container:after {
      left: 0px;
      transform: skewY(-10deg);
    }
    .bottom-container:after {
      right: 0px;
      transform: skewY(10deg);
      background-position: 100% 0%;
    }
    <div class="image">
      <div class='top-container'></div>
      <div class='bottom-container'></div>
    </div>

    【讨论】:

    • 谢谢你,Harry,我会看看 CSS 变换路径,剪辑路径效果很好,但我仍然需要 IE 支持 :( 一旦我产生了我需要的结果,我会更新这个问答。
    • Harry 我玩过您提供的链接中建议的转换代码。看到link你有什么进一步的建议,因为它没有像他们的sn-ps那样产生干净的线条。
    • 实际上它有 chrome。但不是 safari.. 任何针对 safari 的反分析修复?
    • @user3072439:请参考我刚才添加到答案中的sn-p(最后一个)。它应该适用于所有浏览器,如果不能,那么恐怕除了剪辑路径之外别无选择。
    • 对我来说 48% 和 50% 太模糊了,因为我试图画一条直线。 49.9% 和 50.1% 工作正常。但是,当您追求完美时,您的眼睛会看到边缘处有这个微小的切口。
    【解决方案2】:

    刚刚使用calc(50% - 1px)在codepen上找到了一个非常好的解决方案

    https://codepen.io/hellonico/pen/xEYXmL

    background: linear-gradient(7deg, currentColor calc(50% - 1px), transparent 50%);
    

    没有任何模糊,只有平滑的边缘

    编辑:.. 显然不在 Safari 中?..

    【讨论】:

    • 不错,但是这个计算技巧并没有为这个问题中使用的 sn-p 产生更好的输出。所以,似乎我们应该使用适合我们特定情况的任何一个。
    • 我在 Safari 和 Edge 中使用 calc(50% - 1px) 时也遇到了问题。我见过一些分辨率,但老实说,在所有浏览器中只做 49.9% 看起来都很棒。
    • 在 firefox 和 calc 中测试过看起来更好 0.9%
    猜你喜欢
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多