【问题标题】:Image between SVGSVG 之间的图像
【发布时间】:2018-10-02 17:09:24
【问题描述】:

#home {
  height: 100vh;
  background-image: url("http://4.bp.blogspot.com/-yB6Tc3mleuE/T4NkAKeazYI/AAAAAAAACB0/tlichKzIu3Q/s1600/Simple+unique+odd+weird+wallpapers+minimalistic+%2330+battleship+titanic.jpg");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  overflow: hidden;
  padding: 0;
  margin-left: -10px;
}
.background {
  overflow: hidden;
}
#fg {
  fill: pink;
  stroke: #fff;
  stroke-width: 10px;
  stroke-dasharray: 1024px;
  -webkit-animation: dash 2s;
  animation: dash 2s;
}

@keyframes dash {
  from {
    stroke-dashoffset: 1024px;
  }
  to {
    stroke-dashoffset: 0px;
  }
}

@-webkit-keyframes dash {
  from {
    stroke-dashoffset: 1024px;
  }
  to {
    stroke-dashoffset: 0px;
  }
}

#bg {
  fill: white;
  stroke: none;
  transform-origin: 1270px 550px;
  -webkit-animation: bgfill 2s linear 2s forwards;
  animation: bgfill 2s linear 2s forwards;
}

@keyframes bgfill {
  from {
    transform: scale(1);
  }
  to {
    transform: scale(4);
  }
}

@-webkit-keyframes bgfill {
  from {
    transform: scale(1);
  }
  to {
    transform: scale(4);
  }
}
<div id="home" class="section" style="height:100vh;">
    <div class="background">
        <svg viewBox="0 0 1376 764">
            <defs>
                <path id="shape" d="M1034.5,770.5a125.59,125.59,0,0,0-17-32c-12.32-16.72-25.68-25.84-33-31-6-4.23-59.88-42.55-100-90-13.64-16.14-20.57-24.48-26-38-15-37.48-3.73-73.65,0-85,8.68-26.45,23-43.26,35-57,19-21.82,33.56-29.9,67-54,33.68-24.27,55.39-39.91,77-60,40.56-37.69,35.94-49.35,81-96,34.18-35.39,51.27-53.08,79-65,7.79-3.35,76-31.44,140,2a142.06,142.06,0,0,1,31,22l7.5,7.5  L 1376,770.5 Z" />
            </defs>
            <use xlink:href="#shape" id="bg" />
            <use xlink:href="#shape" id="fg" />
        </svg>
    </div>
</div>

我似乎无法在保持相同效果的同时始终使背景图像可见,使填充透明摆脱了动画,我还尝试在各种元素上使用 z-index 但没有成功,如何我可以让它在白线内而不是粉红色的svg内显示背景图像吗?

我还尝试将相同的图像应用到粉红色 SVG 作为填充,它有点工作,我似乎无法让图像看起来像全屏时那样,它也使页面有点慢:

#fg {
  fill: url(#image);
  stroke: #fff;
  stroke-width: 10px;
  stroke-dasharray: 1024px;
  -webkit-animation: dash 2s;
  animation: dash 2s;
}

<pattern id="image" width="1" height="1">
  <image xlink:href="http://4.bp.blogspot.com/-yB6Tc3mleuE/T4NkAKeazYI/AAAAAAAACB0/tlichKzIu3Q/s1600/Simple+unique+odd+weird+wallpapers+minimalistic+%2330+battleship+titanic.jpg"></image>
</pattern>

【问题讨论】:

    标签: html css svg


    【解决方案1】:

    您可以使用fill-opacity 摆脱粉红色。但是您需要调整覆盖背景图像的白色“背景”。

    您可能需要为此更改形状。

    您还可以将图像作为图层包含在 svg 中。

    #home {
      height: 100vh;
      background-image: url("http://4.bp.blogspot.com/-yB6Tc3mleuE/T4NkAKeazYI/AAAAAAAACB0/tlichKzIu3Q/s1600/Simple+unique+odd+weird+wallpapers+minimalistic+%2330+battleship+titanic.jpg");
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
      overflow: hidden;
      padding: 0;
      margin-left: -10px;
    }
    .background {
      overflow: hidden;
    }
    #fg {
      fill: pink;
      fill-opacity: 0;
      stroke: #fff;
      stroke-width: 10px;
      stroke-dasharray: 1024px;
      -webkit-animation: dash 2s;
      animation: dash 2s;
    }
    
    @keyframes dash {
      from {
        stroke-dashoffset: 1024px;
      }
      to {
        stroke-dashoffset: 0px;
      }
    }
    
    @-webkit-keyframes dash {
      from {
        stroke-dashoffset: 1024px;
      }
      to {
        stroke-dashoffset: 0px;
      }
    }
    
    #bg {
      fill: white;
      opacity: .5;
      stroke: none;
      transform-origin: 1270px 550px;
      -webkit-animation: bgfill 2s linear 2s forwards;
      animation: bgfill 2s linear 2s forwards;
    }
    
    @keyframes bgfill {
      from {
        transform: scale(1);
      }
      to {
        transform: scale(4);
      }
    }
    
    @-webkit-keyframes bgfill {
      from {
        transform: scale(1);
      }
      to {
        transform: scale(4);
      }
    }
    <div id="home" class="section" style="height:100vh;">
        <div class="background">
            <svg viewBox="0 0 1376 764">
                <defs>
                    <path id="shape" d="M1034.5,770.5a125.59,125.59,0,0,0-17-32c-12.32-16.72-25.68-25.84-33-31-6-4.23-59.88-42.55-100-90-13.64-16.14-20.57-24.48-26-38-15-37.48-3.73-73.65,0-85,8.68-26.45,23-43.26,35-57,19-21.82,33.56-29.9,67-54,33.68-24.27,55.39-39.91,77-60,40.56-37.69,35.94-49.35,81-96,34.18-35.39,51.27-53.08,79-65,7.79-3.35,76-31.44,140,2a142.06,142.06,0,0,1,31,22l7.5,7.5  L 1376,770.5 Z" />
                </defs>
                <use xlink:href="#shape" id="bg" />
                <use xlink:href="#shape" id="fg" />
            </svg>
        </div>
    </div>

    【讨论】:

    • 如果您想尝试 svg 中的图像,这里有一个链接:developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/SVG_Image_Tag
    • 我想保留白色填充,我编辑了代码,显示我尝试使用填充来显示相同​​的图像,但我不能让它只显示右下角图片
    • 你想在哪里填充白色?在形状之外?如果是这样,你需要为你的 BG 重写你的形状
    • 是的,在白线之外,如果我想实现它,你知道形状应该是什么样子吗?我画它没有问题,但我不知道它应该是什么样子
    • 你可以试试线条的形状,然后把描边增加到左上角...
    猜你喜欢
    • 2021-12-30
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2022-01-17
    • 2012-06-27
    相关资源
    最近更新 更多