skyweaver 手把手教你写css3通用动画
之前接了几个微信里的项目,类似电子邀请函,什么分析报告这样的项目, 对css3动画要求十分高,每个页面客户几乎都有天马行空的想法,或者说设计师有这样的想法。
众所周知css3里的keyframe写好了就不能赋值复用的, 所以怎样能把keyframe通用起来就异常关键了。
好,下面先上几个之前写的项目:(今天sae挂了 ,只好用别人的生产地址),手机打开或者chrome模拟看哦!
http://www.51qianlima.cn/bim/
http://wx.mgcc.com.cn/fotile/invite/index.html?leader=2&from=timeline&isappinstalled=0
仔细看你就发现页面的动画有渐显,有飞入,而且可能2者或多者同时存在。
那么问题来了:是不是每个页面的keyframe都得单独写?
在说这个问题之前,你可能得掌握几种常用的水平居中,垂直居中,中心居中;水平偏移,垂直偏移的写法(以下demo效果请chrome模拟手机端打开,-webkit内核哦)。
demo1:postion结合margin:auto

<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,minimal-ui"><title></title><style>
* {
padding:0;
margin:0;}
html,body {
height:100%;}
.demo {
width:100px;
height: 100px;
text-align: center;
line-height: 100px;
background:lightblue;}
.demo-1 {
position: absolute;
margin:0 auto;
left:0;
right:0;}
.demo-2 {
position: absolute;
margin:auto 0;
top:0;
bottom:0;}
.demo-3 {
position: absolute;
margin:auto;
top:0;
bottom:0;
left:0;
right:0;}</style></head><body><div class="demo demo-1">水平居中</div><div class="demo demo-2">垂直居中</div><div class="demo demo-3">中心居中</div></body></html>

demo2:postion和translate 一起使用达到水平、垂直居中

<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,minimal-ui"><title></title><style>
* {
padding:0;
margin:0;}
html,body {
height:100%;}
.demo {
width:100px;
height: 100px;
text-align: center;
line-height: 100px;
background:peachpuff;}
.demo-1 {
position: absolute;
left:50%;
-webkit-transform:translateX(-50%);}
.demo-2 {
position: absolute;
top:50%;
-webkit-transform:translateY(-50%);}
.demo-3 {
position: absolute;
top:50%;
left:50%;
-webkit-transform:translateX(-50%) translateY(-50%);}</style></head><body><div class="demo demo-1">水平居中</div><div class="demo demo-2">垂直居中</div><div class="demo demo-3">中心居中</div></body></html>

好 掌握了以上2个demo的写法和区别后,正在的项目上 ,往往用到的是根据水平中心点,或者垂直中心点偏移的写法,因为手机屏幕大小不一,所以这个写法异常常用。
demo3:中心点偏移

<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,minimal-ui"><title></title><style>
* {
padding:0;
margin:0;}
html,body {
height:100%;}
.demo {
width:100px;
height: 100px;
text-align: center;
background:lightblue;}
.demo-1 {
position: absolute;
margin: 0 auto;
left: -88px;
right: 0;}
.demo-2 {
position: absolute;
margin: auto 0;
top: -30px;
bottom: 0;} body:before {
content: '';
width: 100%;
border-bottom: 2px dotted blue;
position: absolute;
top: 50%;
-webkit-transform: translateY(-2px);} body:after {
content: '';
height: 100%;
border-left: 2px dotted blue;
position: absolute;
left: 50%;
-webkit-transform: translateX(2px);}</style></head><body><div class="demo demo-1">水平 距离偏移</div><div class="demo demo-2">垂直 距离偏移</div></body></html>

行 掌握后接下来可以为写通用动画作铺垫了(主要是飞入效果)
如果单纯渐显的话,咱们只需要控制opacity 就可以达到了:

