Aks*_*hot 10 javascript css svg
SVG 中opacityvs 的区别是什么fill-opacity?
我提到了文档填充不透明度和不透明度,但我很困惑fill-opacity: opacity of the content of the current objectvs的意思opacity: transparency of an object
Har*_*rry 20
区别正是名称所示的差异:).fill-opacity仅适用于fill该元件的(或者换句话说,只是它的背景),stroke-opacity仅适用于stroke而opacity适用于这两者.
这opacity是一种后处理操作.也就是说,渲染元素(或组)作为整体(填充+笔划),然后基于不透明度设置调整透明度,而fill-opacity和stroke-opacity是中间步骤,因此透明度单独添加到笔触和填充.因此,当stroke-opacity和fill-opacity它们一起使用时,结果仍然与使用相同opacity(因为填充的透明度将使填充颜色显示在它们重叠的任何地方).您可以在下面的前两个元素中直观地看到差异.
此外,通过罗伯特(在评论)所示,fill-opacity不适用于image而opacity做的工作.
svg {
width: 100vw;
height: 100vh;
}
body {
background: url(http://lorempixel.com/600/600/nature/1);
height: 100vh;
}
polygon {
stroke-width: 3;
}Run Code Online (Sandbox Code Playgroud)
<svg viewBox='0 0 40 190'>
<polygon points='10,10 30,10 30,30 10,30' fill='steelblue' stroke='crimson' opacity='0.5' />
<polygon points='10,40 30,40 30,60 10,60' fill='steelblue' stroke='crimson' fill-opacity='0.5' stroke-opacity='0.5' />
<polygon points='10,70 30,70 30,90 10,90' fill='steelblue' stroke='crimson' fill-opacity='0.5' />
<polygon points='10,100 30,100 30,120 10,120' fill='steelblue' stroke='crimson' stroke-opacity='0.5' />
<image xlink:href='http://lorempixel.com/100/100/nature/3' x='8.5' y='130' width='23' height='23' stroke='crimson' opacity='0.5' />
<image xlink:href='http://lorempixel.com/100/100/nature/3' x='8.5' y='160' width='23' height='23' stroke='crimson' fill-opacity='0.5' />
</svg>Run Code Online (Sandbox Code Playgroud)
在CSS世界中,您可以将其视为下面代码段中的内容.(请注意,我并不是说它们是相同的,SVG和CSS之间存在细微差别.它只是试图解释这些SVG属性在CSS中会转换为什么.)
div {
height: 20vh;
width: 20vh;
margin: 30px auto;
font-family: Verdana;
font-size: 2vw;
}
div:nth-of-type(1) {
opacity: 0.5;
background: rgba(70, 130, 180, 1);
border: .35vw solid rgba(220, 20, 60, 1);
}
div:nth-of-type(2) {
background: rgba(70, 130, 180, 0.5);
border: .35vw solid rgba(220, 20, 60, 1);
}
div:nth-of-type(3) {
background: rgba(70, 130, 180, 1);
border: .35vw solid rgba(220, 20, 60, 0.5);
}
body {
background: url(http://lorempixel.com/600/600/nature/1);
height: 100vh;
}Run Code Online (Sandbox Code Playgroud)
<div></div>
<div></div>
<div></div>Run Code Online (Sandbox Code Playgroud)
除了影响每个单个元素的哪个部分(例如填充与笔触)之外,另一个区别是元素在组中重叠时会发生什么。opacity影响整个组的透明度,同时fill-opacity影响组内各个元素的透明度。这样的一个结果是,当此类组中的元素重叠时,fill-opacity在使用时重叠区域中会产生复合效果,而在使用时opacity则不会。当对组内的每个元素或组本身应用(填充)不透明度0.5时,在下图中演示了这一点。
<svg height="200">
<g transform="translate(0, 0)">
<rect x="10" y="10" width="40" height="40" fill-opacity="0.5"/>
<rect x="30" y="30" width="40" height="40" fill-opacity="0.5"/>
</g>
<g transform="translate(80, 0)" fill-opacity="0.5">
<rect x="10" y="10" width="40" height="40"/>
<rect x="30" y="30" width="40" height="40"/>
</g>
<g transform="translate(0, 80)">
<rect x="10" y="10" width="40" height="40" opacity="0.5"/>
<rect x="30" y="30" width="40" height="40" opacity="0.5"/>
</g>
<g transform="translate(80, 80)" opacity="0.5">
<rect x="10" y="10" width="40" height="40"/>
<rect x="30" y="30" width="40" height="40"/>
</g>
<text transform="translate(170,45)">fill-opacity</text>
<text transform="translate(170,125)">opacity</text>
<text transform="translate(10,175)">applied to</text>
<text transform="translate(0,190)">each element</text>
<text transform="translate(90,175)">applied to</text>
<text transform="translate(103,190)">group</text>
</svg>Run Code Online (Sandbox Code Playgroud)