javascript - How to anime EACH svg
问题描述
Now my code anime only one SVG with many path in my page, but I would like anime each SVG in my page. If I have many SVG in my page, only first SVG anime.
I work on my localhost for the moment
''script
{
class MorphingBG {
constructor(el) {
this.DOM = {};
this.DOM.el = el;
this.DOM.paths = Array.from(this.DOM.el.querySelectorAll('path'));
this.animate();
}
animate() {
this.DOM.paths.forEach((path) => {
setTimeout(() => {
anime({
targets: path,
duration: anime.random(3000,5000),
easing: [0.5,0,0.5,1],
d: path.getAttribute('pathdata:id'),
loop: true,
direction: 'alternate'
});
}, anime.random(0,1000));
});
}
};
new MorphingBG(document.querySelector('.svg-morph'));
};
''html
<svg class="svg-morph morph-1" viewBox="0 0 963 754">
<path d="M 739.8-218.7C456-218,480-70,209.9-85.9C39-85,56.6,133.8,61,201.5c15.1,233.2-6.4,205.1-49.5,314 C-69,811,459,815,457.4,632c2.2-161.1,601.7-37.1,635.9-382.5C1124.6-66.8,915.2-218.7,739.8-218.7 Z" pathdata:id="M 739.8-218.7C456-218,480-70,209.9-85.9C39-85,18,28,20,170c3,228-3,125-8.5,345.5C8,763,379.34,770,551.34,622 c140-157,502.46,60.4,536.66-285C1119.3,20.7,915.2-218.7,739.8-218.7 Z"></path>
</svg>
<svg class="svg-morph morph-2" version="1.1" viewBox="0 0 735.6 807">
<path d="M 739.8-218.7C456-218,480-70,209.9-85.9C39-85,56.6,133.8,61,201.5c15.1,233.2-6.4,205.1-49.5,314 C-69,811,459,815,457.4,632c2.2-161.1,601.7-37.1,635.9-382.5C1124.6-66.8,915.2-218.7,739.8-218.7 Z" pathdata:id="M 739.8-218.7C456-218,480-70,209.9-85.9C39-85,18,28,20,170c3,228-3,125-8.5,345.5C8,763,379.34,770,551.34,622 c140-157,502.46,60.4,536.66-285C1119.3,20.7,915.2-218.7,739.8-218.7 Z"></path>
</svg>
For the moment only svg class="svg-morph morph-1" it's animate
解决方案
只需替换这一行:
new MorphingBG(document.querySelector('.svg-morph'));
使用此代码:
// get all elements that you want to morph (not only one)
let toBeMorphed = document.querySelectorAll('.svg-morph');
// apply the MorphingOperator on each of these elements
toBeMorphed.forEach(function(svg) {
new MorphingBG(svg);
});
推荐阅读
- javascript - Vue-simple-suggest 在从 vuex 加载数据时将 v-model 转换为 null
- python - Discord 版主机器人
- r - R:将连续项目配对到新的单独列中
- java - Java中的问题冒泡排序泛型类型
- firebase - 如何在firebase托管的一台服务器上部署nodejs服务器(发送电子邮件)和vuejs应用程序
- stenciljs - 需要帮助为 Stencil 设置类名
- javascript - 如何在 laravel 中使用日期选择器?
- jquery - 尝试使用 jquery 将值传递给 mvc 控制器
- elasticsearch-7 - 非嵌套对象的ES范围查询和嵌套对象的查询词
- ios - 如何在 SwiftUI 中创建 2 个计时器?