【发布时间】:2018-05-13 02:12:07
【问题描述】:
我想将图像添加到 SVG 路径。我在某种程度上尝试过,但它是一个半生不熟的代码,并且图像没有采用适当的 SVG 形状。
这是它的 HTML:
<div class="top-fold">
<div class="curve-img" id="shapeContainer">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1280" height="564" viewBox="0 0 1280 564" preserveAspectRatio="none">
<defs>
<rect id="bg-a" width="1280" height="575"/>
<linearGradient id="bg-b" x1="50%" x2="71.552%" y1="-4.763%" y2="100%">
<stop offset="0%" stop-color="#FAFBFC"/>
<stop offset="100%" stop-color="#EBF3F9"/>
</linearGradient>
</defs>
<g fill="none" fill-rule="evenodd">
<mask id="bg-c" fill="#fff">
<use xlink:href="#bg-a"/>
</mask>
<path fill="url(#bg-b)" d="M0,0 L1280,0 L1280,465.166826 C1282.16971,484.337191 1283.83637,494.281583 1285,495 C1358.96463,540.665449 1275.63125,544.202225 1034,558.75895 C786.666667,573.659188 442,550.377565 0,488.914081 L0,0 Z" mask="url(#bg-c)"/>
</g>
</svg>
</div><!-- curve-img end -->
</div> <!-- top fold end -->
这是相关的 CSS。
.top-fold {
width: 100%;
position: relative;
height: 80vh;
}
.curve-img {
position: absolute;
height: 100%;
width: 100%;
background-size: cover;
top: 0;
left: 0;
z-index: 4;
}
#shapeContainer {
width: 100%;
height: 100%;
margin: auto;
background-image: linear-gradient(
to bottom,
rgba(255, 159, 63, 0.2),
rgba(255, 159, 63, 0.8)
),
url(https://www.nature.org/cs/groups/webcontent/@web/documents/media/2016-photocontest-yosemite-w-1.jpg);
background-size: cover;
background-position: center top;
overflow: hidden;
}
#shapeContainer svg {
display: block;
width: 100%;
height: 100%;
}
@media screen and (max-height: 400px) {
#shapeContainer {
background-size: auto 150%;
}
}
@media screen and (max-height: 200px) {
#shapeContainer {
background-size: auto 200%;
}
}
@media screen and (max-height: 100px) {
#shapeContainer {
background-size: auto 300%;
}
}
这是供您参考的 codepen 链接。 https://codepen.io/enworl/pen/xPyMxL
提前感谢您的帮助。
【问题讨论】:
-
可能重复。请看:stackoverflow.com/questions/3796025/…。我认为接受的答案会对您有所帮助
-
使用面具应该可以。但是,尚不清楚您希望最终结果是什么样子。您可以为我们发布样机图片吗?
-
抱歉,@PaulLeBeau 回复晚了,我正在努力实现这一目标。请检查此链接。 drive.google.com/file/d/1SnhmP8aSKOMg9O0zWvsMHLJWf381jRW_/…