to7*_*7be 11 css svg css-shapes
我有一个箭头作为SVG,我想根据包装DIV的宽度改变它的长度(例如).
我之前的所有尝试都导致了这种行为(扩展整个SVG):
而不是这个,我想实现这种行为:
是否可以通过简单地在代码中添加百分比值来改变SVG内部的单个形状?如果没有,我怎么能这样做?
SVG代码:
<svg width="35px" viewBox="0 0 35 985" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="Group" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" transform="translate(0.000000, 0.000000)">
<rect id="Rectangle-2" fill="#5FDCC7" x="12" y="19.7987928" width="11" height="100%"></rect>
<polygon id="Triangle" fill="#5FDBC6" points="17.5 0 35 20.7887324 0 20.7887324"></polygon>
<rect id="Rectangle-3" fill="#5FDBC6" x="0" y="973.110664" width="35" height="11"></rect>
</g>
</svg>Run Code Online (Sandbox Code Playgroud)
Pau*_*eau 11
作为一般规则,没有办法创建可扩展的SVG,其中部分SVD不能扩展.如果SVG具有viewBox,并且您缩放它,则所有内容都将缩放.没有办法将某些内容标记为不受缩放影响.
最接近的是限制缩放到X轴.然而,箭头仍会伸展.
<svg width="35px" height="150px" viewBox="0 0 35 985" preserveAspectRatio="none">
<g id="Group" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" transform="translate(0.000000, 0.000000)">
<rect id="Rectangle-2" fill="#5FDCC7" x="12" y="19.7987928" width="11" height="100%"></rect>
<polygon id="Triangle" fill="#5FDBC6" points="17.5 0 35 20.7887324 0 20.7887324"></polygon>
<rect id="Rectangle-3" fill="#5FDBC6" x="0" y="973.110664" width="35" height="11"></rect>
</g>
</svg>
<svg width="35px" height="300px" viewBox="0 0 35 985" preserveAspectRatio="none">
<g id="Group" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" transform="translate(0.000000, 0.000000)">
<rect id="Rectangle-2" fill="#5FDCC7" x="12" y="19.7987928" width="11" height="100%"></rect>
<polygon id="Triangle" fill="#5FDBC6" points="17.5 0 35 20.7887324 0 20.7887324"></polygon>
<rect id="Rectangle-3" fill="#5FDBC6" x="0" y="973.110664" width="35" height="11"></rect>
</g>
</svg>Run Code Online (Sandbox Code Playgroud)
如果需要通用解决方案,则必须监视调整大小事件并动态更新SVG.
有限的偷偷摸摸的聪明解决方案......
说了这么多,有一种巧妙的技术可以在有限的情况下运作.限制是:
以下是使用此技术实现的形状演示.如果你水平调整窗口大小,你会看到它适当拉伸:
<svg width="100%" height="35px">
<svg viewBox="0 0 1 1" preserveAspectRatio="none">
<rect x="0" y="0" width="1" height="1" fill="white"/>
<rect x="0" y="0.4" width="1" height="0.2" fill="#5FDBC6"/>
</svg>
<svg viewBox="0 0 0.2 1" preserveAspectRatio="xMinYMid">
<rect x="0" y="0" width="0.2" height="1" fill="#5FDBC6"/>
</svg>
<svg viewBox="0 0 0.8 1" preserveAspectRatio="xMaxYMid">
<rect x="0" y="0" width="0.8" height="1" fill="white"/>
<polygon points="0,0 0.8,0.5 0,1" fill="#5FDBC6"/>
</svg>
</svg>Run Code Online (Sandbox Code Playgroud)
是的,你可以在svg中使用百分比.
对于基本形状,它非常简单.您可以将主矩形写为
<rect x="0" y="5" width="100%" height="10"/>
Run Code Online (Sandbox Code Playgroud)
你的第二个矩阵更简单,因为它始终位于0,0
<rect x="0" y="0" width="10" height="20"/>
Run Code Online (Sandbox Code Playgroud)
但是使用箭头时,在pathes和polygon中存在问题,您无法使用百分比.要解决这个问题,有两步解决方案.
首先将路径放在符号元素中:
<symbol id="arrow" viewBox="0 0 20 20" width="20" height="20">
<path d="M0,0L20 10L0 20z" />
</symbol>
Run Code Online (Sandbox Code Playgroud)
现在你可以定位这个符号,就像你会定位一个矩形......简单......
<use xlink:href="#arrow" x="100%" y="0" width="20" height="20"/>
Run Code Online (Sandbox Code Playgroud)
但现在你的箭头开始100%,完全在你的视口之外.你可以设置溢出:在svg上显示并完成它,但这不是我们想要的......我们希望箭头以100%结束.但这也很容易,我们知道箭头是20px宽.所以只需将使用量翻译成20px:
<use xlink:href="#arrow" x="100%" y="0" width="20" height="20" transform="translate(-20 0)"/>
Run Code Online (Sandbox Code Playgroud)
使用这种方法,您可以根据百分比在任何位置定位任何形状......
为了扭曲这一切,你的完整svg现在看起来像这样:
<svg id="svg" height="20px" xmlns:xlink="http://www.w3.org/1999/xlink">
<symbol id="arrow" viewBox="0 0 20 20" width="20" height="20">
<path d="M0,0L20 10L0 20z" />
</symbol>
<g id="Group" fill="#5FDCC7">
<rect x="0" y="5" width="100%" height="10" transform="translate(-20 0)" />
<rect x="0" y="0" width="10" height="20" />
<use xlink:href="#arrow" width="20" height="20" x="100%" y="0" transform="translate(-20 0)" />
</g>
</svg>
Run Code Online (Sandbox Code Playgroud)
这是一个使用这个svg有3种不同宽度的片段:
svg:nth-of-type(1) {
width: 100px
}
svg:nth-of-type(2) {
width: 200px
}
svg:nth-of-type(3) {
width: 300px
}Run Code Online (Sandbox Code Playgroud)
<svg height="20px" xmlns:xlink="http://www.w3.org/1999/xlink">
<symbol id="arrow" viewBox="0 0 20 20" width="20" height="20">
<path d="M0,0L20 10L0 20z" />
</symbol>
<g id="Group" fill="#5FDCC7">
<rect x="0" y="5" width="100%" height="10" transform="translate(-20 0)" />
<rect x="0" y="0" width="10" height="20" />
<use xlink:href="#arrow" width="20" height="20" x="100%" y="0" transform="translate(-20 0)" />
</g>
</svg>
<br/>
<svg height="20px" xmlns:xlink="http://www.w3.org/1999/xlink">
<symbol id="arrow" viewBox="0 0 20 20" width="20" height="20">
<path d="M0,0L20 10L0 20z" />
</symbol>
<g id="Group" fill="#5FDCC7">
<rect x="0" y="5" width="100%" height="10" transform="translate(-20 0)" />
<rect x="0" y="0" width="10" height="20" />
<use xlink:href="#arrow" width="20" height="20" x="100%" y="0" transform="translate(-20 0)" />
</g>
</svg>
<br/>
<svg height="20px" xmlns:xlink="http://www.w3.org/1999/xlink">
<symbol id="arrow" viewBox="0 0 20 20" width="20" height="20">
<path d="M0,0L20 10L0 20z" />
</symbol>
<g id="Group" fill="#5FDCC7">
<rect x="0" y="5" width="100%" height="10" transform="translate(-20 0)" />
<rect x="0" y="0" width="10" height="20" />
<use xlink:href="#arrow" width="20" height="20" x="100%" y="0" transform="translate(-20 0)" />
</g>
</svg>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3604 次 |
| 最近记录: |