html - 如何为矩形的对 Angular 线设置动画?

标签 html css animation svg

我正在尝试对 Angular 添加动画。我知道的矩形高度和宽度(动态相加)。现在尝试为一条从 N 到 L,或 O 到 M 或其他方式的线设置动画。 我尝试使用 svg 并增加该行的 x1、y1、x2、y2,但这变得越来越复杂。有更简单或更简单的解决方案吗?

enter image description here

最佳答案

你可以给你的线条一个 stroke-dashoffset 并将其设置为 0。计算 stroke-dasharraystroke-dashoffset 的值> 我用过 getTotalLength()方法计算 stroke-dasharraystroke-dashoffset 的值。希望对您有所帮助。

svg{background:#efefef;width:100vh}
rect,line{stroke:black; fill:none}

#om{stroke-dasharray:94.34px;
stroke-dashoffset:94.34px;
animation: om 1s linear forwards;}


@keyframes om {
  to {
    stroke-dashoffset: 0;
  }
}
<svg viewBox = "0 0 100 70">  
  <rect x="10" y="10" width="80" height="50" />
  <line id="om" x1="90" y1="60" x2="10" y2="10" />
</svg>

这次是用 m 到 o 和 l 到 n 的两条动画线:只需将 x1 的值更改为 x2,反之亦然。 y 也一样。这将改变线的方向。

svg{background:#efefef;width:100vh}
rect,line{stroke:black; fill:none}

#mo,#ln{stroke-dasharray:94.34px;
stroke-dashoffset:94.34px;
animation: om 1s linear forwards;}


@keyframes om {
  to {
    stroke-dashoffset: 0;
  }
}
<svg viewBox = "0 0 100 70">  
  <rect x="10" y="10" width="80" height="50" />
  <!--<line id="om" x1="90" y1="60" x2="10" y2="10" />
  <line id="nl" x1="90" y1="10" x2="10" y2="60" />-->
  <line id="mo" x2="90" y2="60" x1="10" y1="10" />
  <line id="ln" x2="90" y2="10" x1="10" y1="60" />
</svg>

我为 #om#nl 使用相同的动画

关于html - 如何为矩形的对 Angular 线设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54745545/

相关文章:

javascript - 使用 javascript 添加和删除 dom 部分

php - 随机图像问题

css - 将 CSS 位置绝对、相对和 z-index 用于 2 个大小响应(不固定)的图像

html - div 中的图像在图像下方有额外的空间

javascript - 使用纯 javascript 创建动画或效果的正确方法是什么?

javascript - CAAT - 重复行为对事件触发的影响

html - Div 对齐和形状

css - 导致 SSL 问题的字体

python - 升级到 matplotlib 3.3.2 时动画停止工作

javascript - 您能为 Web 应用程序推荐一个演示文稿幻灯片页面编辑器吗? (所见即所得,但不是 HTML)