2016-03-15 88 views
3

我需要创建一个“X”复选标记动画(对于失败)。如何使用SVG + CSS绘制X Sign?

我发现了一个很好的动画“v”复选标记示例(成功)。代码使用曲线贝塞尔设计。 我试过阅读并试图做一个X标志,但没有成功。

你能帮我吗?

的链接,在“V”对号是: http://codepen.io/haniotis/pen/KwvYLO

.checkmark__circle { 
 
    stroke-dasharray: 166; 
 
    stroke-dashoffset: 166; 
 
    stroke-width: 2; 
 
    stroke-miterlimit: 10; 
 
    stroke: #7ac142; 
 
    fill: none; 
 
    animation: stroke 0.6s cubic-bezier(0.65, 0, 0.45, 1) forwards; 
 
} 
 
.checkmark { 
 
    width: 56px; 
 
    height: 56px; 
 
    border-radius: 50%; 
 
    display: block; 
 
    stroke-width: 2; 
 
    stroke: #fff; 
 
    stroke-miterlimit: 10; 
 
    margin: 10% auto; 
 
    box-shadow: inset 0px 0px 0px #7ac142; 
 
    animation: fill .4s ease-in-out .4s forwards, scale .3s ease-in-out .9s both; 
 
} 
 
.checkmark__check { 
 
    transform-origin: 50% 50%; 
 
    stroke-dasharray: 48; 
 
    stroke-dashoffset: 48; 
 
    animation: stroke 0.3s cubic-bezier(0.65, 0, 0.45, 1) 0.8s forwards; 
 
} 
 
@keyframes stroke { 
 
    100% { 
 
    stroke-dashoffset: 0; 
 
    } 
 
} 
 
@keyframes scale { 
 
    0%, 100% { 
 
    transform: none; 
 
    } 
 
    50% { 
 
    transform: scale3d(1.1, 1.1, 1); 
 
    } 
 
} 
 
@keyframes fill { 
 
    100% { 
 
    box-shadow: inset 0px 0px 0px 30px #7ac142; 
 
    } 
 
}
<svg class="checkmark" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 52 52"> 
 
    <circle class="checkmark__circle" cx="26" cy="26" r="25" fill="none" /> 
 
    <path class="checkmark__check" fill="none" d="M14.1 27.2l7.1 7.2 16.7-16.8" /> 
 
</svg>

+0

https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d – mplungjan

回答

12

你只需要改变路径定义,形成一个“X”,改变了虚线图案和偏移以补偿改变的路径长度。

我已经简单了,这里只是X的两条腿同时被绘制。有了额外的<path>元素和一些额外的CSS,你可以使它在两个步骤中绘制X.

.checkmark__circle { 
 
    stroke-dasharray: 166; 
 
    stroke-dashoffset: 166; 
 
    stroke-width: 2; 
 
    stroke-miterlimit: 10; 
 
    stroke: #7ac142; 
 
    fill: none; 
 
    animation: stroke 0.6s cubic-bezier(0.65, 0, 0.45, 1) forwards; 
 
} 
 
.checkmark { 
 
    width: 56px; 
 
    height: 56px; 
 
    border-radius: 50%; 
 
    display: block; 
 
    stroke-width: 2; 
 
    stroke: #fff; 
 
    stroke-miterlimit: 10; 
 
    margin: 10% auto; 
 
    box-shadow: inset 0px 0px 0px #7ac142; 
 
    animation: fill .4s ease-in-out .4s forwards, scale .3s ease-in-out .9s both; 
 
} 
 
.checkmark__check { 
 
    transform-origin: 50% 50%; 
 
    stroke-dasharray: 29; 
 
    stroke-dashoffset: 29; 
 
    animation: stroke 0.3s cubic-bezier(0.65, 0, 0.45, 1) 0.8s forwards; 
 
} 
 
@keyframes stroke { 
 
    100% { 
 
    stroke-dashoffset: 0; 
 
    } 
 
} 
 
@keyframes scale { 
 
    0%, 100% { 
 
    transform: none; 
 
    } 
 
    50% { 
 
    transform: scale3d(1.1, 1.1, 1); 
 
    } 
 
} 
 
@keyframes fill { 
 
    100% { 
 
    box-shadow: inset 0px 0px 0px 30px #7ac142; 
 
    } 
 
}
<svg class="checkmark" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 52 52"> 
 
    <circle class="checkmark__circle" cx="26" cy="26" r="25" fill="none" /> 
 
    <path class="checkmark__check" fill="none" d="M16 16 36 36 M36 16 16 36" /> 
 
</svg>

+0

这是伟大的。非常感谢!从哪里可以学习如何做这样的事情?你有这个好的教程吗? – EVH671

+0

网上有很多教程描述SVG路径以及如何使用短划线技巧创建线条绘制动画。只需使用你最喜欢的搜索引擎:) –

+0

#FF0000而不是#7ac142可能比现在更像错误:) – mplungjan