如何仅使用CSS和滚动驱动动画来沿着SVG路径动画化一个元素?

huangapple go评论65阅读模式
英文:

How to animate an element along an SVG path using only CSS and Scroll-driven Animations?

问题

我正在尝试使用Chrome Canary,并有以下代码,其中定义了一个SVG路径和一个图像元素。我想要在滚动时通过SVG路径来使图像动画化。如何仅使用CSS和滚动驱动的动画(不使用JavaScript)来实现这一目标?

<img id="car" width="256" height="256" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAIRlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAADKgAwAEAAAAAQAAADIAAAAAhvHCqAAAAAlwFlzAAALEwAACxMBAJqcGAAAAVlpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDYuMC4wIj4KPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIKICAgICAgPHJkZjpEZXNjcmlwdGlvbj4xPC90aWZmOk9yaWVudGF0aW9uPgogICAgICA8L3JkZjpEZXNjcmlwdGlvbj4KICA8L3g6eG1wbWV0YT4KPC94OnhtcG1ldGE+CgogZXtoQcAAAAv4ScRGF0YUtleTI+LnhtbC1yZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZy1zeW50YXgtbnMiIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtb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CYAGUYdqYeNaLndPfLM4Zef/JHIDzAgVmOcZx59NJs8+cTRkTjeWtqypen6ft6gKCR0IhQ65viyPDgkm6OWbDVdZ8XxzZLjSTVJpRpFstxuS9yFBZ1IWjxP4

<details>
<summary>英文:</summary>

I am experimenting with Chrome Canary, and I have the following code which defines an SVG path and an image element. I would like to animate the image as I scroll, along the SVG path. How do I achieve this using only CSS and scroll-driven animations (without JavaScript)?

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-css --&gt;

    #car {
      width: 25px;
      height: 25px;
    }

&lt;!-- language: lang-html --&gt;
    &lt;img id=&quot;car&quot; width=&quot;256&quot; height=&quot;256&quot; src=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAAAXNSR0IArs4c6QAAAIRlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAABIAAAAAQAAAEgAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAADKgAwAEAAAAAQAAADIAAAAAhvHCqAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAVlpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDYuMC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIKICAgICAgICAgICAgeG1sbnM6dGlmZj0iaHR0cDovL25zLmFkb2JlLmNvbS90aWZmLzEuMC8iPgogICAgICAgICA8dGlmZjpPcmllbnRhdGlvbj4xPC90aWZmOk9yaWVudGF0aW9uPgogICAgICA8L3JkZjpEZXNjcm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ebIdSoX3dDUt+02fef/tX+T4nMOmZqyn/ql794ZdSRnaNbLm8Y39My3iFTOR02eTVOZZ6C8SoQ24nbl0FtifEKaa8JUjWe2yirhrTx2iRniCK5yv0YmS+Lh0oAUGJMUV8olGR+pGyq+YJT832HozZdjGMTUJzeP/262USaRJ7BOyQ/ajvXMxm+SHOFAYci0s8XCk5Ejfanm2/lMG0dvoIy/j6RilJPfrd375fc87M/T85NS3nPTeKgQAyifFhp4BulWgRf9sPN79WowdpwHEM0JWvw9zKLinJkR4mikPWvskpyWYfy1haMVIY2832GJLAZg4YwvuS56SgnG59eMtZuSol3+hWp9UJAJs1QhvgAo7HSZX4DLzbCVrhszJE/HD/5xOMiP2bXxH5YscZez95044NRGD4wPFicIF8riAGnPu/0+4RPVARVQD9DHPRGiVeCXMeVXjFKJDzbzKaGM65uatFjro0d+6lL5uooQCjX5vAr1qvxzkK+5XsBX/Npl0OAg0aaLG3A8xon/wU+dec7zebZbnX1dQqnY/eLnLZK849fdzx+2aG+YguNzfUXz9166wYvCYc8z8+6iZfleMlwkprAeM5oHk+BJnitT9ffJtaUXxtCJN50fAe6eG5ooEiXz9meBzsEcuIXXjq2cP6ej3+9tlx9+LUX/vrJgbvv/GN0+nRulA+VxmKnMzc3130Af7+xydvvLv3xwc5QY6Zuf6s+b1/67o7s37/fe/62D131r6SqXjRXf6o6iH4HwUzbFGOXrlNiX7x04dqDI1Nv/5Hu4sR3eKM/xolM9WavdfZh3z7HOXCA4LJU6P0o+CZW2Dn8W1MGGe/hS0Ez/+sfBd/DePRV6yPQR6CPQB+BPgJ9BP4vEfgnHPy6iS/0w2YAAAAASUVORK5CYII=&quot;&gt;
    &lt;svg width=&quot;175&quot; height=&quot;1300&quot; preserveAspectRatio=&quot;none&quot; viewBox=&quot;0 0 175 1300&quot; fill=&quot;none&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
        &lt;path id=&quot;my_path&quot;
            d=&quot;M 28 0 C -72 200 188 300 88 500 C -32 700 88 1100 148 1000 C 208 900 148 600 68 1000 C 28 1200 128 1300 128 1300&quot;
            stroke-width=&quot;4&quot; stroke=&quot;#000&quot; /&gt;
    &lt;/svg&gt;

