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

在带有边框的一侧倾斜背景图像

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

    我基本上要创建的布局如下:

    enter image description here

    下面是我用当前设置制作的一个快速JSFiddle:

    https://jsfiddle.net/silvawebdesigns/b2ae0k69/11/

    #about-gallery {
      margin: 60px 0;
      display: -ms-flex;
      display: -webkit-flex;
      display: flex;
      flex-wrap: wrap;
      margin-bottom: 500px;
      width: 100%;
    }
    
    .about-img-wrapper {
      position: relative;
      width: 100%;
      display: -ms-flex;
      display: -webkit-flex;
      display: flex;
      flex-wrap: wrap;
      margin: 5px;
    }
    
    #about-gallery .about-bg {
      border: solid 10px black;
      float: left;
      width: 100%;
      height: 300px;
      background-size: cover;
      background-position: center;
    }
    
    #about-gallery .image-1 {
      width: -moz-calc(55% - 10px);
      width: -webkit-calc(55% - 10px);
      width: calc(55% - 10px);
    }
    
    #about-gallery .image-2 {
      width: -moz-calc(45% - 10px);
      width: -webkit-calc(45% - 10px);
      width: calc(45% - 10px);
    }
    
    #about-gallery .image-4,
    #about-gallery .image-5 {
      width: -moz-calc(50% - 10px);
      width: -webkit-calc(50% - 10px);
      width: calc(50% - 10px);
    }
    <div id="about-gallery">
      <div class="about-img-wrapper img-about image-1">
        <div class="about-bg" style="background-image: url(https://via.placeholder.com/1140x400.png?text=SKEW+THIS+IMAGE+ON+ONE+SIDE);"></div>
      </div>
      <div class="about-img-wrapper img-about image-2">
        <div class="about-bg" style="background-image: url(https://via.placeholder.com/1140x400.png?text=SKEW+THIS+IMAGE+ON+ONE+SIDE);"></div>
      </div>
      <div class="about-img-wrapper img-about image-3">
        <div class="about-bg" style="background-image: url(https://via.placeholder.com/1140x400.png?text=SKEW+THIS+IMAGE+ON+ONE+SIDE);"></div>
      </div>
      <div class="about-img-wrapper img-about image-4">
        <div class="about-bg" style="background-image: url(https://via.placeholder.com/1140x400.png?text=SKEW+THIS+IMAGE+ON+ONE+SIDE);"></div>
      </div>
      <div class="about-img-wrapper img-about image-5">
        <div class="about-bg" style="background-image: url(https://via.placeholder.com/1140x400.png?text=SKEW+THIS+IMAGE+ON+ONE+SIDE);"></div>
      </div>
    
    </div>

    我试着用正反两方面的结合来达到这个目的,

    -webkit-transform: skew(20deg);
       -moz-transform: skew(20deg);
         -o-transform: skew(20deg);
    

    1 回复  |  直到 6 年前
        1
  •  2
  •   Temani Afif    6 年前

    诀窍是正确地设置变换orgin、边界,然后应该很容易地应用这两个歪斜变换。您可能还需要一些负边距来校正一些空间。

    我使用CSS变量来简化代码,但这不是强制性的。

    #about-gallery {
      margin: 60px 0;
      display: flex;
      flex-wrap: wrap;
      --s:-8deg; /* The skew factor (the same for all)*/
    }
    
    .about-img-wrapper {
      position: relative;
      margin: 5px;
      overflow: hidden; /* Don't forget this !!*/
      flex-grow:1;
    }
    
    .about-img-wrapper > * {
      height: 300px;
      background-size: 0 0; /* Hide the image here */
      background-position: center;
      overflow: hidden; /* Don't forget this !!*/
      transform:skewX(var(--s,0deg));
    }
    /* We need skewY for the 3 last element */
    .about-img-wrapper:nth-child(n + 3) > *  {
      transform:skewY(var(--s,0deg));
    }
    /* The pseudo element will get the image and the inverse of skewing */
    .about-img-wrapper > *::before {
      content:"";
      display:block;
      width:100%;
      height:100%;
      background:inherit; /* inherit the inline image */
      background-size:cover; 
      transform-origin: inherit; /* inherit the same transform-origin */
      transform:skewX(calc(-1*var(--s,0deg)));
    }
    .about-img-wrapper:nth-child(n + 3) > *::before  {
      transform:skewY(calc(-1*var(--s,0deg)));
    }
    
    /**/
    .image-1 {
      border-left: solid 10px black;
      flex-basis:50%;
      margin-right:-15px;
    }
    
    .image-1>* {
      transform-origin: top;
      border: solid 10px black;border-left: none;
    }
    /**/
    .image-2 {
      border-right: solid 10px black;
      flex-basis:40%;
      margin-left:-15px;
    }
    .image-2>* {
      transform-origin: bottom;
      border: solid 10px black;border-right: none;
    }
    /**/
    .image-3 {
      border-top: solid 10px black;
      flex-basis:100%;
    }
    .image-3>* {
      transform-origin: left;
      border: solid 10px black;border-top: none;
    }
    /**/
    .image-4,
    .image-5{
      border-bottom: solid 10px black;
      flex-basis:40%;
      margin-top:-5vw;
    }
    .image-4>*,
    .image-5>* {
      transform-origin: right;
      border: solid 10px black;border-bottom: none;
    }
    .image-5 {
      margin-top:-12vw;
    }
    .image-5>* {
      height:calc(300px + 7vw); /* We need a bigger height here */
    }
    /**/
    * {
      box-sizing: border-box;
    }
    <div id="about-gallery">
      <div class="about-img-wrapper  image-1">
        <div style="background-image: url(https://picsum.photos/id/10/800/800);"></div>
      </div>
      <div class="about-img-wrapper image-2">
        <div style="background-image: url(https://picsum.photos/id/102/800/800);"></div>
      </div>
      <div class="about-img-wrapper  image-3">
        <div style="background-image: url(https://picsum.photos/id/123/800/800);"></div>
      </div>
      <div class="about-img-wrapper image-4">
        <div style="background-image: url(https://picsum.photos/id/14/800/800);"></div>
      </div>
      <div class="about-img-wrapper  image-5">
        <div style="background-image: url(https://picsum.photos/id/20/800/800);"></div>
      </div>
    
    </div>
    推荐文章