stroke-dashoffset

Baseline 已广泛支持

此功能已经成熟,并可在许多设备和浏览器版本上使用。自 ⁨2020 年 3 月⁩起,它已在各浏览器中推出。

stroke-dashoffset 属性是一个表示属性,用于定义与关联的虚线数组(dash array)的渲染偏移量。

注意: 作为一个表示属性,stroke-dashoffset 也有一个对应的 CSS 属性:stroke-dashoffset。当两者都被指定时,CSS 属性具有更高的优先级。

你可以将此属性与以下 SVG 元素一起使用

示例

html
<svg viewBox="-3 0 33 10" xmlns="http://www.w3.org/2000/svg">
  <!-- No dash array -->
  <line x1="0" y1="1" x2="30" y2="1" stroke="black" />

  <!-- No dash offset -->
  <line x1="0" y1="3" x2="30" y2="3" stroke="black" stroke-dasharray="3 1" />

  <!--
  The start of the dash array computation
  is pulled by 3 user units
  -->
  <line
    x1="0"
    y1="5"
    x2="30"
    y2="5"
    stroke="black"
    stroke-dasharray="3 1"
    stroke-dashoffset="3" />

  <!--
  The start of the dash array computation
  is pushed by 3 user units
  -->
  <line
    x1="0"
    y1="7"
    x2="30"
    y2="7"
    stroke="black"
    stroke-dasharray="3 1"
    stroke-dashoffset="-3" />

  <!--
  The start of the dash array computation
  is pulled by 1 user units which ends up
  in the same rendering as the previous example
  -->
  <line
    x1="0"
    y1="9"
    x2="30"
    y2="9"
    stroke="black"
    stroke-dasharray="3 1"
    stroke-dashoffset="1" />

  <!--
  the following red lines highlight the
  offset of the dash array for each line
  -->
  <path d="M0,5 h-3 M0,7 h3 M0,9 h-1" stroke="rgb(255 0 0 / 50%)" />
</svg>

用法说明

<percentage> | <length>
默认值 0
可动画的

偏移量通常以用户单位表示,并根据 pathLength 进行解析。但如果使用 <percentage>,则该值被解析为当前视口的百分比。

规范

规范
Scalable Vector Graphics (SVG) 2
# StrokeDashoffsetProperty

浏览器兼容性

另见