gpt4 book ai didi

css - 对 SVG 渐变和底层图像的不同响应?

转载 作者:行者123 更新时间:2023-11-28 00:41:04 26 4
gpt4 key购买 nike

我需要在 svg 图像上放置一个透明渐变,使图像从中心到最右侧逐渐变为透明(垂直渐变)。

Like this .

父元素有background-color: red;

问题是我需要渐变来响应父元素大小的变化(比如调整浏览器大小时),同时保持其相对于父元素的比例,并继续淡化底层图像。

底层图像的行为必须类似于 CSS 中的 background-size: cover;。我发现 preserveAspectRatio="xMidYMid slice" 可以很好地实现这一点,但我不希望渐变以同样的方式表现。即使父元素的大小发生变化,它也必须始终占据父元素大小的相同百分比。如果 preserveAspectratio="none"width="100%"height="100%" 在渐变上,它可能会这样做。我已经尝试过了,但它不起作用。

我现在的方式是,当您水平调整浏览器窗口大小时,渐变变得越来越隐藏。在这里查看:CodePen .

我尝试将渐变分离到一个单独的 svg 中,认为它会建立一个新的视口(viewport),允许我插入 preserveAspectratio="none" 这将拉伸(stretch)渐变以适应父元素的大小,但没有也工作。

这是另一种思考方式:CodePen .我想将顶部示例的背景行为与底部示例的渐变行为结合起来。

有什么想法吗?

最佳答案

您可以通过过滤器将彩色封面移动到 div 包装器中并完成您想要的。请注意,此特定实现不适用于 Firefox,因为它不支持 feImage 中的片段引用。您可以通过将我填充的矩形转换为 feImage(Firefox 支持)的自包含数据 URI/svg+xml 来使此 Firefox 兼容。

<div id="main" style="filter: url(#half-cover);">
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="361.42084mm"
height="74.083336mm"
viewBox="0 0 361.42083 74.083337"
preserveAspectRatio="xMidYMid slice"
id="svg1">

<defs>

<linearGradient id="linearGradient33" gradientUnits="objectBoundingBox" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" id="stop1159" stop-color="red" stop-opacity="0"/>
<stop offset="70%" id="stop1159" stop-color="red" stop-opacity="0"/>
<stop offset="100%" id="stop1161" stop-color="red" stop-opacity="1"/>
</linearGradient>

<rect id="rectONE" x="0%" y="0%" width="100%" height="100%" fill="url(#linearGradient33)"/>

<filter id="half-cover" primitiveUnits="objectBoundingBox" x="0%" y="0%" width="100%" height="100%">
<feImage x="0%" y="0%" width="100%" height="200%" xlink:href="#rectONE"/>
<feComposite operator="over" in2="SourceGraphic"/>
</filter>
</defs>



<image y="1.4210855e-014"
x="1.4210855e-014"
preserveAspectRatio="none"
height="74.083336"
width="361.42084"
fill="#fff"
xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABVYAAAEYCAYAAAC3JK0dAAAZgUlEQVR4nO3dT48kR5kH4Mjs6mH+ iBVeDvhiWVgIs4I9APIKCa1YceQLcOUrIO4gcUMcLe6IzzAXC8lgrYw4sMDBQgvLYht5JST8hxl7 PJjuqtxDV2RFZmV311vT3VlZ9TwSmpns6qo3IyIj3/z1eKj+8Ic/NGliTk9P147NZrMRKnly3/ve 9zp/bpom/ftXv3otnzU0bnfu3LmWz4L/+PrXxy5hEr773e+uHSv3ANct1+Hx48drx8a+j/7nq6+u Hbuu++FV2Kde5CZtu6cNrY/vf//7V1LT1Fx23zgk//Txj6cvvfDC2GVM3lWvqSallJoNHzGrKlVb f9L1OLf+qmqPV9U4VTfnjGtVValJaefG8qpdtLbynJTjkF+56biM2YtEa42+73lret/XzFUqe5i8 Vsoe5pDuxbv4LDGWpmnS7Pj4eOw6wqZY83lOTk5u7LOGFvpNfj6HZZ+u0+s0FDKUXLdch11sfC67 FnbNLo7hFGy7pw018IdqaCwO9b4wn8/1G1fgqvffKqWzEHKiLqx/5PO6KNCd7ohvbpO1VZ3z+02M 2Ytc1/y17zvha3JXDPUw5T35kO7F+uCVqqpSPXYRAAAAUVXtUQYAGJduBAAAAAAgSLAKAAAAABAk WAUAAAAACBKsAgAAAAAECVYBAAAAAIIEqwAAAAAAQYJVAAAAAIAgwSoAAAAAQJBgFQAAAAAgSLAK AAAAABAkWAUAAAAACBKsAgAAAAAECVYBAAAAAIIEqwAAAAAAQYJVAAAAAIAgwSoAAAA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63dFy/qpyf8g/sDn7Qnvfa8e5 /KFbr542oC7HPBX3/DK8S909INfRn5f23l+edx7v5X6XqrOwv/3asr9p76t5vyg+Z22+irXX2Qdz HeWYVqsfSPev8/LX9vwH6i772Cqt7gGdazbXk/eooodqe8o8h0XtzXLcyvHPv+a5zvPR7/U6/XBv D0lp9QwytF/310Fnz226wdhR/kF+fq/yntO7tvN79/OZdn7yD40H7rWd/aK3Hw3tkx0D19JaXxbQ WSvLmsp9v1xv/TwnEuJe9Dza+VPxeeV1mIq12fZ6zSpbyWsk7xFr62GLcWlrHtpbir1nbTxCn7Sd fp9a9rKpOHaR/wc8OgTXo7mNbgAAAABJRU5ErkJggg=="/>


</svg>
</div>

关于css - 对 SVG 渐变和底层图像的不同响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53838299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com