SVG动画之描边动画

svg描边动画使用stroke-dasharray与stroke-dashoffset两个属性:

一、stroke-dasharray

stroke-dasharray属性可将svg图形的路径断为虚线,如下代码是一条直线:

<svg x="0" y="0" width="500px" height="100px" viewBox="0 0 500 100">
        <line id="line" x1="0" y1="6" x2="500" y2="6" stroke="#3896fe"></line>
</svg>

如添加stroke-dasharray:10 10属性后,变为下图:

可见直线被分为了间隔为10px的虚线,若实现描边效果只需将stroke-dasharray设置为路径长度即可,使用以下代码可获取路径长度:

document.querySelector('#path').getTotalLength();

二、stroke-dashoffset

stroke-dashoffset可设置路径的偏移量,设置该值后,路径就会偏移设置的值,如上图使用的直线,配合使用stroke-dasharray属性可将直线消失:

#line{
    stroke-dasharray: 500 500;
    stroke-dashoffset: 500;
}

利用以上两个属性配合CSS3的animation动画即可实现SVG的描边效果,以下是利用SVG描边做的动画效果:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .checkmark{
            width: 52px;
            height:52px;
            display: block;
            stroke:#ffffff;
            border-radius: 50%;
            box-shadow: inset 0px 0px 0px #7ac142;
            animation: fill .5s ease-in-out .4s forwards,scale .3s ease-in-out .9s both;
        }
        #circle{
            stroke-dasharray: 156.07 156.07;
            stroke-width: 2;
            fill: none;
            stroke: #7ac142;
            animation: stroke 0.6s cubic-bezier(0.65,0,0.45,1) forwards;
        }
        #path{
            stroke-dasharray: 48;
            fill:none;
            stroke-dashoffset: 48;
            animation: stroke2 0.6s cubic-bezier(0.65,0,0.45,1) .8s forwards;
        }
        @keyframes stroke {
            0%{
                stroke-dashoffset: 156.07;
            }
            100%{
                stroke-dashoffset: 0;
            }
        }
        @keyframes stroke2 {
            100%{
                stroke-dashoffset: 0;
            }
        }
        @keyframes fill {
            100%{
                box-shadow: inset 0 0 0 30px #7ac142;
            }
        }
        @keyframes scale {
            100%{
                transform: scale3d(1.1,1.1,1);
            }
        }
    </style>
<body>
    <svg viewBox="0 0 52 52" class="checkmark">
        <circle id="circle" cx="26" cy="26" r="25" fill="none"></circle>
        <path id="path" d="M14.1 27.2 l7.1 7.2 16.7-16.8" fill="none"></path>
    </svg>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MAXLZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值