概述

我们常常在网站中看到一些画线的动画效果,非常炫酷,大多数这种画线动画效果是通过SVG实现的,也有不少是用纯css实现的,下面我总结了一下这2种方法,供以后开发时参考,相信对其他人也有用。

参考资料:

移动端网页

PC端网页

SVG实现画线效果

在svg里面,可以用stroke-dashoffset属性来实现画线效果,示例如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>svg</title>
<style>
path{
stroke-dasharray: 1000;
stroke-dashoffset: 1000;
animation: draw 5s ease 3;
}
@keyframes draw{
0%{
stroke-dashoffset: 1000;
}
100%{
stroke-dashoffset: 0;
}
}
</style>
</head>
<body>
<svg width="400px" height="400px" viewBox="100 250 400 400" version="1.1" xmlns="http://www.w3.org/2000/svg">
<path d="M153 334
C153 334 151 334 151 334
C151 339 153 344 156 344
C164 344 171 339 171 334
C171 322 164 314 156 314
C142 314 131 322 131 334
C131 350 142 364 156 364
C175 364 191 350 191 334
C191 311 175 294 156 294
C131 294 111 311 111 334
C111 361 131 384 156 384
C186 384 211 361 211 334
C211 300 186 274 156 274"
style="fill:white;stroke:red;stroke-width:2"/>
</svg>
</body>
</html>

需要注意的是,必须要设置stroke-dasharray属性,并且和stroke-dashoffset一样长;其中1000是个自定义的值,只要比画线的总长度长就行了。

纯css画线

对于长方形或者正方形这些规则的图形,用css的transition效果即可,代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>svg</title>
<style>
#border {
position: relative;
width: 300px;
height: 300px;
}
.border-top {
position: absolute;
top: 50px;
left: 50px;
width: 0;
height: 8px;
background-color: #333;
transition: all .5s 1s;
}
.border-bottom {
position: absolute;
bottom: 50px;
right: 50px;
width: 0;
height: 8px;
background-color: #333;
transition: all .5s 2s;
}
.border-right {
position: absolute;
top: 50px;
left: 250px;
width: 8px;
height: 0;
background-color: #333;
transition: all .5s 1.5s;
}
.border-left {
position: absolute;
bottom: 50px;
left: 50px;
width: 8px;
height: 0;
background-color: #333;
transition: all .5s 2.5s;
}
.animate .border-top, .animate .border-bottom {
width: 200px;
}
.animate .border-right, .animate .border-left {
height: 200px;
}
</style>
</head>
<body>
<div id="border">
<i class="border-top"></i>
<i class="border-right"></i>
<i class="border-bottom"></i>
<i class="border-left"></i>
</div>
<script>
setTimeout(function() {
document.getElementById('border').className += ' animate';
}, 500);
</script>
</body>
</html>

画线动画——SVG版和纯CSS版的更多相关文章

  1. WPF 画线动画效果实现

    原文:WPF 画线动画效果实现 弄了将近三天才搞定的,真是艰辛的实现. 看了很多博客,都太高深了,而且想要实现的功能都太强大了,结果基础部分一直实现不了,郁闷啊~ 千辛万苦终于找到了一个Demo,打开 ...

  2. HTML自定义radio单选按钮(纯css版,样式可以随意改变)

    html: <div> <input id="item1" type="radio" name="item" value= ...

  3. 纯CSS制作加<div>制作动画版哆啦A梦

    纯CSS代码加上<div>制作动画版哆啦A梦(机器猫) 哆啦A梦(机器猫)我们大家一定都很熟悉,今天给大家演示怎么用纯CSS代码,来做一个动画版的哆啦A梦. 效果图: ###下面代码同学可 ...

  4. 纯CSS实现帅气的SVG路径描边动画效果(转载)

    本文转载自: 纯CSS实现帅气的SVG路径描边动画效果

  5. 纯JS画点、画线、画圆的方法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 用纯css画个三角形

    用纯css画个三角形以下是源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  7. [stm32] 利用uc-gui封装画图和画线函数移植51上的模拟动画

    >_<:这里的动画是黄色矩形区域中一个模仿俯视图的起重机运作动画,一个是模仿主视图的吊钩的运动.通过改变初始Init函数中的数据b_x,b_y实现矩形区域的移动.当实时采集时要首先根据起重 ...

  8. 纯css做的安卓开机动画

    随着css3的发展,越来越多的负责绚丽的效果可以由纯css来完成了.用css3实现的动画效果丝毫不必js实现的逊色,而且浏览器对css渲染的速度远比js快,大多数时候css的体积也不js小.其中css ...

  9. 一个纯CSS DIV天气动画图标【转扒的】

    <p> </p> <style><!-- /* SUNNY */ .sunny { -webkit-animation: sunny 15s linear i ...

随机推荐

  1. CentOS 7 建立svn仓库 远程连接

    首先安装svn (后补) mikdir /usr/local/svn_repertory  # 创建svn大仓库用于存放所有项目代码 cd  /usr/local/svn_repertory  # 进 ...

  2. python基础之Day20part1

    一.hash算法 什么是hash? 类似工厂加工的过程,传bytes串,经过运算返回字符 hash相当于工厂,传给hash算法的内容是原材料,hash值为产品 为何用hash? hash三大特性: 1 ...

  3. Django使用cropbox包来上传裁剪图片

    1.使用cropbox包来上传裁剪图片,可见介绍:https://www.jianshu.com/p/6c269f0b48c0I ImgCrop包包括:css--style.css,js--cropb ...

  4. Reveal Cards In Increasing Order LT950

    In a deck of cards, every card has a unique integer.  You can order the deck in any order you want. ...

  5. 浅析b-树 b+树 以及Mysql的Innodb,Myisam引擎

    B-树性质 B-树可以看作是对2-3查找树的一种扩展,即他允许每个节点有M-1个子节点. 1根节点至少有两个子节点 2每个节点有M-1个key,并且以升序排列 3位于M-1和M key的子节点的值位于 ...

  6. zabbix3.0监控Windows服务器

    我们下载后,解压开始有多个文件: conf目录存放是agent配置文件bin文件存放windows下32位和64位安装程序 找到conf下的配置文件 zabbix_agentd.win.conf ,修 ...

  7. Chapter5_初始化与清理_成员初始化

    在java中,成员初始化在使用之前应该都要保证已经完成初始化.对于在方法体中的局部变量,如果没有使用指定初始化的方法对成员变量进行初始化,编译器会提示一个错误.而对于类的数据成员,编译器会对这些成员赋 ...

  8. 走进JDK(十二)------TreeMap

    一.类定义 TreeMap的类结构: public class TreeMap<K,V> extends AbstractMap<K,V> implements Navigab ...

  9. Nexus安装、使用说明、问题总结

    Nexus安装.使用说明.问题总结 1 . 私服简介 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件.有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在 ...

  10. lombok学习

    lombok的官方地址:https://projectlombok.org/ lombok的Github地址:https://github.com/rzwitserloot/lombok lombok ...