我有两个svg,我正在尝试创建一个进度栏。第一个是曲线路径,但是,我已经设法让它使用stroke-dasharray="400 400"和stroke-dashoffset="-110" (在mask上使用它来填充进度)。效果很好。
我遇到的问题是直线,我想创建一个类似的进度条,它可以填充进度,但它不起作用,我一直在试图找出原因,可能已经有3个小时了……改变stroke-dasharray和stroke-dashoffset我无法得到掩码来填充点。我不知道我做错了什么。
以下是我想要达到的效果的参考

我附上了我的密码:
body {
background: #171B42;
}
.locked {
text-align: center;
margin: 44px auto;
width: 230px;
position: relative;
margin-top: 180px;
display:inline-block;
}
.locked img {
width: 60px;
margin: 0 26px;
}
.locked.row-1 svg {
width: 163px;
height: 175px;
position: absolute;
top: -130px;
right: -50px;
z-index: -1;
}
.locked.row-2 svg {
width: 77px;
height: 30px;
position: absolute;
top: -54px;
right: 74px;
z-index: -1;
}<!-- CURVED PROGRESS -->
<div class="locked row-1">
<svg viewBox="0 0 163 175">
<defs>
<mask id="mask">
<path stroke-dasharray="400 400" stroke-width="10" stroke-dashoffset="-110" stroke="white" d="M0 16.7246C54 16.7246 145.7 11.6246 146.5 85.2246C147.3 158.825 51 158.225 0 158.225"></path>
</mask>
<path id="path" fill="none" d="M0 16.7246C54 16.7246 145.7 11.6246 146.5 85.2246C147.3 158.825 51 158.225 0 158.225"></path>
</defs>
<!-- solid wide line -->
<use href="#path" stroke="rgba(255,255,255,0.05)" stroke-width="30"></use>
<!-- solid narrow line -->
<use href="#path" stroke="rgba(255,255,255,0.12)" stroke-width="15"></use>
<!-- dotted full line -->
<use href="#path" stroke="rgba(255,255,255,0.2)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round"></use>
<!-- dotted masked line -->
<use href="#path" stroke="rgba(255,255,255,0.8)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round" mask="url(#mask)"></use>
</svg>
<img src="https://i.imgur.com/h3ElY2f_d.webp?maxwidth=728&fidelity=grand">
</div>
<!-- LINE PROGRESS -->
<div class="locked row-2">
<svg viewBox="0 0 77 17">
<defs>
<mask id="mask-2">
<path stroke-dasharray="400 400" stroke-width="10" stroke-dashoffset="10" stroke="white" d="M76.6841 8.40967H0.551514"></path>
</mask>
<path id="path-2" fill="none" d="M76.6841 8.40967H0.551514"></path>
</defs>
<!-- solid wide line -->
<use href="#path-2" stroke="rgba(255,255,255,0.05)" stroke-width="30"></use>
<!-- solid narrow line -->
<use href="#path-2" stroke="rgba(255,255,255,0.12)" stroke-width="15"></use>
<!-- dotted full line -->
<use href="#path-2" stroke="rgba(255,255,255,0.2)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round"></use>
<!-- dotted masked line -->
<use href="#path-2" stroke="rgba(255,255,255,0.8)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round" mask="url(#mask-2)"></use>
</svg>
<img src="https://i.imgur.com/h3ElY2f_d.webp?maxwidth=728&fidelity=grand">
</div>
有什么想法吗?谢谢!
发布于 2020-09-15 14:02:50
所以我想出了问题。我从另一个堆叠溢出问题中找到了解决办法。
,所以您不能使用默认的掩码参数像水平或垂直线(零高度/零宽度边框)那样掩蔽形状。只需将“maskUnits=”userSpaceOnUse添加到掩码元素中即可修复。
有关更多信息:SVG: Mask not working as expected
所以我不得不将maskUnits="userSpaceOnUse"添加到mask中
body {
background: #171B42;
}
.locked {
text-align: center;
margin: 44px auto;
width: 230px;
position: relative;
margin-top: 180px;
display:inline-block;
}
.locked img {
width: 60px;
margin: 0 26px;
}
.locked.row-1 svg {
width: 163px;
height: 175px;
position: absolute;
top: -130px;
right: -50px;
z-index: -1;
}
.locked.row-2 svg {
width: 77px;
height: 30px;
position: absolute;
top: -54px;
right: 74px;
z-index: -1;
}<!-- CURVED PROGRESS -->
<div class="locked row-1">
<svg viewBox="0 0 163 175">
<defs>
<mask id="mask" maskUnits="userSpaceOnUse">
<path stroke-dasharray="400 400" stroke-width="10" stroke-dashoffset="-110" stroke="white" d="M0 16.7246C54 16.7246 145.7 11.6246 146.5 85.2246C147.3 158.825 51 158.225 0 158.225"></path>
</mask>
<path id="path" fill="none" d="M0 16.7246C54 16.7246 145.7 11.6246 146.5 85.2246C147.3 158.825 51 158.225 0 158.225"></path>
</defs>
<!-- solid wide line -->
<use href="#path" stroke="rgba(255,255,255,0.05)" stroke-width="30"></use>
<!-- solid narrow line -->
<use href="#path" stroke="rgba(255,255,255,0.12)" stroke-width="15"></use>
<!-- dotted full line -->
<use href="#path" stroke="rgba(255,255,255,0.2)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round"></use>
<!-- dotted masked line -->
<use href="#path" stroke="rgba(255,255,255,0.8)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round" mask="url(#mask)"></use>
</svg>
<img src="https://i.imgur.com/h3ElY2f_d.webp?maxwidth=728&fidelity=grand">
</div>
<!-- LINE PROGRESS -->
<div class="locked row-2">
<svg viewBox="0 0 77 17">
<defs>
<mask id="mask-2">
<path stroke-dasharray="400 400" stroke-width="10" stroke-dashoffset="10" stroke="white" d="M76.6841 8.40967H0.551514"></path>
</mask>
<path id="path-2" fill="none" d="M76.6841 8.40967H0.551514"></path>
</defs>
<!-- solid wide line -->
<use href="#path-2" stroke="rgba(255,255,255,0.05)" stroke-width="30"></use>
<!-- solid narrow line -->
<use href="#path-2" stroke="rgba(255,255,255,0.12)" stroke-width="15"></use>
<!-- dotted full line -->
<use href="#path-2" stroke="rgba(255,255,255,0.2)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round"></use>
<!-- dotted masked line -->
<use href="#path-2" stroke="rgba(255,255,255,0.8)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round" mask="url(#mask-2)"></use>
</svg>
<img src="https://i.imgur.com/h3ElY2f_d.webp?maxwidth=728&fidelity=grand">
</div>
发布于 2020-09-15 02:55:33
在stroke="rgba(255,255,255,0.2)"的第二个到最后一个实例中,将不透明度从0.2更改为0.8。
body {
background: #171B42;
}
.locked {
text-align: center;
margin: 44px auto;
width: 230px;
position: relative;
margin-top: 180px;
display:inline-block;
}
.locked img {
width: 60px;
margin: 0 26px;
}
.locked.row-1 svg {
width: 163px;
height: 175px;
position: absolute;
top: -130px;
right: -50px;
z-index: -1;
}
.locked.row-2 svg {
width: 77px;
height: 30px;
position: absolute;
top: -54px;
right: 74px;
z-index: -1;
}<!-- CURVED PROGRESS -->
<div class="locked row-1">
<svg viewBox="0 0 163 175">
<defs>
<mask id="mask">
<path stroke-dasharray="400 400" stroke-width="10" stroke-dashoffset="-110" stroke="white" d="M0 16.7246C54 16.7246 145.7 11.6246 146.5 85.2246C147.3 158.825 51 158.225 0 158.225"></path>
</mask>
<path id="path" fill="none" d="M0 16.7246C54 16.7246 145.7 11.6246 146.5 85.2246C147.3 158.825 51 158.225 0 158.225"></path>
</defs>
<!-- solid wide line -->
<use href="#path" stroke="rgba(255,255,255,0.05)" stroke-width="30"></use>
<!-- solid narrow line -->
<use href="#path" stroke="rgba(255,255,255,0.12)" stroke-width="15"></use>
<!-- dotted full line -->
<use href="#path" stroke="rgba(255,255,255,0.2)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round"></use>
<!-- dotted masked line -->
<use href="#path" stroke="rgba(255,255,255,0.8)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round" mask="url(#mask)"></use>
</svg>
<img src="https://i.imgur.com/h3ElY2f_d.webp?maxwidth=728&fidelity=grand">
</div>
<!-- LINE PROGRESS -->
<div class="locked row-2">
<svg viewBox="0 0 77 17">
<defs>
<mask id="mask-2">
<path stroke-dasharray="400 400" stroke-width="10" stroke-dashoffset="10" stroke="white" d="M76.6841 8.40967H0.551514"></path>
</mask>
<path id="path-2" fill="none" d="M76.6841 8.40967H0.551514"></path>
</defs>
<!-- solid wide line -->
<use href="#path-2" stroke="rgba(255,255,255,0.05)" stroke-width="30"></use>
<!-- solid narrow line -->
<use href="#path-2" stroke="rgba(255,255,255,0.12)" stroke-width="15"></use>
<!-- dotted full line -->
<use href="#path-2" stroke="rgba(255,255,255,0.8)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round"></use>
<!-- dotted masked line -->
<use href="#path-2" stroke="rgba(255,255,255,0.8)" stroke-width="5" stroke-dasharray="0 20" stroke-linecap="round" mask="url(#mask-2)"></use>
</svg>
<img src="https://i.imgur.com/h3ElY2f_d.webp?maxwidth=728&fidelity=grand">
</div>
https://stackoverflow.com/questions/63894207
复制相似问题