之前接了几个微信里的项目,类似电子邀请函,什么分析报告这样的项目, 对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通用动画的更多相关文章

  1. 手把手教你写css3通用动画

    之前接了几个微信里的项目,类似电子邀请函,什么分析报告这样的项目, 对css3动画要求十分高,每个页面客户几乎都有天马行空的想法,或者说设计师有这样的想法.众所周知css3里的keyframe写好了就 ...

  2. 手把手教你写Sublime中的Snippet

    手把手教你写Sublime中的Snippet Sublime Text号称最性感的编辑器, 并且越来越多人使用, 美观, 高效 关于如何使用Sublime text可以参考我的另一篇文章, 相信你会喜 ...

  3. 看过《大湿教我写.net通用权限框架(1)之菜单导航篇》之后发生的事(续)——主界面

    引言 在UML系列学习中的小插曲:看过<大湿教我写.net通用权限框架(1)之菜单导航篇>之后发生的事 在上篇中只拿登录界面练练手,不把主界面抠出来,实在难受,严重的强迫症啊.之前一直在总 ...

  4. 手把手教你写LKM rookit! 之 第一个lkm程序及模块隐藏(一)

    唉,一开始在纠结起个什么名字,感觉名字常常的很装逼,于是起了个这<手把手教你写LKM rookit> 我觉得: 你们觉得:...... 开始之前,我们先来理解一句话:一切的操作都是系统调用 ...

  5. 手把手教你写电商爬虫-第三课 实战尚妆网AJAX请求处理和内容提取

    版权声明:本文为博主原创文章,未经博主允许不得转载. 系列教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 看完两篇,相信大家已经从开始的 ...

  6. 手把手教你写电商爬虫-第四课 淘宝网商品爬虫自动JS渲染

    版权声明:本文为博主原创文章,未经博主允许不得转载. 系列教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 手把手教你写电商爬虫-第三课 ...

  7. 只有20行Javascript代码!手把手教你写一个页面模板引擎

    http://www.toobug.net/article/how_to_design_front_end_template_engine.html http://barretlee.com/webs ...

  8. [原创]手把手教你写网络爬虫(4):Scrapy入门

    手把手教你写网络爬虫(4) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 上期我们理性的分析了为什么要学习Scrapy,理由只有一个,那就是免费,一分钱都不用花! 咦?怎么有人扔西红柿 ...

  9. [原创]手把手教你写网络爬虫(5):PhantomJS实战

    手把手教你写网络爬虫(5) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 大家好!从今天开始,我要与大家一起打造一个属于我们自己的分布式爬虫平台,同时也会对涉及到的技术进行详细介绍.大 ...

随机推荐

  1. mysql数据库查找数据的方法。

    今日内容 1.外键的变种 唯一索引:关键字 unique(num) 作用:使指定的列,中的属性不能重复,并且加速查找案例:create table t5(id int,num int,unique(n ...

  2. 【AMAD】django-activity-stream

    动机 简介 个人评分 动机 为你的django站点生成活动流(类似facebook feed).用户可以在个人feed页面看到订阅的人的活动流. 简介 django-activity-stream1中 ...

  3. JAVA -----------交互式程序

    2.6交互式程序 如果程序能在执行期间交互地从用户输入中读取数据,就可使程序每执行一次时计算出新结果,并且新结果取决于输入数据.这样的程序才具有实用性. 2.6.1 Scanner类 Scanner类 ...

  4. prometheus 监控 zookeeper

    1.zookeeper的规则 [root@do1cloud01 prometheus]# cat zookeeper.yml rules: - pattern: "org.apache.Zo ...

  5. 学习笔记:CentOS 7学习之十一:文件的重定向

    1.文件描述符定义 文件描述符:是内核为了高效管理已被而打开的文件所创建的缩影,用于指向被打开的文件,所有执行I/O操作的系统调用都通过文件描述符:文件描述符是一个简单的非负整数,用于标明每一个被进程 ...

  6. provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.

    通常情况下,要解决这个,你去SQL Server配置管理器(SSCM)和: [1]在SSCM中设置 [1.1]确保共享内存协议启用 [1.2]确保命名管道协议 [1.3]确保TCP / IP被启用,和 ...

  7. winform中如何使用确认对话框

    在系统中,常需要这样的功能,让用户确认一些信息:如下图: [退出系统]按钮关键代码如下: private void btnExit_Click(object sender, EventArgs e) ...

  8. 技能节-AI人脸识别

    我们收到技能节项目的通知是在两周之前,项目要求做个人脸评分系统. 两周时间写一个"人脸评分系统",好像时间比较紧了,还好我们完成了~这个项目是将摄像头捕获到的包含人脸的图像传输到百 ...

  9. Spring实战(十二) Spring中注入AspectJ切面

    1.Spring AOP与AspectJ Spring AOP与AspectJ相比,是一个功能比较弱的AOP解决方案. AspectJ提供了许多它不能支持的类型切点,如在创建对象时应用通知,构造器切点 ...

  10. 查询进程内存,cpu占用情况。僵尸进程

    查使用内存最多的5个进程:ps aux | head -1 && ps aux | grep -v USER | sort -nr -k 4 | head -5 查使用CPU最多的5个 ...