热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CSS3的@keyframes用法详解:

原文出处:http:www.cnblogs.comzuihongyanp5902541.htmlCSS3的@keyframes用法详解:此属性与animation属性是密切相关的,关于an

原文出处: http://www.cnblogs.com/zuihongyan/p/5902541.html

CSS3的@keyframes用法详解:
此属性与animation属性是密切相关的,关于animation属性可以参阅CSS3的animation属性用法详解一章节。

一.基本知识:
keyframes翻译成中文,是"关键帧"的意思,如果用过flash应该对这个比较好理解,当然不会flash也没有任何问题。
使用transition属性也能够实现过渡动画效果,但是略显粗糙,因为不能够更为精细的控制动画过程,比如只能够在指定的时间段内总体控制某一属性的过渡,而animation属性则可以利用@keyframes将指定时间段内的动画划分的更为精细一些。

语法结构:

@keyframes animationname {keyframes-selector {css-styles;}}

参数解析:
1.animationname:声明动画的名称。
2.keyframes-selector:用来划分动画的时长,可以使用百分比形式,也可以使用 "from" 和 "to"的形式。
"from" 和 "to"的形式等价于 0% 和 100%。
建议始终使用百分比形式。

二.代码实例:

实例一:

DOCTYPE html>   
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落title>
<style type="text/css">
div
{
width
:100px;
height
:100px;
background
:red;
position
:relative;

animation
:theanimation 5s infinite alternate;
-webkit-animation
:theanimation 5s infinite alternate ;
-moz-animation
:theanimation 5s infinite alternate ;
-o-animation
:theanimation 5s infinite alternate ;
-ms-animation
:theanimation 5s infinite alternate ;
}
@keyframes theanimation
{
from {left
:0px;}
to
{left:200px;}
}
@-webkit-keyframes theanimation
{
from {left
:0px;}
to
{left:200px;}
}
@-moz-keyframes theanimation
{
from {left
:0px;}
to
{left:200px;}
}
@-o-keyframes theanimation
{
from {left
:0px;}
to
{left:200px;}
}
@-ms-keyframes theanimation
{
from {left
:0px;}
to
{left:200px;}
}
style>
head>
<body>
<div>div>
body>
html>

上面代码实现了简单的动画,下面简单做一下分析:
1.使用@keyframes定义了一个名为theanimation的动画。
2.@keyframes声明的动画名称要和animation配合使用。
3.from to等价于0%-100%,所以就是规定5s内做了一件事情。

实例二:

DOCTYPE html>   
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落title>
<style type="text/css">
div
{
width
:100px;
height
:100px;
background
:red;
position
:relative;

animation
:theanimation 4s infinite alternate;
-webkit-animation
:theanimation 4s infinite alternate ;
-moz-animation
:theanimation 4s infinite alternate ;
-o-animation
:theanimation 4s infinite alternate ;
-ms-animation
:theanimation 4s infinite alternate ;
}
@keyframes theanimation
{
0%{top
:0px;left:0px;background:red;}
25%
{top:0px;left:100px;background:blue;}
50%
{top:100px;left:100px;background:yellow;}
75%
{top:100px;left:0px;background:green;}
100%
{top:0px;left:0px;background:red;}
}
@-moz-keyframes theanimation
{
0% {top
:0px;left:0px;background:red;}
25%
{top:0px;left:100px;background:blue;}
50%
{top:100px;left:100px;background:yellow;}
75%
{top:100px;left:0px;background:green;}
100%
{top:0px;left:0px;background:red;}
}
@-webkit-keyframes theanimation
{
0%{top
:0px;left:0px;background:red;}
25%
{top:0px;left:100px;background:blue;}
50%
{top:100px;left:100px;background:yellow;}
75%
{top:100px;left:0px;background:green;}
100%
{top:0px;left:0px;background:red;}
}
@-o-keyframes theanimation
{
0%{top
:0px;left:0px;background:red;}
25%
{top:0px;left:100px;background:blue;}
50%
{top:100px;left:100px;background:yellow;}
75%
{top:100px;left:0px;background:green;}
100%
{top:0px;left:0px;background:red;}
}
style>
head>
<body>
<div>div>
body>
html>

在以上代码中,使用百分比形式将动画时长进行了划分,规定了在指定区间内做指定的事情。



推荐阅读
  • 1、给边框加上圆角及阴影,如下代码:<!DOCTYPEhtmlPUBLIC"-W3CDTDHTML4.01TransitionalEN"" ... [详细]
  • 所有设备的CSS3媒体查询原文:https://www. ... [详细]
  • 这篇文章将为大家详细讲解有关如何使用JavaScript动态设置CSS3属性值,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 移动端常用单位——rem的使用方法和注意事项
    本文介绍了移动端常用的单位rem的使用方法和注意事项,包括px、%、em、vw、vh等其他常用单位的比较。同时还介绍了如何通过JS获取视口宽度并动态调整rem的值,以适应不同设备的屏幕大小。此外,还提到了rem目前在移动端的主流地位。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • 如何在HTML中获取鼠标的当前位置
    本文介绍了在HTML中获取鼠标当前位置的三种方法,分别是相对于屏幕的位置、相对于窗口的位置以及考虑了页面滚动因素的位置。通过这些方法可以准确获取鼠标的坐标信息。 ... [详细]
  • CSS3中如何使用background-size定义背景图片大小
    CSS3中如何使用background-size定义背景图片大小?针对这个问题,今天小编总结这篇有关background-size属性的文章,可供感兴趣的小伙伴们参考借鉴 ... [详细]
  • ext将html代码转为字符串,在iOS中将HTML转换为NSAttributedString
    在iOS7中,UIKit添加了一个initWithData:options:documentAttributes:error ... [详细]
  • 一篇文章搞定css3 3d效果
    css33d学习心得卡片反转魔方banner图首先我们要学习好css33d一定要有一定的立体感通过这个图片应该清楚的了解到了x轴y轴z轴是什么概念了。首先先给大家看一个小 ... [详细]
  • background:-webkit-gradient(linear,10%10%,100%100%, ... [详细]
  • 前端实用的CSS3技巧有哪些
    本文小编为大家详细介绍“前端实用的CSS3技巧有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“前端实用的CSS3技巧有哪些”文章能帮助大家 ... [详细]
  • 1.CSS3透明度渐变(从左到右)#grad{background:-webkit-linear-gradient(left,rgba(198,226,255,1),rgba(19 ... [详细]
author-avatar
贾章鱼_977
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有