当前位置: 首页 > ops >正文

CSS3——transition过渡效果

通常当 CSS 的属性值更改后,浏览器会立即更新相应的样式,例如当鼠标悬停在元素上时,通过 :hover 选择器定义的样式会立即应用在元素上。在 CSS3 中加入了一项过渡功能,通过该功能您可以将元素从一种样式在指定时间内平滑的过渡到另一种样式,类似于简单的动画,但无需借助 flash 或 JavaScript。

CSS 中提供了 5 个有关过渡的属性,如下所示:

  • transition-property:设置元素中参与过渡的属性;

  • transition-duration:设置元素过渡的持续时间;

  • transition-timing-function:设置元素过渡的动画类型;

  • transition-delay:设置过渡效果延迟的时间,默认为 0;

  • transition:简写属性,用于同时设置上面的四个过渡属性。

要成功实现过渡效果,必须定义以下两项内容:

  • 元素中参数与过渡效果的属性;

  • 过渡效果持续的时间。

提示:过渡效果通常会在鼠标悬停在元素上时发生,如果未设置过渡持续的时间,则过渡效果不会生效,因为过渡时间的默认值为 0。

1、transition-property

transition-property 属性用来设置元素中参与过渡的属性名称,语法格式如下:

transition-property: none | all | property;

参数说明如下:

  • none:表示没有属性参与过渡效果;

  • all:表示所有属性都参与过渡效果;

  • property:定义应用过渡效果的 CSS 属性名称列表,多个属性名称之间使用逗号,进行分隔。

示例代码如下:

<!DOCTYPE html>
<html>
<head><style>div {width: 100px;height: 100px;border: 3px solid black;margin: 10px 0px 0px 10px;transition-property: width, background;}div:hover {width: 200px;background-color: blue;}</style>
</head>
<body><div></div>
</body>
</html>

2. transition-duration

transition-duration 属性用来设置过渡需要花费的时间(单位为秒或者毫秒),语法格式如下:

transition-duration: time;

其中,time 为完成过渡效果需要花费的时间(单位为秒或毫秒),默认值为 0,意味着不会有效果。

如果有多个参与过渡的属性,也可以依次为这些属性设置过渡需要的时间,多个属性之间使用逗号进行分隔,例如transition-duration: 1s, 2s, 3s;。除此之外,也可以使用一个时间来为所有参与过渡的属性设置过渡所需的时间。示例代码如下:

<!DOCTYPE html>
<html>
<head><style>div {width: 100px;height: 100px;border: 3px solid black;margin: 10px 0px 0px 10px;transition-property: width, background;transition-duration: .25s, 1s;}div:hover {width: 200px;background-color: blue;}</style>
</head>
<body><div></div>
</body>
</html>

3. transition-timing-function

transition-timing-function 属性用来设置过渡动画的类型,属性的可选值如下:

描述

linear

以始终相同的速度完成整个过渡过程,等同于 cubic-bezier(0,0,1,1)

ease

以慢速开始,然后变快,然后慢速结束的顺序来完成过渡过程,等同于 cubic-bezier(0.25,0.1,0.25,1)

ease-in

以慢速开始过渡的过程,等同于 cubic-bezier(0.42,0,1,1)

ease-out

以慢速结束过渡的过程,等同于 cubic-bezier(0,0,0.58,1)

ease-in-out

以慢速开始,并以慢速结束的过渡效果,等同于 cubic-bezier(0.42,0,0.58,1)

cubic-bezier(n, n, n, n)

使用 cubic-bezier() 函数来定义自己的值,每个参数的取值范围在 0 到 1 之间

示例代码如下:

<!DOCTYPE html>
<html>
<head><style>div {width: 100px;height: 100px;border: 3px solid black;margin: 10px 0px 0px 10px;transition-property: width, background;transition-duration: .25s, 1s;transition-timing-function: ease;}div:hover {width: 200px;background-color: blue;}</style>
</head>
<body><div></div>
</body>
</html>

4. transition-delay

transition-delay 属性用来设置过渡效果何时开始,属性的语法格式如下:

transition-delay: time;

其中,参数 time 用来设置在过渡效果开始之前需要等待的时间,单位为秒或毫秒。

示例代码如下:

<!DOCTYPE html>
<html>
<head><style>div {width: 100px;height: 100px;border: 3px solid black;margin: 10px 0px 0px 10px;transition-property: width, background;transition-duration: .25s, 1s;transition-delay: 3s;}div:hover {width: 200px;background-color: blue;}</style>
</head>
<body><div></div>
</body>
</html>

5. transition

transition 属性是上面四个属性的简写形式,通过该属性可以同时设置上面的四个属性,属性的语法格式如下:

transition: transition-property transition-duration transition-timing-function transition-delay;

transition 属性中,transition-property 和 transition-duration 为必填参数,transition-timing-function 和 transition-delay 为选填参数,如非必要可以省略不写。另外,通过 transition 属性也可以设置多组过渡效果,每组之间使用逗号进行分隔,示例代码如下:

<!DOCTYPE html>
<html>
<head><style>div {width: 100px;height: 100px;border: 3px solid black;margin: 10px 0px 0px 10px;transition: width .25s linear 1.9s, background 1s 2s, transform 2s;}div:hover {width: 200px;background-color: blue;transform: rotate(180deg);}</style>
</head>
<body><div></div>
</body>
</html>

上面的代码也可以写成如下所示的样子:

<!DOCTYPE html>
<html>
<head><style>div {width: 100px;height: 100px;border: 3px solid black;margin: 10px 0px 0px 10px;transition-property: width, background, transform;transition-duration: .25s, 1s, 2s;transition-timing-function: linear, ease, ease;transition-delay: 1.9s, 2s, 0s;}div:hover {width: 200px;background-color: blue;transform: rotate(180deg);}</style>
</head>
<body><div></div>
</body>
</html>
http://www.xdnf.cn/news/11719.html

相关文章:

  • endnote国内杂志文献格式_【科研工具42】Endnote 软件使用教程
  • 网络爬虫是什么
  • SD卡分区及取消分区
  • MicroPython之TPYBoard v102开发板控制OLED显示中文
  • 对象名'***'无效?
  • VS2008操作技巧(不断更新)
  • crc循环校验原理和实现
  • android2.2刷机,中兴 V880 Android 2.2.2 ROM 刷机包 省电 流畅
  • 《刺客信条:启示录》低配置画面设置优化
  • 网吧无盘服务器进u盘启动,利用U盘启动在网吧免费上网
  • Altium_Protel99SE的使用
  • 【手把手教你Ubuntu】Ubuntu 13.04 Server版本安装图解教程
  • springboot基于微信小程序的钓鱼交友平台设计与实现-计算机毕业设计源码33506
  • 文章出轨被爆是愚人节整蛊 网民你知道吗?
  • 黑莓7100刷机及修改PIN,完美破解超越输入法
  • 新浪微博开放平台开发步骤简介(适合新手看)
  • MFC--界面库
  • 电商网站建设中的网站内容管理策略
  • 机器人抓取(三)—— Azure Kinect SDK 及 ROS 驱动安装
  • JAVA下载的详细教程
  • 分享70个ASP江湖论坛源码,总有一款适合您
  • ASP六大对象介绍
  • 微信5.0 Android版飞机大战破解无敌模式手记
  • 个人空间岁末大回报活动12月26日获奖名单
  • JS监听组合按键
  • 星空极速 加密算法(附本人C语言源代码)
  • 有关ARP
  • Filezilla使用
  • elasticsearch最大节点数_10分钟快速入门海量数据搜索引擎Elasticsearch
  • WINDOWSXP主题风格美化教程