之前接了几个微信里的项目,类似电子邀请函,什么分析报告这样的项目, 对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:;
-webkit-animation:.8s ease word_1_kf .5s forwards;
}
.word_2 {
opacity:;
-webkit-animation:.8s ease word_1_kf 1.3s forwards;
}
.word_3 {
opacity:;
-webkit-animation:.8s ease word_1_kf 2.1s forwards;
}
.word_4 {
opacity:;
-webkit-animation:.8s ease word_1_kf 2.9s forwards;
}
.word_5 {
opacity:;
-webkit-animation:.8s ease word_1_kf 3.7s forwards;
}
.word_6 {
opacity:;
-webkit-animation:.8s ease word_1_kf 4.5s forwards;
}
.word_7 {
opacity:;
-webkit-animation:.8s ease word_1_kf 5.3s forwards;
}
.word_8 {
opacity:;
-webkit-animation:.8s ease word_1_kf 6.1s forwards;
}
.word_9 {
opacity:;
-webkit-animation:.8s ease word_1_kf 6.9s forwards;
}
.word_10 {
opacity:;
-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神马的。

好,暂时遇到的项目只用到了渐显和飞入,这2个常用的通用动画,小伙伴们有更多的实际场景欢迎交流,写得不好的地方也欢迎吐槽,谢谢看完  谢谢谢谢。

手把手教你写css3通用动画的更多相关文章

  1. skyweaver 手把手教你写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. STL - set【集合】

    参考:http://www.cplusplus.com/reference/set/set/ 一.set 是按特定顺序存储唯一元素的容器 实现是一种非常高效的平衡检索二叉树:红黑树(Red-Black ...

  2. 记一次msfconsole_android渗透实验

    1>查看本机IP 2>生成App木马 3>将生成的木马安装至手机 4>打开msfconsole 1,  use exploit/multi/handler  加载模块. 2, ...

  3. 2018.11.7 关于将Web项目部署到阿里云服务器-5个步骤搞定

    将Eclipse导出的War包部署到阿里云服务器上,提供给移动端实时的访问 1. 先登录阿里云网站注册账号,选择服务器类型(我用的是 云服务器ECS), 如果你还是在读大学生可享受优惠价,最低好像是9 ...

  4. 如何使用MiniProfiler(附最新版MiniProfiler使用心得)

    MiniProfiler这个工具早就久仰大名,不过之前一直没有动力去用,正好最近手上有个ASP.NET MVC的项目,正好拿来试试手,下面是使用最新的4.0.138版本的心得体会以及踩到一些小坑的解决 ...

  5. 根据ip确定城市

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  6. POJ 3616 Milking Time(加掩饰的LIS)

    传送门: http://poj.org/problem?id=3616 Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Sub ...

  7. python 通过 socket 发送文件

    目录结构: client: #!/usr/bin/env python # -*-coding:utf-8 -*- import socket, struct, json download_dir = ...

  8. SpringBoot非官方教程 | 第二十四篇: springboot整合docker

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot24-docker/ 本文出自方志朋的博客 这篇文 ...

  9. vue.js中的slot

    vue.js 中的 slot 一.slot 的作用 调用组件的时候,对于数据,我们会用props将数据从父组件传至子组件.但是,如果从父组件到子组件,单纯是页面局部渲染的改变,slot会更合适. 二. ...

  10. 2 3 5 7的倍数 (51Nod - 1284)[容斥定理]

    20180604 给出一个数N,求1至N中,有多少个数不是2 3 5 7的倍数. 例如N = 10,只有1不是2 3 5 7的倍数. Input 输入1个数N(1 <= N <= 10^1 ...