<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,minimal-ui"><title></title><style>
* {
padding:0;
margin:0;}
html,body {
height:100%;}
.demo {
width:100px;
height: 100px;
text-align: center;
line-height: 100px;
background:lightblue;
opacity: 0;}
.demo-1 {
position: absolute;
margin:0 auto;
left:0;
right:0;
-webkit-animation:.8s ease opacity_kf 0s forwards;}
.demo-2 {
position: absolute;
margin:auto 0;
top:0;
bottom:0;
-webkit-animation:.8s ease opacity_kf .8s forwards;}
.demo-3 {
position: absolute;
margin:auto;
top:0;
bottom:0;
left:0;
right:0;
-webkit-animation:.8s ease opacity_kf 1.6s forwards;} @-webkit-keyframes opacity_kf {
0% {
opacity: 0;}
100% {
opacity: 1;}
}
</style></head><body><div class="demo demo-1">水平居中</div><div class="demo demo-2">垂直居中</div><div class="demo demo-3">中心居中</div></body></html>

扯了这么多 ,主角来了,就是飞入也分2种写法,一种就是每个飞入效果是一个width:100%;height:100%;的层,然后操作是整个层 translate,XY;但是这样写有一个很不方便就是审查元素的时候,放大镜只能定位到一个层上(多个飞入动画,别的层就给盖住了)

demo如下:

<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,minimal-ui"><title></title><style>
* {
padding:0;
margin:0;}
html,body {
height:100%;}
.demo {
width:100px;
height: 100px;
text-align: center;
line-height: 100px;
background:lightblue;}
.demo-1 {
position: absolute;
margin:0 auto;
left:0;
right:0;}
.demo-2 {
position: absolute;
margin:auto 0;
top:0;
bottom:0;}
.demo-3 {
position: absolute;
margin:auto;
top:0;
bottom:0;
left:0;
right:0;} @-webkit-keyframes translate_kf {
0% {
-webkit-transform:translateX(100%);}
100% {
-webkit-transform:translateX(0);}
} .demo-1-ani {
-webkit-animation:.8s ease translate_kf 0s forwards;}
.demo-2-ani {
-webkit-animation:.8s ease translate_kf .8s forwards;}
.demo-3-ani {
-webkit-animation:.8s ease translate_kf 1.6s forwards;} .translate-wrap {
width:100%;
height:100%;
-webkit-transform:translateX(100%);
position:absolute;}</style></head><body><div class="translate-wrap demo-1-ani"><div class="demo demo-1">水平居中</div></div><div class="translate-wrap demo-2-ani"><div class="demo demo-2">垂直居中</div></div><div class="translate-wrap demo-3-ani"><div class="demo demo-3">中心居中</div></div></body></html>

但是如果换一个写法,就是translate的包裹器里面的节点top和left都写了定值,那么外面动画的包裹器其实是不占文档流的
然后我们只需要操作这个不占文档流的容器就可以了

<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,minimal-ui"><title></title><style>
* {
padding:0;
margin:0;}
html,body {
height:100%;}
.demo {
width:100px;
height: 100px;
text-align: center;
line-height: 100px;
background:lightblue;}
.demo-1 {
position: absolute;
left:0;
right:0;}
.demo-2 {
position: absolute;
top:150px;}
.demo-3 {
position: absolute;
top:300px;
left:100px;} @-webkit-keyframes translate_kf {
0% {
-webkit-transform:translateX(100%);}
100% {
-webkit-transform:translateX(0);}
} .demo-1-ani {
-webkit-animation:.8s ease translate_kf 0s forwards;}
.demo-2-ani {
-webkit-animation:.8s ease translate_kf .8s forwards;}
.demo-3-ani {
-webkit-animation:.8s ease translate_kf 1.6s forwards;} .translate-wrap {
width:100%;
-webkit-transform:translateX(100%);
position:absolute;}</style></head><body><div class="translate-wrap demo-1-ani"><div class="demo demo-1">水平居中</div></div><div class="translate-wrap demo-2-ani"><div class="demo demo-2">垂直居中</div></div><div class="translate-wrap demo-3-ani"><div class="demo demo-3">中心居中</div></div></body></html>

