早上无心间进入一个网站,看到他们的LOGO效果略屌,如图:css
刚开始觉得是gif动画之类的,审查元素发现竟然是用SVG + CSS3动画实现的,顿时激起了个人(hao)欲(qi)望(xin),决定要一探究竟,查看代码以后,发现原理竟然是如此简单:多个SVG描边动画使用不一样的animation-delay便可!html
对于一个形状SVG元素或文本SVG元素,能够使用stroke-dasharray来控制描边的间隔样式,而且能够用stroke-dashoffset来控制描边的偏移量,借此能够实现描边动画效果,更具体的资料能够看看张大神的《纯CSS实现帅气的SVG路径描边动画效果》segmentfault
咱们先实现一个简单的文字描边动画:svg
<svg width="100%" height="100"> <text text-anchor="middle" x="50%" y="50%" class="text"> segmentfault.com </text> </svg>
.text{ font-size: 64px; font-weight: bold; text-transform: uppercase; fill: none; stroke: #3498db; stroke-width: 2px; stroke-dasharray: 90 310; animation: stroke 6s infinite linear; } @keyframes stroke { 100% { stroke-dashoffset: -400; } }
演示地址:http://output.jsbin.com/demic...wordpress
而后咱们同时使用多个描边动画,并设置不一样的animation-delay:动画
<svg width="100%" height="100"> <text text-anchor="middle" x="50%" y="50%" class="text text-1"> segmentfault.com </text> <text text-anchor="middle" x="50%" y="50%" class="text text-2"> segmentfault.com </text> <text text-anchor="middle" x="50%" y="50%" class="text text-3"> segmentfault.com </text> <text text-anchor="middle" x="50%" y="50%" class="text text-4"> segmentfault.com </text> </svg>
注意:要使用多少种颜色,就放多少个text网站
.text{ font-size: 64px; font-weight: bold; text-transform: uppercase; fill: none; stroke-width: 2px; stroke-dasharray: 90 310; animation: stroke 6s infinite linear; } .text-1{ stroke: #3498db; text-shadow: 0 0 5px #3498db; animation-delay: -1.5s; } .text-2{ stroke: #f39c12; text-shadow: 0 0 5px #f39c12; animation-delay: -3s; } .text-3{ stroke: #e74c3c; text-shadow: 0 0 5px #e74c3c; animation-delay: -4.5s; } .text-4{ stroke: #9b59b6; text-shadow: 0 0 5px #9b59b6; animation-delay: -6s; } @keyframes stroke { 100% { stroke-dashoffset: -400; } }
大功告成,演示地址:http://output.jsbin.com/vuyuv...spa
须要注意的几个点:code