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) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 大家好!从今天开始,我要与大家一起打造一个属于我们自己的分布式爬虫平台,同时也会对涉及到的技术进行详细介绍.大 ...
随机推荐
- 【机器学习】【计算机视觉】非常全面的图像数据集《Actions》
目录(?)[+] 1.搜狗实验室数据集: http://www.sogou.com/labs/dl/p.html 互联网图片库来自sogou图片搜索所索引的部分数据.其中收集了包括人物.动物.建筑 ...
- elasticsearch 常用查询 + 删除索引 + 集群状态诊断
1.多条件查询 curl -X POST \ http://10.0.0.42:9200/addressbook_user/_search \ -H 'cache-control: no-cache' ...
- flask钩子函数
@app.context_processor def context_processor(): return {"current_user":"zhiliao" ...
- ioremap&buddy system
生死契阔,与子成说,执子之手,与子携老 一.ioremap 1.参考: 理解 (1)http://www.linuxidc.com/Linux/2011-04/34295.htm 代码过程 (1)ht ...
- Design Log Storage System
You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...
- 七牛云图床及MPIC工具使用
考虑到图片更容易被人接受,但是大量图片又会延迟博客加载速度.因此,个人感觉可以把静态文件资源托管在云端,这样加载的话就不至于太慢. 注册七牛云 实名验证通过 创建文件存储 内容管理-上传图片 下载Mp ...
- 黑科技——树剖两次dfs转一次dfs!
黑科技--树剖两次\(dfs\)转一次\(dfs\)! 重所周知,树链剖分通常是要\(dfs\)两次的,就像这样: int Fa[N],dep[N],Sz[N],son[N]; void dfs1( ...
- Minimum Cut(2015沈阳online)【贪心】
Minimum Cut[贪心]2015沈阳online 题意:割最少的边使得图不连通,并且割掉的边中有且仅有一条是生成树的边. 首先,我们选择一条树中的边进行切割,此时仅考虑树上的边集,有两种情况:1 ...
- python中的with语句
https://www.ibm.com/developerworks/cn/opensource/os-cn-pythonwith/index.html
- 向量运算(lua,三维) 点乘、叉乘、模、夹角
向量运算在游戏制作中经常用到,稍微总结一下. 一.点乘 如图,假设 向量a与b的点乘表示a在b上的投影与b的模的乘积 公式: 代码: function MathHelper.GetVector3D ...