濮阳杆衣贸易有限公司

主頁 > 知識庫 > SVG實現(xiàn)多彩圓環(huán)倒計時效果的示例代碼

SVG實現(xiàn)多彩圓環(huán)倒計時效果的示例代碼

熱門標簽:云南大理400電話申請官方 江蘇智能電銷機器人哪家好 電銷機器人電話用什么卡 南寧點撥外呼系統(tǒng)哪家公司做的好 成都智能外呼系統(tǒng)平臺 黃島區(qū)地圖標注 鎮(zhèn)江智能外呼系統(tǒng)有效果嗎 當涂高德地圖標注 四川點撥外呼系統(tǒng)

圓環(huán)倒計時我們經(jīng)常見到,實現(xiàn)的方法也有很多種。但是本文將介紹一種全新的實現(xiàn)方式,使用SVG來實現(xiàn)倒計時功能。

本文主要用到了SVG的stroke-dasharray和stroke-dashoffset特性。下圖是倒計時運行效果:

SVG倒計時案例

下面說說相關(guān)的實現(xiàn)代碼。css實現(xiàn)代碼如下:

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;
}

相關(guān)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>

最后是相關(guān)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();

整個案例的代碼非常少,有喜歡的朋友可以將代碼保存到html中,運行一下,體驗體驗實際效果。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標簽:咸寧 南京 廣西 淮安 酒泉 西寧 十堰 佳木斯

巨人網(wǎng)絡(luò)通訊聲明:本文標題《SVG實現(xiàn)多彩圓環(huán)倒計時效果的示例代碼》,本文關(guān)鍵詞  SVG,實現(xiàn),多彩,圓環(huán),倒計時,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SVG實現(xiàn)多彩圓環(huán)倒計時效果的示例代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于SVG實現(xiàn)多彩圓環(huán)倒計時效果的示例代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    绥中县| 卫辉市| 南阳市| 宜君县| 济南市| 桐梓县| 井冈山市| 瑞金市| 丰台区| 西乡县| 南召县| 磐安县| 浦县| 历史| 穆棱市| 荔波县| 江山市| 八宿县| 历史| 峨边| 清徐县| 神池县| 石楼县| 通辽市| 尖扎县| 维西| 墨竹工卡县| 双峰县| 鄱阳县| 保德县| 乌恰县| 孟村| 固始县| 浦东新区| 乌兰察布市| 沽源县| 宜宾县| 读书| 大城县| 资溪县| 双峰县|