SVG stroke-dasharray 、stroke-dashoffset

stroke-dasharray

stroke-dasharray用来绘制路径虚线。可以有一个或者多个参数,下面看一下它在不同参数下的效果。

1.不添加stroke-dasharray属性

#rect {
	stroke-width: 6px;
	fill: yellow;
	stroke: red;
	/*stroke-dasharrry:10;*/
}
<svg height="100" width="300">
    <rect id="rect" height="100" width="300" />
</svg>

图1:矩形的边框为实线
图1

2.一个参数

stroke-dasharray:10;

图2:矩形的边框为虚线。红色线段和间隙都是10px;
图2

3.两个参数

stroke-dasharray:10 20;

图3:矩形的边框为虚线。红色线段:10px , 间隙是20px;
图3
4.三个参数

stroke-dasharray:10 20 30;

图4:红色线段和间隙长短不一。从矩形绘制的起点左上角开始:红->10,间隙->20,红->30,间隙->10,…
在这里插入图片描述
可以发现它是以所给的参数为一个周期,从路径起点开始进行循环绘制。

stroke-dashoffset

stroke-dashoffset 是对整条虚线从起点开始向相反的方向做偏移。

stroke-dashoffset:100;

图5:向反方向偏移100px;
在这里插入图片描述

应用:

stroke-dashoffset动画

#rect {
	stroke-width: 6px;
	fill: #fff;
	stroke: red;
	stroke-dasharray: 20;
	animation: path-animation 5s;
	animation-timing-function:linear;
	animation-iteration-count:infinite;
}
		
@keyframes path-animation {
  from {
	stroke-dashoffset: 100%; 
  }
  to {
	stroke-dashoffset: 0%; 
  } 
}

动画效果
动画

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值