佳木斯湛栽影视文化发展公司

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

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

熱門標(biāo)簽:阿里云 硅谷的囚徒呼叫中心 檢查注冊(cè)表項(xiàng) 百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 美圖手機(jī) 使用U盤裝系統(tǒng) 網(wǎng)站建設(shè) 智能手機(jī)

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

本文主要用到了SVG的stroke-dasharray和stroke-dashoffset特性。下圖是倒計(jì)時(shí)運(yùn)行效果:

SVG倒計(jì)時(shí)案例

下面說說相關(guān)的實(shí)現(xiàn)代碼。css實(shí)現(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("時(shí)間到!");
            circleInit()
        }
    };
    a();
    timerTimeCount=setInterval(a,1000)
};
fnTimeCount();

整個(gè)案例的代碼非常少,有喜歡的朋友可以將代碼保存到html中,運(yùn)行一下,體驗(yàn)體驗(yàn)實(shí)際效果。

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

標(biāo)簽:黃山 通遼 賀州 湖北 山南 煙臺(tái) 懷化 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SVG實(shí)現(xiàn)多彩圓環(huán)倒計(jì)時(shí)效果的示例代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    永州市| 宁安市| 称多县| 澄江县| 合阳县| 三门县| 德兴市| 阜康市| 深泽县| 年辖:市辖区| 松江区| 昭通市| 临清市| 兴文县| 德清县| 同心县| 鄱阳县| 尤溪县| 岱山县| 桂平市| 仙居县| 宿州市| 航空| 轮台县| 温泉县| 新河县| 长顺县| 崇礼县| 株洲县| 鄂伦春自治旗| 明溪县| 章丘市| 衡东县| 高清| 湖州市| 无为县| 太湖县| 全南县| 沂南县| 青河县| 牡丹江市|