這次帶給大家SVG怎麼開始實現多彩圓環倒數計時,怎樣用SVG實現多彩圓環倒數計時? SVG實現多彩圓環倒數計時的注意事項有哪些,以下就是實戰案例,一起來看一下。
圓環倒數計時我們常見到,實現的方法也有很多種。但本文將介紹一種全新的實作方式,使用SVG來實作倒數功能。
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程式碼如下:
<div id="timeCountX" class="time-count-x"> <svg width="440" height="440" viewBox="0 0 440 440" class="center"> <defs> <linearGradient x1="1" y1="0" x2="0" y2="0" id="gradient1"> <stop offset="0%" stop-color="#e52c5c"></stop> <stop offset="100%" stop-color="#ab5aea"></stop> </linearGradient> <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 circle"); var eleTimeSec=document.getElementById("timeSecond"); var perimeter=Math.PI*2*170; var circleInit=function(){ if(eleCircles[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=b||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(eleTimeSec){ eleTimeSec.innerHTML=b } b--; if(b<0){ clearInterval(timerTimeCount); timerTimeCount=null; alert("时间到!"); circleInit() } }; a(); timerTimeCount=setInterval(a,1000) }; fnTimeCount();
相信看了這些案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
相關閱讀:
以上是SVG怎麼開始實現多彩圓環倒數計時的詳細內容。更多資訊請關注PHP中文網其他相關文章!