HTML5的动画是通过伪类@实现的,设置动画的起始位置,结束位置,动画时间即可,中间的过度HTML会自动补齐,简单的代码就可以实现炫酷动画效果。
实例:
<!DOCTYPE html>
<html>
<head>
<!-- 移动端适配 全屏 支持屏幕放大缩小的倍率 -->
<meta name="full-screen" content="yes">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>动画</title>
<style type="text/css">
.animate,.animated {
-webkit-animation-duration: .5s;/*动画时长*/
animation-duration: .5s;
-webkit-animation-fill-mode: both;/*动画模式*/
animation-fill-mode: both;
}
/*元素初始位置为屏幕下方300像素*/
.bottom{background-color:#333;width:100%;z-index:100;color:#515151;height:300px;position: absolute;bottom: -300px;}
/*opacity 透明度(渐显,渐隐) translate 偏移量*/
/*上滑 直接实现 元素位置位于屏幕下方300像素*/
@-webkit-keyframes fadeInUp {
0% {
opacity: 0;
-webkit-transform: translateY(0);/*起点位置即为元素位置*/
transform: translateY(0);
}
100% {
opacity: 1;
-webkit-transform: translateY(-300px);/* 终点位置为起始位置上移300像素*/
transform: translateY(-300px);
}
}
@keyframes fadeInUp {
0% {
opacity: 0;
-webkit-transform: translateY(0);
transform: translateY(0);
}
100% {
opacity: 1;
-webkit-transform: translateY(-300px);
transform: translateY(-300px);
}
}
.fadeInUp {
-webkit-animation-name: fadeInUp;
animation-name: fadeInUp;
}
/*元素初始位置为屏幕下方*/
.bottom1{background-color:#333;width:100%;z-index:100;color:#515151;height:300px;position: absolute;display:none;bottom: 0px;}
/*上滑1 先将元素下移至屏幕下方300像素在上移 */
@-webkit-keyframes fadeInUp1 {
0% {
opacity: 0;
-webkit-transform: translateY(300px);/*起点位置元素初始位置下移300像素*/
transform: translateY(300px);
}
100% {
opacity: 1;
-webkit-transform: translateY(0);/* 向上移300像素*/
transform: translateY(0);
}
}
@keyframes fadeInUp1 {
0% {
opacity: 0;
-webkit-transform: translateY(300px);
transform: translateY(300px);
}
100% {
opacity: 1;
-webkit-transform: translateY(0);
transform: translateY(0);
}
}
.fadeInUp1 {
-webkit-animation-name: fadeInUp1;
animation-name: fadeInUp1;
}
/* 侧滑 显示*/
@keyframes fadeInRight {
0% {
-webkit-transform: translateX(0);
transform: translateX(0);
}
100% {
-webkit-transform: translateX(120px);/* 左移120像素*/
transform: translateX(120px);
}
}
@-webkit-keyframes fadeInRight {
0% {
-webkit-transform: translateX(0);
transform: translateX(0);
}
100% {
-webkit-transform: translateX(120px);
transform: translateX(120px);
}
}
.fadeInRight {
-webkit-animation-name: fadeInRight;
animation-name: fadeInRight;
}
/* 侧滑 隐藏*/
@keyframes fadeOutLeft {
0% {
-webkit-transform: translateX(120px);
transform: translateX(120px);
}
100% {
-webkit-transform: translateX(0);
transform: translateX(0);
}
}
@-webkit-keyframes fadeOutLeft {
0% {
-webkit-transform: translateX(120px);
transform: translateX(120px);
}
100% {
-webkit-transform: translateX(0);
transform: translateX(0);
}
}
.fadeOutLeft {
-webkit-animation-name: fadeOutLeft;
animation-name: fadeOutLeft;
}
*{margin: 0;padding: 0;list-style: none;}
html{overflow:hidden;}
body{background-color: #f4f4f4;font-size: 18px;color:white;font-family: Microsoft YaHei;overflow: hidden;height: 100%;}
#nav{width: 120px;color: white;background-color: #333;height: 100%;position: fixed;top: 0;left: -120px;}
.header{box-shadow: 0px 1px 2px #999;top:0;z-index:100;position:fixed;width:100%;background-color:white;/* background: -webkit-linear-gradient(bottom,#9b2413,#c94323);background: linear-gradient(bottom,#9b2413,#c94323); */height: 50px;text-align: center;color: #555;}
.header b{display:inline-block;padding:3px;/* background: -webkit-linear-gradient(bottom,#9a2413,#cd3419); */font-size:38px;line-height:42px;text-align:center;color: #555;height: 38px;width: 30%;margin-left:20px;margin-top:2px;border-radius:5px;box-shadow: inset 0 1px 2px rgba(0,0,0,0.2),0px 1px 0 rgba(100,100,100,0.2);-webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,0.2),0px 1px 0 rgba(100,100,100,0.2);}
.header b.on{background: rgba(0,0,0,0.3);color: white;}
.header span{text-align: center;line-height: 50px;font-size: 16px;}
</style>
</head>
<body>
<div class="nav animate" id="nav">
<ul>
<li>菜单1</li>
<li>菜单2</li>
</ul>
</div>
<div class="body animate">
<div class="header">
<b class="menu" id="menu"><span>侧滑</span></b>
<b class="menu" id="top"><span>上滑</span></b>
<b class="menu" id="top1"><span>上滑1</span></b>
</div>
</div>
<div class="bottom animated">上滑</div>
<div class="bottom1 animated">上滑1</div>
</body>
<script src="http://www.zeptojs.cn/zepto.min.js"></script>
<script type="text/javascript">
$(function(){
//屏幕高度自适应
$("body").height($(window).height());
//侧滑
$("#menu").click(function(){
if(!$(this).hasClass("on")){
$(this).addClass("on");
$(".animate").removeClass("fadeOutLeft").addClass("fadeInRight");
}
else{
$(this).removeClass("on");
$(".animate").removeClass("fadeInRight").addClass("fadeOutLeft");
}
});
//上滑
$("#top").click(function(){
if(!$(this).hasClass("on")){
$(this).addClass("on");
$(".bottom").addClass("fadeInUp").show();
}
else{
$(this).removeClass("on");
$(".bottom").removeClass("fadeInUp").hide();
}
});
//上滑1
$("#top1").click(function(){
if(!$(this).hasClass("on")){
$(this).addClass("on");
$(".bottom1").addClass("fadeInUp1").show();
}
else{
$(this).removeClass("on");
$(".bottom1").removeClass("fadeInUp1").hide();
}
});
})
</script>
</html>