最终成果如下:
动画分成两步
- 制订运行轨迹
- 创立DOM并依照轨迹动画
制订运行轨迹
咱们先要画一条底部的淡蓝色半透明路劲做为能量流动的管道
这里用SVG的path去做(其实这里能够间接用背景图), 代码如下:
<!-- 代码是用react写的, 删除了遍历以及局部代码 -->
<svg> <!-- 工具形容提示符,被用在fill里做过滤等操作,这里是小球底部的发光 --> <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个淡蓝色线条门路 d为门路--> <path d={item.path} stroke="rgba(29,159,167,0.4)" fill="transparent" strokeWidth={5}></path> ... <!-- 这里是发光小球 通过两个圆叠加造成 --> <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>
创立DOM并依照轨迹动画
这里的外围原理通过offset-path这个属性设置静止偏移门路,再通过offset-distance来设置偏移量,这样通过css3 animation就能够让元素依照肯定的轨迹静止
<!-- 这里要保障盒子跟SVG的盒子地位重合,宽高统一,这样门路点能力统一 --><div className={styles.animate}> <!-- 这里遍历N个div,让每一个div都依照offsetPath也就是svg内path的d的值进行流动 --> <!-- animationDelay 正数示意渲染前就曾经执行, 渲染时就能够铺满整个门路 --> <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%; }}