最后提一点 如果同时用到渐显和飞入,只需要设置好几个间隔一致的class就能很简单的混合使用了,例如有10个控制渐显的动画,和5个控制飞入的动画

.word_1 {
opacity: 0;
-webkit-animation:.8s ease word_1_kf .5s forwards;
}
.word_2 {
opacity: 0;
-webkit-animation:.8s ease word_1_kf 1.3s forwards;
}
.word_3 {
opacity: 0;
-webkit-animation:.8s ease word_1_kf 2.1s forwards;
}
.word_4 {
opacity: 0;
-webkit-animation:.8s ease word_1_kf 2.9s forwards;
}
.word_5 {
opacity: 0;
-webkit-animation:.8s ease word_1_kf 3.7s forwards;
}
.word_6 {
opacity: 0;
-webkit-animation:.8s ease word_1_kf 4.5s forwards;
}
.word_7 {
opacity: 0;
-webkit-animation:.8s ease word_1_kf 5.3s forwards;
}
.word_8 {
opacity: 0;
-webkit-animation:.8s ease word_1_kf 6.1s forwards;
}
.word_9 {
opacity: 0;
-webkit-animation:.8s ease word_1_kf 6.9s forwards;
}
.word_10 {
opacity: 0;
-webkit-animation:.8s ease word_1_kf 7.7s forwards;
}
/********************** 左边动画 start ********************************/
.translateX_animation_left_1 {
-webkit-transform: translateX(-100%);
-webkit-animation: .8s ease translateX_1_left_kf forwards .5s;
}
.translateX_animation_left_2 {
-webkit-transform: translateX(-100%);
-webkit-animation: .8s ease translateX_1_left_kf forwards 1.3s;
}
.translateX_animation_left_3 {
-webkit-transform: translateX(-100%);
-webkit-animation: .8s ease translateX_1_left_kf forwards 2.1s;
}
@-webkit-keyframes translateX_1_left_kf {
0% {
-webkit-transform: translateX(-100%);
}
100% {
-webkit-transform: translateX(0);
}
}
/********************** 左边动画 end ********************************//********************** 右边动画 start ********************************/
.translateX_animation_right_1 {
-webkit-transform: translateX(-100%);
-webkit-animation: .8s ease translateX_1_right_kf .5 forwards;
}
.translateX_animation_right_2 {
-webkit-transform: translateX(100%);
-webkit-animation: .8s ease translateX_1_right_kf forwards 1.3s;
}
.translateX_animation_right_3 {
-webkit-transform: translateX(100%);
-webkit-animation: .8s ease translateX_1_right_kf forwards 2.1s;
}
.translateX_animation_right_4 {
-webkit-transform: translateX(100%);
-webkit-animation: .8s ease translateX_1_right_kf forwards 2.9s;
}
.translateX_animation_right_5 {
-webkit-transform: translateX(100%);
-webkit-animation: .8s ease translateX_1_right_kf forwards 3.7s;
}
@-webkit-keyframes translateX_1_right_kf {
0% {
-webkit-transform: translateX(100%);
}
100% {
-webkit-transform: translateX(0);
}
}
/********************** 右边动画 end ********************************/

