CSS动画 能够取代js动画 在移动端会更加流畅!
下面是一个的绘制太阳系各大行星运行轨迹笔记,能够自学参考!javascript
首先咱们须要建立一个@keyframes规则css
@keyframes name{
from{width:1px}
to{width:100px}
}
//或者使用百分比
@keyframes name{
0%{width:1px}
100{width:100px}
}
复制代码
建立好以后,咱们须要在css选择器里引用咱们写的规则,html
<div class="box1"></div>
复制代码
.box1{
width: 0px;
height: 100px;
background-color: #00FF7F;
/* 引用 / 捆绑*/
animation: first 2s;
}
@keyframes first{
0%{width:1px}
100{width:100px}
}
复制代码
效果图: java
width
还能够改变其余的属性:height
、定位、移动、旋转、缩放等你所能想到的css属性css3动画属性很是多,我感受经常使用的是animation
的简写形式和一个动画周期须要花费的时间animation-duration
;jquery
如下也是一个小的实例:css3
<div class="horse"></div>
复制代码
html,
body {
height: 100%;
}
.horse {
width: 128px;
height: 128px;
background: url(images/Horse_256px_1096282_easyicon.net.png) no-repeat;
background-size: 100% 100%;
transform: scaleX(-1);
animation: bounce 0.1s infinite;
}
@keyframes runhorse {
0% {
transform: translate(0, 0);
animation-timing-function: cubic-bezier(0.215, 0.61, 0.355, 1);
}
25% {
transform: translate(calc(100vw - 128px), 10px) scaleY(-1);
animation-timing-function: cubic-bezier(0.755, 0.05, 0.855, 0.06);
}
50% {
transform: translate(calc(100vw - 129px), calc(100vh - 200px));
animation-timing-function: cubic-bezier(0.755, 0.05, 0.855, 0.06);
}
75% {
transform: translate(0, calc(100vh - 128px)) scaleX(-1);
}
100% {
transform: translate(10px, 10px) translate3d(0, -4px, 0);
}
}
body:hover .horse {
animation: runhorse 2s linear infinite;
}
复制代码
效果图: git
animate.css
①下载 animate.css
官方地址:animate.cssgithub
②或者css3动画
直接进入animate.css 随后右键另存为便可使用ide
③ 直接在页面顶部head标签经过link引入
基本模板以下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>动画</title>
<link rel="stylesheet" href="css/animate.css">
<style> .demo1{ font-size: 30px; font-weight: bold; color: #00008B; } </style>
</head>
<body>
<div class="demo1 animated zoomIn infinite">
Anyw3c
</div>
</body>
</html>
复制代码
效果以下:
接下来,就是对animate.css运动的一个小总结,虽然很少,可是归类后方便后面查找! /按官网顺序/
①Attention seekers
②Bouncing Entrances
③Fading Entrances
④Flippers
⑤Lightspeed
lightSpeedIn 字面意思就是光速出来喽,记得调快速度哦,什么,怎么调速我没说么?好吧,先留个坑,待会儿补。
lightSpeedOut 光速消失
⑥Rotating Entrances
rotateIn 准确说是以正中心点180度旋转渐显
rotateInDownLeft 没错,就是以左上角为中心点转下来
rotateInDownRight 就是以右上角为中心点转下来
rotateInUpLeft 就是以左上角为中心点转上去
rotateInUpRight 就是以右上角为中心点转上去
rotating Exits
rotateOut 准确说是以正中心点180度旋转渐隐
rotateOut DownLeft
rotateOut DownRight
rotateOut UpLeft
rotateOut UpRight
⑦Sliding Entrances
⑧Zoom Entrances
⑨Specials
算了,仍是在这里填吧,若想用到延时加载和控制运动过渡时间,就必需要用到jquery了,因此咱们先去找个jq引入到页面底部
Demo以下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<link rel="stylesheet" href="css/animate.css" />
<style type="text/css"> .test{ position: absolute; width: 100px; font-size: 50px; top: 50px; left: 50%; margin-left: -50px; } </style>
</head>
<body>
<div class="animated rollIn test">test</div>
<script src="js/jquery-1.12.0.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript"> $(document).ready(function(){   $('.test').css({'animation-duration':'.3s','animation-delay':'3s'}) }) </script>
</body>
</html>
复制代码
使用jq来重定义css样式,这种方法其实违背了animate简化运动代码的初衷!
参考文章:
复制代码