首页前端开发HTMLSVG实现多彩圆环倒计时效果的示例代码

SVG实现多彩圆环倒计时效果的示例代码

时间2024-01-24 21:42:19发布访客分类HTML浏览503
导读:收集整理的这篇文章主要介绍了SVG实现多彩圆环倒计时效果的示例代码,觉得挺不错的,现在分享给大家,也给大家做个参考。 圆环倒计时我们经常见到,实现的方法也有很多种。但是本文将介绍一种全新的实现方式,使用SVG来实现倒计时功能。本文...
收集整理的这篇文章主要介绍了SVG实现多彩圆环倒计时效果的示例代码,觉得挺不错的,现在分享给大家,也给大家做个参考。

圆环倒计时我们经常见到,实现的方法也有很多种。但是本文将介绍一种全新的实现方式,使用SVG来实现倒计时功能。

本文主要用到了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代码如下:

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(b0){
                clearInterval(timerTimeCount);
                timerTimeCount=null;
                alert("时间到!");
            circleInit()        }
    }
    ;
        a();
    timerTimeCount=setInterval(a,1000)}
    ;
    fnTimeCount();
    

整个案例的代码非常少,有喜欢的朋友可以将代码保存到html中,运行一下,体验体验实际效果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

上一篇: HTML5打开手机扫码功能及优缺点下一篇:HTML5响应式(自适应)网页设计的...猜你在找的html5相关文章 关于移动端h5开发相关内容总结2022-05-17html5教程-学表单的第二天2018-12-10html5教程-HTML5浏览器支持2018-12-10html5教程-HTML5智能表单2018-12-10html5教程-微信H5使用resLoader实现加载页效果2018-12-10html5教程-day01-初级-JS0-热身运动JS入门教程2018-12-10html5教程-html5shiv.js和respond.min.js2018-12-10html5教程-不同浏览器对于html5 audio标签和音频格式的兼容性2018-12-10html5教程-使用Html5实现手风琴案例2018-12-10html5教程-html5笔记2018-12-10 其他相关热搜词更多phpjavapython程序员load

若转载请注明出处: SVG实现多彩圆环倒计时效果的示例代码
本文地址: https://pptw.com/jishu/585782.html
canvas进阶之贝塞尔公式推导与物体跟随复杂曲线的轨迹运动 HTML5响应式(自适应)网页设计的实现

游客 回复需填写必要信息