css – 背景图像,线性渐变锯齿状边缘结果需要平滑边缘

前端之家收集整理的这篇文章主要介绍了css – 背景图像,线性渐变锯齿状边缘结果需要平滑边缘前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让图像的底部指向.我试图通过在底部生成两个三角形来获得此效果.他们必须有回应.在搜索了整个互联网之后,有很多例子对我的要求不起作用,这是迄今为止我设法生产的最好的例子.
  1. body,html {
  2. height: 100%
  3. }
  4. .image {
  5. width: 1410px;
  6. margin-right: auto;
  7. margin-left: auto;
  8. height: 500px;
  9. overflow: hidden;
  10. position: relative;
  11. }
  12. .pointer {
  13. height: 50px;
  14. position: absolute;
  15. bottom: 0;
  16. left: 0;
  17. width: 100%;
  18. }
  19. .triangleWrapper {
  20. width: 50%;
  21. height: 50px;
  22. float: left;
  23. }
  24. .lefttriangle {
  25. width: 100%;
  26. height: 10px;
  27. left: 0px;
  28. top: 0px;
  29. background-image: linear-gradient(to right top,#ffffff 50%,transparent 50%);
  30. }
  31. .righttriangle {
  32. width: 100%;
  33. height: 10px;
  34. right: 0px;
  35. top: 0px;
  36. background: linear-gradient(to left top,transparent 50%)
  37. }
  1. <div class="image">
  2. <img src="http://placekitten.com/1410/500">
  3. <div class="pointer">
  4. <div class="triangleWrapper">
  5. <div style="height: 100%;" class="lefttriangle"></div>
  6. </div>
  7. <div class="triangleWrapper">
  8. <div style="height: 100%;" class="righttriangle"></div>
  9. </div>
  10. </div>
  11. </div>

CodePen Demo

它完全符合我的要求,因为它无需媒体查询即可响应.但它在三角形线上的锯齿状边缘不是90度.

如果不是所有现代浏览器,如何在大多数现代浏览器中生成平滑线?我不是要求向后兼容性.

任何帮助是极大的赞赏!

解决方法

不幸的是,这总是发生在我们使用有角度的线性渐变图像时,目前克服这种行为的唯一方法似乎是避免颜色的硬停止(也就是说,不要将一种颜色的停止点作为开始点下一个).使第二种颜色从第一种颜色的停止点开始稍微远离会产生模糊区域并使其看起来更平滑.这仍然不是100%完美,但比锯齿状边缘更好.
  1. .lefttriangle {
  2. width: 100%;
  3. height: 10px;
  4. left: 0px;
  5. top: 0px;
  6. background-image: linear-gradient(to right top,#ffffff 48%,transparent 50%); /* note the change of stop and start points */
  7. }
  8. .righttriangle {
  9. width: 100%;
  10. height: 10px;
  11. right: 0px;
  12. top: 0px;
  13. background: linear-gradient(to left top,transparent 50%); /* note the change of stop and start points */
  14. }
  1. body,transparent 50%);
  2. }
  1. <div class="image">
  2. <img src="http://placekitten.com/1410/500">
  3. <div class="pointer">
  4. <div class="triangleWrapper">
  5. <div style="height: 100%;" class="lefttriangle"></div>
  6. </div>
  7. <div class="triangleWrapper">
  8. <div style="height: 100%;" class="righttriangle"></div>
  9. </div>
  10. </div>
  11. </div>

替代实施:

剪辑路径:您还可以使用剪辑路径功能来产生类似的效果.使用剪辑路径的优点是它既响应又产生透明剪切. The SVG based clip-path has better browser support than the CSS version.但IE尚未支持功能.

  1. body,html {
  2. height: 100%
  3. }
  4. .image {
  5. width: 1410px;
  6. margin-right: auto;
  7. margin-left: auto;
  8. height: 500px;
  9. overflow: hidden;
  10. position: relative;
  11. }
  12. .css-clip {
  13. -webkit-clip-path: polygon(0% 0%,0% 90%,50% 100%,100% 90%,100% 0%);
  14. clip-path: polygon(0% 0%,100% 0%);
  15. }
  16. .svg-clip {
  17. -webkit-clip-path: url(#clipper);
  18. -moz-clip-path: url(#clipper);
  19. clip-path: url(#clipper);
  20. }
  1. <!-- CSS Clip-path - Lower browser support -->
  2. <div class="image css-clip">
  3. <img src="http://placekitten.com/1410/500">
  4. </div>
  5.  
  6. <!-- SVG Clip-path - Better browser support -->
  7.  
  8. <svg width="0" height="0">
  9. <defs>
  10. <clipPath clipPathUnits="objectBoundingBox" id="clipper">
  11. <path d="M0,0 0,0.9 0.5,1 1,0.9 1,0z" />
  12. </clipPath>
  13. </defs>
  14. </svg>
  15. <div class="image svg-clip">
  16. <img src="http://placekitten.com/1410/500">
  17. </div>

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

  1. body,html {
  2. height: 100%
  3. }
  4. .image {
  5. width: 1410px;
  6. margin-right: auto;
  7. margin-left: auto;
  8. height: 500px;
  9. overflow: hidden;
  10. position: relative;
  11. }
  12. .top-container,.bottom-container {
  13. position: absolute;
  14. bottom: 0px;
  15. height: 100%;
  16. width: 50%;
  17. overflow: hidden;
  18. backface-visibility: hidden;
  19. }
  20. .top-container {
  21. left: 0px;
  22. transform-origin: right bottom;
  23. transform: skewY(10deg);
  24. }
  25. .bottom-container {
  26. right: 0px;
  27. transform-origin: left bottom;
  28. transform: skewY(-10deg);
  29. background-position: 0% 100%;
  30. }
  31. .top-container:after,.bottom-container:after {
  32. position: absolute;
  33. content: '';
  34. height: 100%;
  35. width: 100%;
  36. bottom: -62px; /* tan(10) * (width/2) / 2 */
  37. background: url(http://placekitten.com/1410/500);
  38. background-size: 200% 100%;
  39. }
  40. .top-container:after {
  41. left: 0px;
  42. transform: skewY(-10deg);
  43. }
  44. .bottom-container:after {
  45. right: 0px;
  46. transform: skewY(10deg);
  47. background-position: 100% 0%;
  48. }
  1. <div class="image">
  2. <div class='top-container'></div>
  3. <div class='bottom-container'></div>
  4. </div>

猜你在找的CSS相关文章