&lt;!-- end snippet --&gt;



</details>


# 答案1
**得分**: -2

以下是您提供的文本的翻译:

**注意:这仅在启用了`experimental-web-platform-features`的Chrome Canary 115+中有效。**

总体而言,策略是使用`offset-path`样式属性和`url()` CSS函数引用元素应该遵循的路径。在这种情况下,您将其设置为`url(#my_path)`。然后在您的`@keyframes <name>`中,从`offset-distance: 0%;`过渡到`offset-distance: 100%;`。

然而,将滚动驱动的时间轴附加到您的动画的方法在很大程度上取决于您的页面结构以及路径在页面上的确切位置。具体来说,有两种情况。

**情况1:路径跨越整个滚动容器**

这是简单的情况。我们可以使用`animation-timeline: scroll();`将滚动时间轴与动画关联起来,该时间轴引用了最近祖先滚动容器的块轴。然后我们使用`animation: <name> linear;`将动画设置在元素上。

确保还通过`position: absolute`和`inset: 0`将元素的初始位置设置为偏移父元素的`(0,0)`。所有这些,代码应该看起来像这样:

```css
#car {
  width: 25px;
  height: 25px;

  position: absolute;
  inset: 0;

  offset-path: url(#my_path);
  animation: offsetDistance linear;
  animation-timeline: scroll();
}

@keyframes offsetDistance {
    from { offset-distance: 0%; }
    to { offset-distance: 100%; }
}

情况2:路径是滚动容器的一部分

在这种情况下,我们必须使用View Progress Timeline,这需要更多的设置才能正确设置,因此更容易逐步解释。

  1. 在元素和路径的最近共同祖先上声明一个范围。这使得可以将视图时间轴的附加推迟到后代,后代将是路径元素,并由其他后代引用,后者将是图像元素。
#container {
  position: relative;
  timeline-scope: --container;
}
  1. 使用相同的名称将路径元素附加到视图时间轴,如下所示:
#my_path {
  view-timeline-name: --container;
}
  1. 将命名的时间轴与元素的动画关联。
#car {
  animation-timeline: --container; /* ... */
}
  1. 确定您的动画的时间轴范围。这也很大程度上取决于您页面的结构,可能需要一些手动测试。但是,这个工具在了解各种范围的行为方面非常有帮助。在我的示例中,我选择了以下范围:
#car {
  animation-timeline: --container; 
  animation-range: exit-crossing -5% entry-crossing 105%;
  /* ... */
}
  1. 最后,为了防止动画结束时出现视觉故障,请将animation-fill-mode更改为forwards
#car {
  animation: <name> linear forwards;
  animation-timeline: --container; 
  animation-range: exit-crossing -5% entry-crossing 105%;
  /* ... */
}

最后,将所有内容放在一起,我们得到以下动画:

#container {
  position: relative;
  timeline-scope: --container;
}

#my_path {
  view-timeline-name: --container;
}

#car {
  width: 25px;
  height: 25px;

  position: absolute;
  inset: 0;

  offset-path: url(#my_path);
  animation: offsetDistance linear forwards;
  animation-timeline: --container;
  animation-range: exit-crossing -5% entry-crossing 105%;
}

@keyframes offsetDistance {
  from { offset-distance: 0%; }
  to { offset-distance: 100%; }
}

希望这有助于您理解文本的内容和意义。

英文:

Note: This currently only works in Chrome Canary 115+ with experimental-web-platform-features enabled.

In general, the strategy is to use the offset-path style property with the url() CSS function to reference the path the element should follow. In this case specifically, you would set it to url(#my_path). And then in your @keyframes &lt;name&gt;, simply transition from offset-distance: 0%; to offset-distance: 100%;.

However, the method of attaching a scroll-driven timeline to your animation depends largely on the structure of your page and precisely where the path is located on the page. Namely, there are two cases.

Case 1: The path spans the entire scroll container

This is the simple case. We can associate a scroll timeline with the animation using animation-timeline: scroll();, which references the block axis of the nearest ancestor scroll container. And then we set the animation on the element using animation: &lt;name&gt; linear;.

Make sure to also set the initial position of the element to (0,0) of the offset parent via position: absolute and inset: 0. All together, the code should look something like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#car {
width: 25px;
height: 25px;
position: absolute;
inset: 0;
offset-path: url(#my_path);
animation: offsetDistance linear;
animation-timeline: scroll();
}
@keyframes offsetDistance {
from { offset-distance: 0%; }
to { offset-distance: 100%; }
}

<!-- language: lang-html -->

&lt;img id=&quot;car&quot; width=&quot;256&quot; height=&quot;256&quot; src=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAMAAAAp4XiDAAAABGdBTUEAALGPC/xh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RUlbYqBRUbJAPt57eSopK4o/PgAPEsYsKeJwbtA7OLg1M9pXVIosKy5ISwALDQAaHVBPUdJNStpRTshTUaI8Oqg3NrMzMahDQalSUtxfXAMGCHc5OsQzMAAsMNxLR9dEQeN3dbUjIdxjYAAUFk1FRU45OwAeIQEoK9NLR75VVLgtK20yM3ssLJIyMpk2Nd5pZq1CQa4SEJVKSRweH8o0MoI7Ot9lYTYzNa0wLbtDQH1LSqEuLEhNU9FUUURGSok2NQIxM0BAQ1tZXdVoZmdiZUxKSwAlKJI2NmIvLj85OisjJHZCRLMbGbYEAEtmbpkwLrQ7OYxMTZuFhScvMC8xNRxBQ9BfXTQbHCcUFgAWGFEnJ0BQUCoYGUc/QHZVW89NSjBUWAtGSXh7fIdDQl5sb5SYmISIiJdeXw0fIEoiIrxHRRYfIRM7QMdfXSw/QL8pJlFfX5hvb5MqKXh2dmlWVgBAQYFhYaBzc6yuSJ0AAADfdFJOUwD3/hQ5OTgCAQQg+g45Tf5v/vz4Gpos1vv89/ouMf79/fFu2q3s5anDkPsk1cr+YP3ILfu+PP3898yd9PmNdE7Kk8D7hPzukuXH+Pz8a4px/v5B/vn5kv////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////7////////////////////////////////////+//////////////6TEl4rAAAESUlEQVRIx2NgGAWjgHqAgxOHODsuwAmW5AABVA14bGHnQijj5OSEKAVq52AWE2NjY+MN4mVFAF5eXllZLr9+PmYRZh5OmCVQTUrKitOT6+qS0mfOzAWBDx/27+8AgW1z5879YSAlpeqho+Ok4ijApw3SBMT6vZdbk6enz5iRLNcWHPC8evGaHj09JqaeNWurV5/beHTL7KKl2dlVS5csWRLvpeHPCdTDqbxIOLYl6Wn7W7PD6+e1X794ceFGQUF+wbOzOq61B974tcrH2yxLWEiSUVoovXDJX0seBgbTz3kJCQmHNz57dWV9YVxFXFwckBvb0lJXt/Ttm9evpx2Z9nDVyYzSmJlFyy9Jx1vcsGJgEEg1bmkonLv49Im2eStiYmIiI2NAZH5C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AAASUVORK5CYII=&quot;&gt;
&lt;svg width=&quot;175&quot; height=&quot;1300&quot; viewBox=&quot;0 0 175 1300&quot; fill=&quot;none&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
&lt;path id=&quot;my_path&quot; d=&quot;M 28 0 C -72 200 188 300 88 500 C -32 700 88 1100 148 1000 C 208 900 148 600 68 1000 C 28 1200 128 1300 128 1300&quot;
stroke-width=&quot;4&quot; stroke=&quot;#000&quot; /&gt;
&lt;/svg&gt;

<!-- end snippet -->

Case 2: The path is a portion of the scroll container

In this case, we must use a View Progress Timeline which requires a little more effort to setup correctly, and so it's easier to explain step-by-step.

  1. Declare a scope on the nearest common ancestor of the element and the path. This enables the attachment of a view timeline to be deferred to a descendent, which will be the path element, and referenced by other decendents, which will be the image element.
#container {
  timeline-scope: --container;
}
  1. Attach the path element to the view timeline with the same name, like so:
#my_path {
  view-timeline-name: --container;
}
  1. Associate the named timeline with the element's animation.
#car {
  animation-timeline: --container; /* ... */
}
  1. Determine your animation's timeline range. This also depends greatly on your page's structure and likely requires some manual testing. However, this tool is extremely helpful in understanding the behavior of various ranges. In my example, I choose the following:
#car {
  animation-timeline: --container; 
  animation-range: exit-crossing -5% entry-crossing 105%;
  /* ... */
}
  1. Lastly, to prevent visual glitches at the end of the animation, change the animation-fill-mode to forwards.
#car {
  animation: &lt;name&gt; linear forwards;
  animation-timeline: --container; 
  animation-range: exit-crossing -5% entry-crossing 105%;
  /* ... */
}

Finally, putting everything together, we get the following animation:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#container {
position: relative;
timeline-scope: --container;
}
#my_path {
view-timeline-name: --container;
}
#car {
width: 25px;
height: 25px;
position: absolute;
inset: 0;
offset-path: url(#my_path);
animation: offsetDistance linear forwards;
animation-timeline: --container;
animation-range: exit-crossing -5% entry-crossing 105%;
}
@keyframes offsetDistance {
from { offset-distance: 0%; }
to { offset-distance: 100%; }
}

<!-- language: lang-html -->

&lt;div style=&quot;height: 150px;&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;container&quot;&gt;
&lt;img id=&quot;car&quot; width=&quot;256&quot; height=&quot;256&quot;
src=&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAMAAAAp4XiDAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAFZaVRYdFhNTDpjb20uYWRvYmUueG1wAAAAAAA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJYTVAgQ29yZSA2LjAuMCI+CiAgIDxyZGY6UkRGIHhtbG5zOnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+CiAgICAgIDxyZGY6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiCiAgICAgICAgICAgIHhtbG5zOnRpZmY9Imh0dHA6Ly9ucy5hZG9iZS5jb20vdGlmZi8xLjAvIj4KICAgICAgICAgPHRpZmY6T3JpZW50YXRpb24+MTwvdGlmZjpPcmllbnRhdGlvbj4KICAgICAgPC9yZGY6RGVzY3JpcHRpb24+CiAgIDwvcmRmOlJERj4KPC94OnhtcG1ldGE+Chle4QcAAAAJcEhZcwAACxMAAAsTAQCanBgAAAL6UExURUdwTLwoJQAEB8w9OuJBPuZWU9BCP7hPTOceHq4tKNktJdpsarwpJeRPSq8ICHEnJrAPC4cxL8lAPdBTULUbG6lCQs1FQbcpJ9hlYsAuK885NsIzMeM3Me07N4NQUr8hHq8yMNFHRMY8Obk4NcI3NMo1MrwpJ7spJrctK7QXEtdjX6MAAMo6OMhCPy4MDrxBP6osK8dAPOlAO9x5d8Q8OaYAALVXVrdLScZqaZBmZpAAAIwzM8xPTKw7Oa80NJ8nJ7wqJ78qKMY2NJcdG95LR0pLUMMrKbkzL8s5N6coJsQ6NkJDQzlMTGQAAOFPTGgAAB9QV510dOdSTjxaYs1YVdJkYv+apddqFdhlY9dhX9pqaBUUFdZjYdNWVNZaV9BEQdRYVdBGQ9VUUdNPS9pvbNx4dhsZGtJRTtxxb9ZfXdloZdI3M89QTcdOTNx0ctZdWjAtLttaWME7ON6AftlnZNpsaiUdHsxAPNlbWM8/PNQ9OgA3Os9KRtdXVNdQTSQkJdVbWCEWFzk5PM06N9NUUdNIRUlbYqBRUbJAPt57eSopK4o/PgAPEsYsKeJwbtA7OLg1M9pXVIosKy5ISwALDQAaHVBPUdJNStpRTshTUaI8Oqg3NrMzMahDQalSUtxfXAMGCHc5OsQzMAAsMNxLR9dEQeN3dbUjIdxjYAAUFk1FRU45OwAeIQEoK9NLR75VVLgtK20yM3ssLJIyMpk2Nd5pZq1CQa4SEJVKSRweH8o0MoI7Ot9lYTYzNa0wLbtDQH1LSqEuLEhNU9FUUURGSok2NQIxM0BAQ1tZXdVoZmdiZUxKSwAlKJI2NmIvLj85OisjJHZCRLMbGbYEAEtmbpkwLrQ7OYxMTZuFhScvMC8xNRxBQ9BfXTQbHCcUFgAWGFEnJ0BQUCoYGUc/QHZVW89NSjBUWAtGSXh7fIdDQl5sb5SYmISIiJdeXw0fIEoiIrxHRRYfIRM7QMdfXSw/QL8pJlFfX5hvb5MqKXh2dmlWVgBAQYFhYaBzc6yuSJ0AAADfdFJOUwD3/hQ5OTgCAQQg+g45Tf5v/vz4Gpos1vv89/ouMf79/fFu2q3s5anDkPsk1cr+YP3ILfu+PP3898yd9PmNdE7Kk8D7hPzukuXH+Pz8a4px/v5B/vn5kv////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////7////////////////////////////////////+//////////////6TEl4rAAAESUlEQVRIx2NgGAWjgHqAgxOHODsuwAmW5AABVA14bGHnQijj5OSEKAVq52AWE2NjY+MN4mVFAF5eXllZLr9+PmYRZh5OmCVQTUrKitOT6+qS0mfOzAWBDx/27+8AgW1z5879YSAlpeqho+Ok4ijApw3SBMT6vZdbk6enz5iRLNcWHPC8evGaHj09JqaeNWurV5/beHTL7KKl2dlVS5csWRLvpeHPCdTDqbxIOLYl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r+RYyevpzgbKkkrirV3N9f1TyhCAgmTJijMGfOvn0HDx68uvns2UnOjG5qauIOGuqa8IwMKgiYuUDFBRsLEjAykpGRkeVy6efjEeHhgZdmkOKCA6oPf6EELM7ghRKIi7vsA6nlwCj68Bew7KOVzCigEAAAop3jpH2nqD8AAAAASUVORK5CYII=&quot;&gt;
&lt;svg width=&quot;175&quot; height=&quot;1300&quot; preserveAspectRatio=&quot;none&quot; viewBox=&quot;0 0 175 1300&quot; fill=&quot;none&quot;
xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
&lt;path id=&quot;my_path&quot; d=&quot;M 28 0 C -72 200 188 300 88 500 C -32 700 88 1100 148 1000 C 208 900 148 600 68 1000 C 28 1200 128 1300 128 1300&quot;
stroke-width=&quot;4&quot; stroke=&quot;#000&quot; /&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;div style=&quot;height: 150px;&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月29日 15:10:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76355317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定