开始
begin 属性定义动画何时开始。
属性值是一个用分号分隔的值列表。开始时间列表的解释在 SMIL 规范的“开始和结束时间列表的评估”中详细说明。每个单独的值可以是以下之一:<offset-value>、<syncbase-value>、<event-value>、<repeat-value>、<accessKey-value>、<wallclock-sync-value> 或关键字 indefinite。
您可以将此属性与以下 SVG 元素一起使用
animate、animateMotion、animateTransform、set
对于 <animate>、<animateMotion>、<animateTransform> 和 <set>,begin 定义元素应何时开始,即何时变为活动状态。
| 值 | <begin-value-list> |
|---|---|
| 默认值 | 0s |
| 可动画 | 否 |
<begin-value-list> 是一个用分号分隔的值列表。每个值可以是以下之一
<offset-value>-
此值定义了一个时钟值,表示相对于 SVG 文档开头的时间点(通常是
load或DOMContentLoaded事件)。负值有效。 <syncbase-value>-
此值定义了一个同步基准以及一个可选的与该同步基准的偏移量。元素的动画开始时间相对于另一个动画的开始或活动结束时间定义。
有效的 syncbase-value 包括对另一个动画元素的 ID 引用,后跟一个点,以及
begin或end以标识是与引用的动画元素的开始还是活动结束同步。可以附加一个可选的偏移值,如<offset-value>中所定义。 <event-value>-
此值定义了一个事件和一个可选的偏移量,该偏移量确定元素的动画应开始的时间。动画开始时间相对于指定事件触发的时间定义。
有效的 event-value 包括元素 ID,后跟一个点和该元素支持的事件之一。所有有效事件(并非所有元素都支持)由 DOM 和 HTML 规范定义。它们是
focusblurfocusinfocusoutDOMActivateauxclickclickdblclickmousedownmouseentermouseleavemousemovemouseoutmouseovermouseupwheelbeforeinputinputkeydownkeyupcompositionstartcompositionupdatecompositionendloadunloadaborterrorselectresizescrollbeginEventendEventrepeatEvent
可以附加一个可选的偏移值,如
<offset-value>中所定义。 <repeat-value>-
此值定义了一个限定的重复事件。元素动画开始时间相对于具有指定迭代值的重复事件引发的时间定义。
有效的 repeat 值包括元素 ID,后跟一个点和函数
repeat(),该函数的参数是指定重复次数的整数。可以附加一个可选的偏移值,如<offset-value>中所定义。 <accessKey-value>-
此值定义了一个应触发动画的访问键。当用户按下指定的键时,元素动画将开始。
有效的 accessKey-value 包括函数
accessKey(),其参数是要输入的字符。可以附加一个可选的偏移值,如<offset-value>中所定义。 <wallclock-sync-value>-
此值将动画开始时间定义为现实世界中的时钟时间。
有效的 wallclock-sync-value 包括函数
wallclock(),其参数是时间值。时间语法基于ISO 8601中定义的语法。 indefinite-
动画的开始将由
beginElement()方法调用或目标为该元素的超链接确定。
示例
偏移量示例
<svg
width="120"
height="120"
viewBox="0 0 120 120"
xmlns="http://www.w3.org/2000/svg"
version="1.1">
<!-- animated rectangles -->
<rect x="10" y="35" height="15" width="0">
<animate
attributeType="XML"
attributeName="width"
to="100"
begin="0s"
dur="8s"
fill="freeze" />
</rect>
<rect x="35" y="60" height="15" width="0">
<animate
attributeType="XML"
attributeName="width"
to="75"
begin="2s"
dur="6s"
fill="freeze" />
</rect>
<rect x="60" y="85" height="15" width="0">
<animate
attributeType="XML"
attributeName="width"
to="50"
begin="4s"
dur="4s"
fill="freeze" />
</rect>
<!-- grid -->
<text x="10" y="20" text-anchor="middle">0s</text>
<line x1="10" y1="25" x2="10" y2="105" stroke="grey" stroke-width=".5" />
<text x="35" y="20" text-anchor="middle">2s</text>
<line x1="35" y1="25" x2="35" y2="105" stroke="grey" stroke-width=".5" />
<text x="60" y="20" text-anchor="middle">4s</text>
<line x1="60" y1="25" x2="60" y2="105" stroke="grey" stroke-width=".5" />
<text x="85" y="20" text-anchor="middle">6s</text>
<line x1="85" y1="25" x2="85" y2="105" stroke="grey" stroke-width=".5" />
<text x="110" y="20" text-anchor="middle">8s</text>
<line x1="110" y1="25" x2="110" y2="105" stroke="grey" stroke-width=".5" />
<line x1="10" y1="30" x2="110" y2="30" stroke="grey" stroke-width=".5" />
<line x1="10" y1="105" x2="110" y2="105" stroke="grey" stroke-width=".5" />
</svg>
同步基准示例
<svg
width="120"
height="120"
viewBox="0 0 120 120"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- animated rectangles -->
<rect x="10" y="35" height="15" width="0">
<animate
attributeType="XML"
attributeName="width"
to="50"
id="first"
begin="0s;third.end"
dur="4s" />
</rect>
<rect x="60" y="60" height="15" width="0">
<animate
attributeType="XML"
attributeName="width"
to="25"
id="second"
begin="first.end"
dur="2s" />
</rect>
<rect x="85" y="85" height="15" width="0">
<animate
attributeType="XML"
attributeName="width"
to="25"
id="third"
begin="second.end"
dur="2s" />
</rect>
<!-- grid -->
<text x="10" y="20" text-anchor="middle">0s</text>
<line x1="10" y1="25" x2="10" y2="105" stroke="grey" stroke-width=".5" />
<text x="35" y="20" text-anchor="middle">2s</text>
<line x1="35" y1="25" x2="35" y2="105" stroke="grey" stroke-width=".5" />
<text x="60" y="20" text-anchor="middle">4s</text>
<line x1="60" y1="25" x2="60" y2="105" stroke="grey" stroke-width=".5" />
<text x="85" y="20" text-anchor="middle">6s</text>
<line x1="85" y1="25" x2="85" y2="105" stroke="grey" stroke-width=".5" />
<text x="110" y="20" text-anchor="middle">8s</text>
<line x1="110" y1="25" x2="110" y2="105" stroke="grey" stroke-width=".5" />
<line x1="10" y1="30" x2="110" y2="30" stroke="grey" stroke-width=".5" />
<line x1="10" y1="105" x2="110" y2="105" stroke="grey" stroke-width=".5" />
</svg>
事件示例
<svg
width="120"
height="120"
viewBox="0 0 120 120"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- animated rectangle -->
<rect x="10" y="35" height="15" width="0">
<animate
attributeType="XML"
attributeName="width"
from="0"
to="100"
begin="startButton.click"
dur="8s"
fill="freeze" />
</rect>
<!-- trigger -->
<rect
id="startButton"
style="cursor:pointer;"
x="19.5"
y="62.5"
rx="5"
height="25"
width="80"
fill="#EFEFEF"
stroke="black"
stroke-width="1" />
<text x="60" y="80" text-anchor="middle" style="pointer-events:none;">
Click me.
</text>
<!-- grid -->
<text x="10" y="20" text-anchor="middle">0s</text>
<line x1="10" y1="25" x2="10" y2="55" stroke="grey" stroke-width=".5" />
<text x="35" y="20" text-anchor="middle">2s</text>
<line x1="35" y1="25" x2="35" y2="55" stroke="grey" stroke-width=".5" />
<text x="60" y="20" text-anchor="middle">4s</text>
<line x1="60" y1="25" x2="60" y2="55" stroke="grey" stroke-width=".5" />
<text x="85" y="20" text-anchor="middle">6s</text>
<line x1="85" y1="25" x2="85" y2="55" stroke="grey" stroke-width=".5" />
<text x="110" y="20" text-anchor="middle">8s</text>
<line x1="110" y1="25" x2="110" y2="55" stroke="grey" stroke-width=".5" />
<line x1="10" y1="30" x2="110" y2="30" stroke="grey" stroke-width=".5" />
<line x1="10" y1="55" x2="110" y2="55" stroke="grey" stroke-width=".5" />
</svg>
重复示例
<svg
width="120"
height="120"
viewBox="0 0 120 120"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- animated rectangle -->
<rect x="10" y="35" height="15" width="0">
<animate
attributeType="XML"
attributeName="width"
from="0"
to="100"
id="myLoop"
begin="0s;myLoop.end"
dur="4s"
repeatCount="3" />
<set
attributeType="CSS"
attributeName="fill"
to="green"
begin="myLoop.begin" />
<set
attributeType="CSS"
attributeName="fill"
to="gold"
begin="myLoop.repeat(1)" />
<set
attributeType="CSS"
attributeName="fill"
to="red"
begin="myLoop.repeat(2)" />
</rect>
<!-- grid -->
<text x="10" y="20" text-anchor="middle">0s</text>
<line x1="10" y1="25" x2="10" y2="55" stroke="grey" stroke-width=".5" />
<text x="35" y="20" text-anchor="middle">1s</text>
<line x1="35" y1="25" x2="35" y2="55" stroke="grey" stroke-width=".5" />
<text x="60" y="20" text-anchor="middle">2s</text>
<line x1="60" y1="25" x2="60" y2="55" stroke="grey" stroke-width=".5" />
<text x="85" y="20" text-anchor="middle">3s</text>
<line x1="85" y1="25" x2="85" y2="55" stroke="grey" stroke-width=".5" />
<text x="110" y="20" text-anchor="middle">4s</text>
<line x1="110" y1="25" x2="110" y2="55" stroke="grey" stroke-width=".5" />
<line x1="10" y1="30" x2="110" y2="30" stroke="grey" stroke-width=".5" />
<line x1="10" y1="55" x2="110" y2="55" stroke="grey" stroke-width=".5" />
</svg>
访问键示例
<svg
width="120"
height="120"
viewBox="0 0 120 120"
xmlns="http://www.w3.org/2000/svg"
version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- animated rectangles -->
<rect x="10" y="35" height="15" width="0">
<animate
attributeType="XML"
attributeName="width"
from="0"
to="100"
begin="accessKey(s)"
dur="8s"
fill="freeze" />
</rect>
<!-- trigger -->
<text x="60" y="80" text-anchor="middle" style="pointer-events:none;">
Hit the "s" key
</text>
<!-- grid -->
<text x="10" y="20" text-anchor="middle">0s</text>
<line x1="10" y1="25" x2="10" y2="55" stroke="grey" stroke-width=".5" />
<text x="35" y="20" text-anchor="middle">2s</text>
<line x1="35" y1="25" x2="35" y2="55" stroke="grey" stroke-width=".5" />
<text x="60" y="20" text-anchor="middle">4s</text>
<line x1="60" y1="25" x2="60" y2="55" stroke="grey" stroke-width=".5" />
<text x="85" y="20" text-anchor="middle">6s</text>
<line x1="85" y1="25" x2="85" y2="55" stroke="grey" stroke-width=".5" />
<text x="110" y="20" text-anchor="middle">8s</text>
<line x1="110" y1="25" x2="110" y2="55" stroke="grey" stroke-width=".5" />
<line x1="10" y1="30" x2="110" y2="30" stroke="grey" stroke-width=".5" />
<line x1="10" y1="55" x2="110" y2="55" stroke="grey" stroke-width=".5" />
</svg>
此示例嵌入在 iframe 中。如果要激活键事件,则必须先单击它。
规范
| 规范 |
|---|
| SVG 动画级别 2 # BeginAttribute |