【问题标题】:Apply three CSS filters side by side on an image?在图像上并排应用三个 CSS 过滤器?
【发布时间】:2016-05-26 01:47:31
【问题描述】:

看看下面的 CodePen 演示:http://codepen.io/anon/pen/vLPGpZ

这是我的代码:

  body {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: url(http://lorempixel.com/900/300) no-repeat center center fixed;
  }

  body:before {
    left: 0;
    right: 0;
    content: "";
    position: fixed;
    height: 100%;
    width: 30%;
    background: url(http://lorempixel.com/900/300) no-repeat center center fixed;
    filter: sepia(1);
  }

您将看到应用了棕褐色过滤器。我想要的是在同一张图片上并排应用三个 CSS 过滤器。前 1/3 部分使用灰度滤镜,中间 1/3 部分使用棕褐色滤镜,最后 1/3 部分使用对比度滤镜。如何使用 jQuery 或 JavaScript 来实现这一点?

现在,即使是三分之一的部分也没有被 Sepia 正确覆盖。

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    编辑:我看到有人已经发布了答案,但我将发布我的答案,因为我的做法有点不同。

    如果您想让它们正确划分,您需要将其拆分为每个过滤器的不同元素。您还需要让每个元素都有自己的背景设置,以便过滤器可以应用于图像的自己的部分,并且您需要相应地设置位置(您不必担心请求,因为它会只请求一次背景图像)。

    您的第一部分不是 1/3 的原因也是因为您将其设置为 30%,而 1/3 在技术上是 33.33%(当然是重复的)。

    我发了codepen here

    .container {
       position: relative;
       width: 900px;
       height: 300px;
    }
    
    .filter-section {
      float: left;
      width: 33.333333333%;
      height: 100%;
      background: url(http://lorempixel.com/900/300) no-repeat;
    }
        
    
    .grayscale {
      -webkit-filter: grayscale(1);
      filter: grayscale(1);
      background-position: left;
    }
    
    .sepia {
      -webkit-filter: sepia(1);
      filter: sepia(1);
      background-position: center;
    }
    
    .contrast {
      -webkit-filter: contrast(200%);
      filter: contrast(200%);
      background-position: right;
    }
    <div class="container">
      <div class="grayscale filter-section"></div>
      <div class="sepia filter-section"></div>
      <div class="contrast filter-section"></div>
    </div>

    【讨论】:

      【解决方案2】:

      给你,JSFiddle

          body {
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            background: url(http://lorempixel.com/900/300) no-repeat center center fixed;
            -webkit-filter: contrast(1);
            filter: contrast(1);
          }
      
      body:before {
          right: 0;
          top: 0;
          content: "";
          position: fixed;
          height: 100%;
          width: 33%;
          background: url(http://lorempixel.com/900/300) no-repeat right center fixed;
          -webkit-filter: sepia(1);
          filter: sepia(1);
      }
      
      body:after {
          left: 0;
          top: 0;
          content: "";
          position: fixed;
          height: 100%;
          width: 33%;
          background: url(http://lorempixel.com/900/300) no-repeat left center fixed;
          -webkit-filter: grayscale(1);
          filter: grayscale(1);
      }
      

      或者这个宽度重复的背景:

      JSFiddle

      【讨论】:

      • 有没有办法应用三个以上的过滤器?
      • 在这种情况下,我不得不说 No ,在这个例子中我使用了伪元素,它每个元素只有两个额外的元素,afterbefore ,但您可以忘记此方法,例如在您的body 中使用 6 div 并将过滤器提供给它们。
      • 我不得不接受另一个答案,因为它是可扩展的:)。
      【解决方案3】:

      另一种方法是使用混合模式而不是过滤器。

      这样,您在设置布局时有更大的灵活性,(例如,您可以将图像设置为具有大小的背景:封面)

      您只需要知道这些过滤器的混合模式等效项

      .base {
        position: absolute;
        left: 0px;
        top: 0px;
        width: 600px;
        height: 300px;
        background-image: url(http://lorempixel.com/400/200);
        background-size: cover;
      }
      
      .filter {
        position: absolute;
        left: 0px;
        top: 0px;
        width: 600px;
        height: 300px;
      }
      
      .filter div {
        width: calc(100% / 3);
        height: 100%;
        position: absolute;
        top: 0px;
      }
      
      .gray {
        background-color: gray;
        mix-blend-mode: color;
        left: 0px;
      }
      
      .sepia {
        background-color: rgb(112, 66, 20);
        mix-blend-mode: color;
        left: calc(100% / 3);
      }
      
      .contrast {
        background-color: hsl(128,100%,50%);
        mix-blend-mode: saturation;
        left: calc(200% / 3);
      }
      <div class="base"></div>
      <div class="filter">
      <div class="gray"></div>
      <div class="sepia"></div>
      <div class="contrast"></div>
      </div>

      【讨论】:

      【解决方案4】:

      我看到有人已经回复了,但是我觉得如果你让它负责,效果会更好。为此,请添加以下 HTML

      <div class="container">
        <div class="wrapper grayscale">
          <div class="picture"></div>
        </div>
        <div class="wrapper original">
          <div class="picture"></div>
        </div>
        <div class="wrapper sepia">
          <div class="picture"></div>
        </div>
      </div>
      

      还有 CSS

      .container {
        height: 300px;
        max-width: 900px;
        margin: 100px 0 0 0;
        position: relative;
        width: 100%;
      
      }
      
      .wrapper {
        height: 300px;
        float: left;
        max-width: 900px;
        overflow: hidden;
        position: relative;
        width: 33.3%;
      }
      
      .picture {
        background-image: url(http://lorempixel.com/900/300);
        background-repeat: no-repeat;
        background-size: 900px;
        background-position: left 100px;
        background-attachment: fixed;
        height: 300px;
        left: 0;
        position: absolute;
        top: 0;
        width: 900px;
      }
      
      .grayscale .picture {
        -webkit-filter: grayscale(1);
        filter: grayscale(1);
      }
      
      .picture.original {
      }
      
      .sepia .picture {
        -webkit-filter: sepia(1);
        filter: sepia(1);
      } 
      

      主要技巧是使用 css 属性来固定背景。

      background-attachment: fixed;
      

      请看示例:http://codepen.io/guilhermelucio/pen/obVLpd

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-20
        • 2010-10-19
        • 2021-04-08
        • 2015-06-17
        • 1970-01-01
        • 1970-01-01
        • 2021-11-11
        相关资源
        最近更新 更多