#碎碎念 #前端 #SVG #tip
写了这么一段代码,想要一段可随高度变化的虚线。
发现不对!高度特别小的时候挤在一块了(如图1)。
咋办?使用
https://developer.mozilla.org/zh-CN/docs/Web/SVG/Reference/Attribute/vector-effect
为什么不用
现在完美实现!(图2)
写了这么一段代码,想要一段可随高度变化的虚线。
<svg
className="pointer-events-none absolute bottom-0 left-px h-[calc(100%+1rem)] w-[1.5px]"
viewBox="0 0 2 366"
preserveAspectRatio="none meet"
aria-hidden="true"
>
<path d="M0.749978 365.5L0.750106 0" stroke="white" strokeOpacity="0.2" strokeWidth="1.5" strokeDasharray="5.25 5.25" />
</svg>发现不对!高度特别小的时候挤在一块了(如图1)。
咋办?使用
vectorEffect="non-scaling-stroke"https://developer.mozilla.org/zh-CN/docs/Web/SVG/Reference/Attribute/vector-effect
non-scaling-stroke 的使用可以看这篇文章:CSS vector-effect与SVG stroke描边缩放该值修改了笔触的方式。通常,笔触涉及在当前用户坐标系中计算形状路径的笔触轮廓,并用笔触颜料(颜色或渐变)填充轮廓。该值的最终视觉效果是笔触宽度不依赖于元素的变换(包括非均匀缩放和剪切变换)和缩放级别。
为什么不用
border-style: dashed; ?因为他没有办法定义线段的长度和大小,视不同实现而定。 strokeOpacity="0.2"
strokeWidth="1.5"
strokeDasharray="5.25 5.25"
+ vectorEffect="non-scaling-stroke"
/>
</svg>
<div className="space-y-3">现在完美实现!(图2)