最終效果如下:

動(dòng)畫(huà)分成兩步
- 制定運(yùn)行軌跡
- 創(chuàng)建DOM并按照軌跡動(dòng)畫(huà)
制定運(yùn)行軌跡
我們先要畫(huà)一條底部的淡藍(lán)色半透明路勁做為能量流動(dòng)的管道
這里用SVG的path去做(其實(shí)這里可以直接用背景圖), 代碼如下:
<!-- 代碼是用react寫(xiě)的, 刪除了遍歷以及部分代碼 -->
<svg>
<!-- 工具描述提示符,被用在fill里做過(guò)濾等操作,這里是小球底部的發(fā)光 -->
<defs>
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style={{ stopColor: "rgba(2,246,255,.5)" }} />
<stop offset="100%" style={{ stopColor: "rgba(2,246,255,0)" }} />
</radialGradient>
</defs>
<!-- 這里遍歷N個(gè)淡藍(lán)色線(xiàn)條路徑 d為路徑-->
<path d={item.path} stroke="rgba(29,159,167,0.4)" fill="transparent" strokeWidth={5}></path>
...
<!-- 這里是發(fā)光小球 通過(guò)兩個(gè)圓疊加形成 -->
<g>
<circle cx={cx} cy={cy} r="15" fill="url(#grad1)"></circle>
<circle cx={cx} cy={cy} r="5" fill="rgba(2,246,255)"></circle>
</g>
</svg>
創(chuàng)建DOM并按照軌跡動(dòng)畫(huà)
這里的核心原理通過(guò)offset-path這個(gè)屬性設(shè)置運(yùn)動(dòng)偏移路徑,再通過(guò)offset-distance來(lái)設(shè)置偏移量,這樣通過(guò)css3 animation就可以讓元素按照一定的軌跡運(yùn)動(dòng)
<!-- 這里要保證盒子跟SVG的盒子位置重合,寬高一致,這樣路徑點(diǎn)才能一致 -->
<div className={styles.animate}>
<!-- 這里遍歷N個(gè)div,讓每一個(gè)div都按照offsetPath也就是svg內(nèi)path的d的值進(jìn)行流動(dòng) -->
<!-- animationDelay 負(fù)數(shù)表示渲染前就已經(jīng)執(zhí)行, 渲染時(shí)就可以鋪滿(mǎn)整個(gè)路徑 -->
<div key={index} className={styles.point3} style={{ "offsetPath": "path('M 105 34 L 5 34')", "animationDelay": `-${index * 1}s`, "animationDuration": '5s', 'animationPlayState': `${stop ? 'paused' : 'running'}` }}></div>
...
</div>
.point3 {
width: 10px;
height: 2px;
// offset-path: path('M 248 108 L 248 172 L 1510 172');
offset-distance: 0%;
animation: flow 20s linear normal infinite;
background-image: linear-gradient(to right, rgba(255, 255, 255, 0) 10%, #FEFE02);
position: absolute;
left: 0;
right: 0;
}
}
@keyframes flow {
from {
offset-distance: 0%;
}
to {
offset-distance: 100%;
}
}
到此這篇關(guān)于css animation配合SVG制作能量流動(dòng)效果的文章就介紹到這了,更多相關(guān)css animation配合SVG內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!