css3中transition属性详解
css3中通过transition属性可以实现一些简单的动画过渡效果~
1、语法
transition: property duration timing-function delay;
transition 属性设置元素当过渡效果,是一个复合属性,包括四个简写属性:
- transition-property:指定CSS属性的name,transition效果(默认值:all)
- transition-duration(必须):过渡效果持续时间(不指定默认为0,不会有任何效果)
- transition-timing-function:指定过渡效果的转速曲线(默认值:ease)
- transition-delay:指定过渡延迟开始时间(默认为0)
注意:IE9及以下不支持该属性,safari3.1-6、IOS3.2-6.1、android2.1-4.3需要添加-webkit-前缀;而其余高版本浏览器支持标准写法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
.tra{
width: 50px;
height: 50px;
background-color: lightcoral;
/* 复合属性 */
transition: all 2s ease 0s;
/* 采用以下分属性也是可以的 */
transition-duration: 2s;
transition-property: all;
transition-timing-function: ease;
transition-delay: 0s;
}
.tra:hover{
width: 200px;
}
</style>
</head>
<body>
<div class="tra"></div>
</body>
</html>
运行效果:

注意:在使用 transition 复合属性时,各个属性用空格隔开,不能使用 ,
2、分属性
(1)transition-property
transition-property属性可以指定需要过渡的css属性,默认值为all表示所有属性都过渡(不写该属性值也表示all),如果为none则没有任何属性存在过渡效果
.tra{
width: 50px;
height: 50px;
background-color: lightcoral;
/* 指定 width 属性过渡 */
transition: width 2s ease 0s;
}
.tra:hover{
width: 200px;
height: 100px;
}
<div class="tra"></div>

只指定 width 属性过渡, height 属性未指定
注意:并不是所有css属性都可以过渡,只有具备中间值的属性才有过渡效果,比如 display:block 不能过渡为 display:none
(2)transition-duration
transition-duration属性可以用来设置一个属性过渡所需要的时间,也就是该属性从旧值到新值过渡所需时间(持续时间),默认值为0s,不指定就没有过渡效果
.tra{
width: 50px;
height: 50px;
display: block;
background-color: lightcoral;
/* 此处transition-property省略,默认为all */
/* 指定过渡时间为2s */
transition: 2s ease 0s;
}
.tra:hover{
width: 100px;
height: 100px;
}

注意:
- 不能为负值
- 必须带上单位,单独一个数字无效
- 该值为单值时,即所有过渡属性都对应同样时间;该值为多值时,过渡属性按照顺序对应持续时间
.tra{
width: 50px;
height: 50px;
display: block;
background-color: lightcoral;
transition-property: width,background;
transition-timing-function: ease;
transition-duration: 5s, 2s;
/* 以上分属性等价于下面复合属性写法 */
transition: width 5s ease 0, background 2s ease 0;
}
.tra:hover{
width: 100px;
background-color: blue;
}

当该值为多值时,过渡属性按照顺序对应持续时间
(3)transition-timing-function
transition-timing-function属性指的是过渡的“缓动函数”,用来指定属性过渡时动画运动形式,值可以是关键字、贝塞尔曲线(bezier),默认值为ease
关键字:linear| ease| ease-in| ease-out| ease-in-out|
贝塞尔:cubic-bezier(n,n,n,n);

.tra{
width: 50px;
height: 50px;
display: block;
background-color: lightcoral;
/* transition-timing-function默认值为ease */
transition: 1s linear| ease| ease-in| ease-out| ease-in-out|;
}
.tra:hover{
border-radius: 50%;
background-color: blue;
}
ease :开始和结束慢,中间快

linear :匀速

ease-in :开始慢,越来越快

ease-out :结束慢,越来越慢

ease-in-out :先加速后减速,与ease类似,但比ease幅度大

cubic-bezier(n,n,n,n) 贝塞尔曲线中4个值随意调整,就会得到不同的效果
.tra{
width: 50px;
height: 50px;
display: block;
background-color: lightcoral;
transition: 1s cubic-bezier(.75,2.03,0,.14);
}
.tra:hover{
border-radius: 50%;
background-color: blue;
}

(4)transition-delay
transition-delay属性指定过渡开始前的延迟时间
.tra{
width: 50px;
height: 50px;
display: block;
background-color: lightcoral;
/* 2s过后过渡才开始执行 */
transition: 1s cubic-bezier(.75,2.03,0,.14) 2s;
}
.tra:hover{
border-radius: 50%;
background-color: blue;
}

