SVG怎样开始实现多彩圆环倒计时


本文摘自PHP中文网,作者php中世界最好的语言,侵删。

这次给大家带来SVG怎样开始实现多彩圆环倒计时,怎样用SVG实现多彩圆环倒计时?SVG实现多彩圆环倒计时的注意事项有哪些,下面就是实战案例,一起来看一下。

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

SVG倒计时案例

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<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代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

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中文网其它相关文章!

相关阅读:

H5的所有元素及基本语法归纳

html5怎样操作indexedDB

关于老版本的浏览器不兼容H5和C3的处理方法

以上就是SVG怎样开始实现多彩圆环倒计时的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

用h5做出微信的支付过程的实现步骤

h5做出手机摇一摇功能的实现步骤

什么是js深拷贝和浅拷贝及其实现方式

滚动条的简单实现

怎么能在页面上实现一个圆形可点击区域?

js如何实现自定义鼠标右击菜单

在html中select标签怎样实现单选和多选

h5手机扫码怎么实现

h5动画--canvas绘制圆环百分比进度的实例

h5的拖放应该如何实现

更多相关阅读请进入《圆环》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...