word_1后可以随意执行 translateX_animation_right_2 或者 translateX_animation_left_2 ,然后word_3神马的。
skyweaver 手把手教你写css3通用动画的更多相关文章
- 手把手教你写css3通用动画
之前接了几个微信里的项目,类似电子邀请函,什么分析报告这样的项目, 对css3动画要求十分高,每个页面客户几乎都有天马行空的想法,或者说设计师有这样的想法.众所周知css3里的keyframe写好了就 ...
- 手把手教你写Sublime中的Snippet
手把手教你写Sublime中的Snippet Sublime Text号称最性感的编辑器, 并且越来越多人使用, 美观, 高效 关于如何使用Sublime text可以参考我的另一篇文章, 相信你会喜 ...
- 看过《大湿教我写.net通用权限框架(1)之菜单导航篇》之后发生的事(续)——主界面
引言 在UML系列学习中的小插曲:看过<大湿教我写.net通用权限框架(1)之菜单导航篇>之后发生的事 在上篇中只拿登录界面练练手,不把主界面抠出来,实在难受,严重的强迫症啊.之前一直在总 ...
- 手把手教你写LKM rookit! 之 第一个lkm程序及模块隐藏(一)
唉,一开始在纠结起个什么名字,感觉名字常常的很装逼,于是起了个这<手把手教你写LKM rookit> 我觉得: 你们觉得:...... 开始之前,我们先来理解一句话:一切的操作都是系统调用 ...
- 手把手教你写电商爬虫-第三课 实战尚妆网AJAX请求处理和内容提取
版权声明:本文为博主原创文章,未经博主允许不得转载. 系列教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 看完两篇,相信大家已经从开始的 ...
- 手把手教你写电商爬虫-第四课 淘宝网商品爬虫自动JS渲染
版权声明:本文为博主原创文章,未经博主允许不得转载. 系列教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 手把手教你写电商爬虫-第三课 ...
- 只有20行Javascript代码!手把手教你写一个页面模板引擎
http://www.toobug.net/article/how_to_design_front_end_template_engine.html http://barretlee.com/webs ...
- [原创]手把手教你写网络爬虫(4):Scrapy入门
手把手教你写网络爬虫(4) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 上期我们理性的分析了为什么要学习Scrapy,理由只有一个,那就是免费,一分钱都不用花! 咦?怎么有人扔西红柿 ...
- [原创]手把手教你写网络爬虫(5):PhantomJS实战
手把手教你写网络爬虫(5) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 大家好!从今天开始,我要与大家一起打造一个属于我们自己的分布式爬虫平台,同时也会对涉及到的技术进行详细介绍.大 ...
随机推荐
- python界面编程
这是一个简单的加法计算器 首先,要先对这个简易计算器进行布局设计,需要两个输入框,还有一个输入框用于存放就算的结果,还需要两个table,一个是"+"一个是"=" ...
- postman 请求接口 Could not get any response
前提: 今天用postman请求接口的时候,能请求到接口,但是打断点后发现方法里面要抛出异常就出现错误: 错误原因: 返回的 http 的 code不是3位的,如下我写成了四位 4002: <? ...
- python列表与字符串、元组的区别以及列表引用的方式
一.字符串 字符串也可以用下标取值.切片.for循环.len()取长度以及 in 和 not in 来进行操作. 但字符串是不可变的,不能被更改.只能构造一个“新的”字符串来存取你想要修改后的数据. ...
- c++练习—实现简单的4则运算
#pragma once class Counter { public: void setExp(const char* exp);//设置表达式 void cleanExp(); //清除表达式 v ...
- PAT B1022 D进制的A+B
课本AC代码 #include <cstdio> int main() { int a, b, d; scanf("%d%d%d", &a, &b, & ...
- Firebase Chat (firebase 实现web聊天室)
基于firebase + cloud Function 实现web聊天(demo版) 知识点: 使用Firebase SDK创建Google Cloud功能. 触发云功能基于Auth,云存储和Clou ...
- Python-基础-文件操作-随机存取
随机存取 文件操作中,read()和write()都属于顺序存取,只能按顺序从头到尾的进行读写.实际上我们可以只访问文件中我们感兴趣的部分.对此,我们可以使用seek()和tell()方法.两种方法的 ...
- python-day6(正式学习)
流程控制之while循环 语法 循环就是一个重复的过程,人需要重复做某项工作,那么计算机也一样.当ATM验证失败,那么计算机就会让我们再输入一次密码.这时候我们说的循环就是while循环,while循 ...
- Jmeter之TCP取样器
1.在线程组中添加“TCP取样器” 2.填写数据 以下截图是必须配置的 TCPClient classname: 填写TCP报文格式(有三类),默认前缀:org.apache.jmeter.prot ...
- linux 阿里云 新机器 安装jdk1.8
2019年7月17日15:58:34 按着顺序来: wget https://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4f ...