path动画效果

path “ d ” 属性的值用到了D3中的线段生成器

  • 要点:stroke-dasharray 和 stroke-dashoffset
    stroke-dasharray 用来设置虚线样式,后面跟两个值用来设置每一段虚线中黑线和空白的长度。
    (- - - ) <- 这是三段
    stroke-dashoffset 用来设置虚线的偏移量

  • 动画效果
    stroke-dasharray 值设为线的长度
    stroke-dashoffset 设为 0

动画效果其实是 stroke-dashoffset 值变换的过程
具体通过@keyframes设置

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="//d3js.org/d3.v3.min.js"></script>

    <style>
        .line_ {
            animation: dash 10s;
            /*-webkit-animation: dash 10s;*/

        }

       /* @-webkit-keyframes dash {
            from {
                stroke-dashoffset: 100%;
            }
            to {
                stroke-dashoffset: 0;
            }
        }*/

        @keyframes dash {
            from {
                stroke-dashoffset: 100%;
            }
            to {
                stroke-dashoffset: 0;
            }
        }
    </style>

</head>

<body>


<script>
    var svg = d3.select("body").append("svg")
        .attr("width", 1000)
        .attr("height", 1000);

    var lines1 = [[80, 80], [200, 100], [200, 200], [100, 200]];
    var lines2 = [[180, 180], [300, 200], [300, 300], [200, 300]];
    var linePath = d3.svg.line();
    svg.append("path")
        .attr("class", "line_")
        .attr("d", linePath(lines1))
        .attr("stroke", "#000000")
        .attr("fill", "transparent")
        .attr("stroke-width", "3px")
        .attr("stroke-dasharray", "100%")
        .attr("stroke-dashoffset", "0%");
    svg.append("path")
        .attr("class", "line_")
        .attr("d", linePath(lines2))
        .attr("stroke", "#000000")
        .attr("fill", "transparent")
        .attr("stroke-width", "3px")
        .attr("stroke-dasharray", "100%")
        .attr("stroke-dashoffset", "0%");
</script>
</body>

</html>

注意:我使用的是chrome浏览器,@keyframes 支持
具体什么浏览器使用哪个参见
http://www.w3school.com.cn/cssref/pr_keyframes.asp

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值