3、结束语
最后再说明补充两点内容:
1、触发方式:transition属性并非只有hover才能触发,其他比如【点击事件】【获得/失去焦点】【长按】等操作都可以触发transition属性过渡
2、事件回调:transition属性只有一个事件,那就是transitionend事件,它在过渡事件完成后触发
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#test{
height: 60px;
width: 60px;
background-color: lightpink;
transition: width 1.5s;
font-size: 12px;
}
#test:hover{
width: 250px;
}
</style>
</head>
<body>
<div id="test"></div>
</body>
<script>
//监听 transitionend 事件
test.addEventListener("transitionend", myFunction);
// 回调函数
function myFunction(e){
e = e || event;
test.innerHTML = "过渡结束,执行事件回调内容"
}
</script>
</html>

css3中transition属性详解的更多相关文章
- CSS3中border-image属性详解
border-images可以说也是CSS3中的重量级属性,如同圆角.边框颜色属性border-color.块阴影属性一样,也是属于边框属性中的一员. 从其字面意思上看,我们可以理解为“边框-图片”, ...
- DIV css中cursor属性详解-鼠标移到图片变换鼠标形状 (转)
css中cursor属性详解-鼠标移到图片变换鼠标形状 语法: cursor : auto | all-scroll | col-resize| crosshair | default | han ...
- Javascript中prototype属性详解 (存)
Javascript中prototype属性详解 在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不 ...
- CSS中background属性详解
CSS背景属性 background css 说明 background-image:url(图片的网址); 背景图 background: url( 图片的网址 ); 背景 background-c ...
- CSS3中的Transition属性详解
w3c标准中对CSS3的transition这是样描述的:“CSS的transition允许CSS的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发, ...
- CSS3中的Transition属性详解(贝赛尔曲线)
transition语法: transition : [<'transition-property'> || <'transition-duration'> || <'t ...
- 第98天:CSS3中transform变换详解
transform变换详解 本文主要介绍变形transform. Transform字面上就是变形,改变的意思.在CSS3中transform主要包括以下几种:旋转rotate.扭曲skew.缩放sc ...
- Android中shape属性详解
一.简单使用 刚开始,就先不讲一堆标签的意义及用法,先简单看看shape标签怎么用. 1.新建shape文件 首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.x ...
- spring sessionFactory 属性配置详解,applicationContext中各种属性详解
1.Bean的id为sessionFactory,对应的类为AnnotationSessionFactory,即采用注解的形式实现hibernate. 2.hibernateProperties,配置 ...
随机推荐
- [BUUCTF]PWN——level4
level4 附件 步骤: 例行检查,32位程序,开启了NX保护 运行一下程序,看看大概的情况 32位ida载入,首先检索程序里的字符串,根据上一步运行看到的字符串进行跳转 输入点在function里 ...
- docker初识-docker安装、基于docker安装mysql及tomcat、基本命令
一.docker是什么 用go语言开发,开源的应用容器引擎,容器性能开销极低 二.整体架构图 Docker 包括三个基本概念: 镜像(Image):Docker 镜像(Image),就相当于是一个 r ...
- LuoguP7715 「EZEC-10」Shape 题解
Content 有一个 \(n\times m\) 的网格,网格上的格子被涂成了白色或者黑色. 设两个点 \((x_1,y_1)\) 和 \((x_2,y_2)\),如果以下三个条件均满足: \(1\ ...
- java 常用类库:操作系统System类,运行时环境Runtime
System类: System 类代表Java程序的运行平台,程序不能创建System类的对象, System类提供了一些类变量和类方法,允许直接通过 System 类来调用这些类变量和类方法. Sy ...
- ftp:500 Illegal PORT command. 425
局域网用FTP命令访问外网的FTP SERVER,执行任何命令(如dir,ls,put,get)总是报下面的错误ftp:500 Illegal PORT command. 425ftp:Use POR ...
- SQL:查询Mysql表结构
背景:有时需要做数据字典,其中最重要的就是表结构.经整理,编写SQL如下: 代码: 1 -- drop TABLE `cfg_data_dict` ; 2 CREATE TABLE `cfg_data ...
- Linux C++获取磁盘剩余空间和可用空间
完整源码 #include <sys/statfs.h> #include <string> #include <iostream> #include <li ...
- 【LeetCode】1041. Robot Bounded In Circle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找规律 日期 题目地址:https://leetco ...
- 【LeetCode】704. Binary Search 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 线性查找 二分查找 日期 题目地址:https:// ...
- spoj-SUBSUMS - Subset Sums
SUBSUMS - Subset Sums Given a sequence of N (1 ≤ N ≤ 34) numbers S1, ..., SN (-20,000,000 ≤ Si ≤ 20, ...