SVG实现多彩圆环倒计时效果

2018-01-08 前端开发 访问量 184

圆环倒计时我们经常见到,实现的方法也有很多种。但是本文将介绍一种全新的实现方式,使用SVGhttp://www.ddwclub.com/2ac4/4428506c8d96.html来实现倒计时功能。

本文主要用到了SVG的stroke-dasharray和stroke-dashoffset特性。下图是倒计时运行效果:

SVG倒计时案例

下面说说相关的实现代码。css实现代码如下:

svg {
    transform: rotate(-0.05deg);
}
circle {
    transition: stroke-dasharray .2s;
}
.time-count-x {
    line-height: 1.5;
    position: relative;
}
.time-second {
    position: absolute;
    top: 50%; left: 0; right: 0;
    margin-top: -.75em;
    text-align: center;
    font-size: 100px;
}

相关html代码如下:

<http://www.ddwclub.com/67ef/2f4814d02fa3.html;div id="timeCountX" class="time-count-x">
    <svg width="440" height="440" viewBox="0 0 440 440" class="center">
        <defs>
            <linearGhttp://www.ddwclub.com/da01/f8fee2fb3ad7.htmlradient x1="1" y1="0" x2="0" y2="0" id="gradient1">
                <stop offset="0%" stop-color="#e52c5c"&ghttp://www.ddwclub.com/234a/9c2875407ddf.htmlt;</stop>
                <stop offset="100%" stop-color="#ab5aea"></stop>
            </linearGradient>
 http://www.ddwclub.com/c7b4/f7ee5f07349c.html          <linearGradient x1="1" y1="0" x2="0" y2="0" id="gradient2">
                <stop offset="0%" stop-color="#4352f3"></stop>
                <stop offset="100%" stop-color="#ab5aea"></stop>
            </linearGradient>
        </defs>
        <g transform="matrix(0,-1,1,0,0,440)">
            <circle cx="220" cy="220" r="170" stroke-width="50" stroke="#f0f1f5" fill="none" stroke-dasharray="1069 1069"></circle>
            <circle cx="220" cy="220" r="170" stroke-width="50" stroke="url('#gradient1')" fill="none" stroke-dasharray="1069 1069"></circle>
            <circle cx="220" cy="220" r="170" stroke-width="50" stroke="url('#gradient2')" fill="none" stroke-dasharray="534.5 1069"></circle>
        </g>
    </svg>
    <span id="timeSecond" class="time-second"></span>
</div>

最后是相关JavaScript代码:

var eleCircles=document.querySelectorAll("#timeCountX circhttp://www.ddwclub.com/84c8/1316d01d8504.htmlle");
var eleTimeSec=document.getElementById("timeSecond");
var perimeter=Math.PI*2*170;
var circleInit=function(){
	if(elhttp://www.ddwclub.com/6422/4c03c233df05.htmleCircles[1]){
		eleCircles[1].setAttribute("stroke-dasharray","1069 1069")
	}
	if(eleCircles[2]){
		eleCircles[2].setAttribute("stroke-dasharray",perimeter/2+" 1069")
	}
	eleTimeSec.innerHTML=""
};
var timerTimeCount=null;
var fnTimeCount=function(b){
	if(timerTimeCount){
		return
	}
	var b=bhttp://www.ddwclub.com/8c6e/aab35f6d9b93.html||10;
	var a=function(){
		var c=b/10;
		if(eleCircles[1]){
			eleCircles[1].setAttribute("stroke-dasharray",perimeter*c+" 1069")
		}
		if(eleCircles[2]&&b<=5){
			eleCircles[2].setAttribute("stroke-dasharray",perimeter*c+" 1069")
		}
		if(eleThttp://www.ddwclub.com/9ca2/c23d94afc04c.htmlimeSec){
			eleTimeSec.innerHTML=b
		}
		b--;
		if(b<0){
			clearInterval(timerTimeCount);
			timerTimeCount=null;
			alert("时间到!");
			circleInit()
		}
	};
	a();
	timerTimeCount=setInterval(a,1000)
};
fnTimeCount();

整个案例的代码非常少,有喜欢的朋友可以将代码保http://www.ddwclub.com/8782/0b24d98ba0f3.html存到html中,运行一下,体验体验实际效果。

本站是基于ThinkPHP3.2.3版本框架开发 Theme by WordPress 粤ICP备15112997号-1