【CSS3系列】第七章 · 过渡和动画

news2024/9/20 6:02:17

写在前面


        Hello大家好, 我是【麟-小白】,一位软件工程专业的学生,喜好计算机知识。希望大家能够一起学习进步呀!本人是一名在读大学生,专业水平有限,如发现错误不足之处,请多多指正!谢谢大家!!!

        如果小哥哥小姐姐们对我的文章感兴趣,请不要吝啬你们的小手,多多点赞加关注呀!❤❤❤ 爱你们!!!


目录

写在前面

1. 过渡

1.1 过渡简介

1.2 transition-property

1.3 transition-duration

1.4 transition-delay

1.5 transition-timing-function

1.6 transition 复合属性

 2. 动画

2.1 什么是帧

2.2 什么是关键帧

2.3 动画的基本使用

2.4 动画的其他属性

2.5 动画复合属性

2.6 动画与过渡的区别

结语


【往期回顾】

【CSS3系列】第六章 · 2D和3D变换

【CSS3系列】第五章 · web 字体

【CSS3系列】第四章 · CSS3新增渐变

【CSS3系列】第三章 · CSS3新增边框和文本属性

【CSS3系列】第二章 · CSS3 新增盒模型和背景属性

【CSS3系列】第一章 · CSS3新增的三种基本属性


【其他系列】

【HTML5系列】

【HTML4系列】

【CSS2系列】

【Java基础系列】


1. 过渡


1.1 过渡简介

  • 过渡可以在不使用 Flash 动画,不使用 JavaScript 的情况下,让元素从一种样式,平滑过渡为另一种样式。

1.2 transition-property

  • 作用:定义哪个属性需要过渡,只有在该属性中定义的属性(比如宽、高、颜色等)才会有过渡效果。
  • 常用值:
    • none :不过渡任何属性。
    • all :过渡所有能过渡的属性。
    • 具体某个属性名 ,例如: width heigth ,若有多个以逗号分隔。
  • 不是所有的属性都能过渡,值为数字,或者值能转为数字的属性,都支持过渡,否则不支持过渡。
  • 常见的支持过渡的属性有:颜色、长度值、百分比、 z-index 、 opacity 、 2D 变换属性、 3D 变换属性、阴影。

1.3 transition-duration

  • 作用:设置过渡的持续时间,即:一个状态过渡到另外一个状态耗时多久。
  • 常用值:
    • 0 :没有任何过渡时间 —— 默认值。
    • s ms :秒或毫秒。
    • 列表
      • 如果想让所有属性都持续一个时间,那就写一个值。
      • 如果想让每个属性持续不同的时间那就写一个时间的列表。

代码演示:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>01_基本使用</title>
    <style>
        .box1 {
            width: 200px;
            height: 200px;
            background-color: orange;
            opacity: 0.5;
            /* 设置哪个属性需要过渡效果 */
            /* transition-property: width,height,background-color; */

            /* 让所有能过渡的属性,都过渡 */
            transition-property: all;

            /* 分别设置时间 */
            /* transition-duration: 1s,1s,1s; */
            /* 设置一个时间,所有人都用 */
            transition-duration: 1s;
        }
        .box1:hover {
            width: 400px;
            height: 400px;
            background-color: green;
            transform: rotate(45deg);
            box-shadow: 0px 0px 20px black;
            opacity: 1;
        }
    </style>
</head>
<body>
    <div class="box1"></div>
</body>
</html>

1.4 transition-delay

  • 作用:指定开始过渡的延迟时间,单位: s ms

1.5 transition-timing-function

  • 作用:设置过渡的类型
  • 常用值:
    • ease 平滑过渡 —— 默认值
    • linear 线性过渡
    • ease-in 慢 → 快
    • ease-out 快 → 慢
    • ease-in-out 慢 → 快 → 慢
    • step-start 等同于 steps(1, start)
    • step-end 等同于 steps(1, end)
    • steps( integer,?) 接受两个参数的步进函数。第一个参数必须为正整数,指定函数的步数。第二个参数取值可以是 start end ,指定每一步的值发生变化的时间点。第二个参数默认值为 end
    • cubic-bezie ( number, number, number, number)特定的贝塞尔曲线类型。
    • 在线制作贝赛尔曲线:https://cubic-bezier.com

代码演示:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>02_高级使用</title>
    <style>
        .outer {
            width: 1300px;
            height: 900px;
            border: 1px solid black;
        }
        .outer:hover .box {
            width: 1300px;
        }
        .box {
            width: 200px;
            height: 100px;
            /* 让所有能过渡的属性,都过渡 */
            transition-property: all;
            /* 设置一个时间,所有人都用 */
            transition-duration: 5s;
            /* 过渡延迟 */
            /* transition-delay: 2s; */
        }
        .box1 {
            background-color: skyblue;
            transition-timing-function: ease;
        }
        .box2 {
            background-color: orange;
            transition-timing-function: linear;
        }
        .box3 {
            background-color: gray;
            transition-timing-function: ease-in;
        }
        .box4 {
            background-color: tomato;
            transition-timing-function: ease-out;
        }
        .box5 {
            background-color: green;
            transition-timing-function: ease-in-out;
        }
        .box6 {
            background-color: purple;
            transition-timing-function: step-start;
        }
        .box7 {
            background-color: deepskyblue;
            transition-timing-function: step-end;
        }
        .box8 {
            background-color: chocolate;
            transition-timing-function: steps(20,end);
        }
        .box9 {
            background-color: rgb(18, 78, 34);
            transition-timing-function: cubic-bezier(1,.35,.78,1.24);
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="box box1">ease(慢,快,慢)</div>
        <div class="box box2">linear(匀速)</div>
        <div class="box box3">ease-in(慢,快)</div>
        <div class="box box4">ease-out(快,慢)</div>
        <div class="box box5">ease-in-out(慢,快,慢)</div>
        <div class="box box6">step-start不考虑过渡的时间,直接就是终点</div>
        <div class="box box7">step-end考虑过渡时间,但无过渡效果,过渡时间到了以后,瞬间到达终点</div>
        <div class="box box8">steps分步过渡</div>
        <div class="box box9">无敌的贝赛尔曲线</div>
    </div>
</body>
</html>

1.6 transition 复合属性

  • 如果设置了一个时间,表示 duration ;如果设置了两个时间,第一是 duration ,第二个是delay ;其他值没有顺序要求。
transition:1s 1s linear all;
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>03_过渡复合属性</title>
    <style>
        .outer {
            width: 1000px;
            height: 100px;
            border: 1px solid black;
        }
        .inner {
            width: 100px;
            height: 100px;
            background-color: orange;
            transition:linear all 3s 0.5s ;
        }
        .outer:hover .inner {
            width: 1000px;
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
    </div>
</body>
</html>

 2. 动画


2.1 什么是帧

  • 一段动画,就是一段时间内连续播放 n 个画面。每一张画面,我们管它叫做。一定时间内连续快速播放若干个帧,就成了人眼中所看到的动画。同样时间内,播放的帧数越多,画面看起来越流畅。

2.2 什么是关键帧

  • 关键帧指的是,在构成一段动画的若干帧中,起到决定性作用的 2-3 帧。


2.3 动画的基本使用

  • 第一步:定义关键帧(定义动画)
1. 简单方式定义:
/*写法一*/
@keyframes 动画名 {
    from {
        /*property1:value1*/
        /*property2:value2*/
    }
    to {
        /*property1:value1*/
    }
}
2. 完整方式定义:
@keyframes 动画名 {
    0% {
        /*property1:value1*/
    }
    20% {
        /*property1:value1*/
    }
    40% {
        /*property1:value1*/
    }
    60% {
        /*property1:value1*/
    }
    80% {
        /*property1:value1*/
    }
    100% {
        /*property1:value1*/
    }
}
  • 第二步:给元素应用动画,用到的属性如下:
    • animation-name :给元素指定具体的动画(具体的关键帧)
    • animation-duration :设置动画所需时间
    • animation-delay :设置动画延迟
.box {
    /* 指定动画 */
    animation-name: testKey;
    /* 设置动画所需时间 */
    animation-duration: 5s;
    /* 设置动画延迟 */
    animation-delay: 0.5s;
}
代码演示:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>01_基本使用</title>
    <style>
        .outer {
            width: 1000px;
            height: 100px;
            border: 1px solid black;
        }
        /* 定义一个动画(定义一组关键帧)—— 第一种方式 */
        @keyframes wangyoudong {
            /* 第一帧 */
            from {
                
            }
            /* 最后一帧 */
            to {
                transform: translate(900px);
                background-color: red;
            }
        }
        /* 定义一个动画(定义一组关键帧)—— 第二种方式 */
        @keyframes wangyoudong2 {
            /* 第一帧 */
            0% {

            }
            /* 29% {
              background-color: red;  
            } */
            /* 48% {
                background-color: orange;
            } */
            /* 88% {
                background-color: yellow;
            } */
            /* 最后一帧 */
            100% {
                transform: translate(900px) rotate(360deg);
                background-color: purple;
                border-radius: 50%;
            }
        }
        .inner {
            width: 100px;
            height: 100px;
            background-color: deepskyblue;
            /* 应用动画到元素 */
            animation-name: wangyoudong2;
            /* 动画持续的时间 */
            animation-duration: 3s;
            /* 动画延迟时间 */
            animation-delay: 0.2s;
        }
        
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
    </div>
</body>
</html>

2.4 动画的其他属性

  • animation-timing-function ,设置动画的类型,常用值如下:
    • ease 平滑动画 —— 默认值
    • linear 线性过渡
    • ease-in 慢 → 快
    • ease-out 快 → 慢
    • ease-in-out 慢 → 快 → 慢
    • step-start 等同于 steps(1, start)
    • step-end 等同于 steps(1, end)
    • steps( integer,?) 接受两个参数的步进函数。第一个参数必须为正整数,指定函数的步数。第二个参数取值可以是 start end ,指定每一步的值发生变化的时间点。第二个参数默认值为 end
    • cubic-bezie ( number, number, number, number)特定的贝塞尔曲线类型。
  • animation-iteration-count ,指定动画的播放次数,常用值如下:
    • number :动画循环次数
    • infinite 无限循环
  • animation-direction ,指定动画方向,常用值如下:
    • normal 正常方向 (默认)
    • reverse 反方向运行
    • alternate 动画先正常运行再反方向运行,并持续交替运行
    • alternate-reverse 动画先反运行再正方向运行,并持续交替运行
  • animation-fill-mode ,设置动画之外的状态
    • forwards 设置对象状态为动画结束时的状态
    • backwards 设置对象状态为动画开始时的状态
  • animation-play-state ,设置动画的播放状态,常用值如下:
    • running 运动 (默认)
    • paused 暂停

代码演示:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>02_其他属性</title>
    <style>
        .outer {
            width: 1000px;
            height: 100px;
            border: 1px solid black;
        }
        @keyframes atguigu {
            from {
                
            }
            to {
                transform: translate(900px) rotate(360deg);
                background-color: purple;
                border-radius: 50%;
            }
        }
        .inner {
            width: 100px;
            height: 100px;
            background-color: deepskyblue;
            /* 应用动画到元素 */
            animation-name: atguigu;
            /* 动画持续的时间 */
            animation-duration: 3s;
            /* 动画延迟时间 */
            animation-delay: 0.2s;

            /* ********其他属性--start*********** */
            /* 设置动画的方式 */
            animation-timing-function: linear;

            /* 动画播放的次数 */
            animation-iteration-count: infinite;

            /* 动画的方向 */
            animation-direction: alternate;

            /* 动画以外的状态(不发生动画的时候在哪里) */
            /* animation-fill-mode: forwards; */
        }
        .outer:hover .inner {
            /* 动画的播放状态 */
            animation-play-state: paused;
        }
        
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
    </div>
</body>
</html>

2.5 动画复合属性

  • 只设置一个时间表示 duration ,设置两个时间分别是:duration delay ,其他属性没有数量和顺序要求。
.inner {
    animation: atguigu 3s 0.5s linear 2 alternate-reverse forwards;
}
  • 备注: animation-play-state 一般单独使用。

代码演示:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>03_动画的复合属性</title>
    <style>
        .outer {
            width: 1000px;
            height: 100px;
            border: 1px solid black;
        }
        @keyframes atguigu {
            from {
                
            }
            to {
                transform: translate(900px) rotate(360deg);
                background-color: purple;
                border-radius: 50%;
            }
        }
        .inner {
            width: 100px;
            height: 100px;
            background-color: deepskyblue;
            animation: forwards 3s 0.5s alternate-reverse linear 2 atguigu;
        }
        .outer:hover .inner {
            /* 动画的播放状态 */
            animation-play-state: paused;
        }
        
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
    </div>
</body>
</html>

2.6 动画与过渡的区别

代码演示:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>04_动画与过渡的区别</title>
    <style>
        .outer {
            width: 1000px;
            height: 200px;
            border: 1px solid black;
        }
        .inner {
            width: 100px;
            height: 100px;
        }
        /* 用过渡,实现inner1跑到右边去 */
        .inner1 {
            background-color: orange;
            transition: 3s linear;
        }
        .outer:hover .inner1 {
            transform: translate(900px);
        }
        /* 用动画,实现inner2跑到右边去 */
        @keyframes atguigu {
            0%{}
            50%{
                background-color: red;
                border-radius: 50%;
                box-shadow: 0px 0px 20px black;
            }
            100%{
                transform: translate(900px);
            }
        }
        .inner2 {
            background-color: green;
            animation: atguigu 3s linear forwards;
        }

    </style>
</head>
<body>
    <div class="outer">
        <div class="inner inner1">过渡</div>
        <div class="inner inner2">动画</div>
    </div>
</body>
</html>

结语


本人会持续更新文章的哦!希望大家一键三连,你们的鼓励就是作者不断更新的动力

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/676223.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

可视化计算机科学论文库DBLP,高效整理文献,生成领域趋势图、词云图

Github项目链接&#xff1a;https://github.com/yaunsine/getDBLP Http克隆方式&#xff1a;git clone https://github.com/yaunsine/getDBLP.git SSH克隆方式&#xff1a;git clone gitgithub.com:yaunsine/getDBLP.git 作者&#xff1a;yaunsine dblp网站相信大家都不陌生&am…

模拟电路系列文章-ADC驱动电路

文章目录 概要整体架构流程技术名词解释技术细节小结 概要 提示&#xff1a;这里可以添加技术概要 模数转换器(Analog to Digital Convertor&#xff0c;ADC)将模拟量转变成数字量&#xff0c;是电学测量、控制领域—个极为重要的部件。 一个模拟电压信号&#xff0c;在进入A…

统信UOS系统开发笔记(六):提取在线安装软件后,提取其安装包,部署目标机使用离线软件包方式安装软件

若该文为原创文章&#xff0c;转载请注明原文出处 本文章博客地址&#xff1a;https://hpzwl.blog.csdn.net/article/details/131348876 红胖子(红模仿)的博文大全&#xff1a;开发技术集合&#xff08;包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软…

数据库底层物理存储层 的管理方案

​专栏内容&#xff1a; postgresql内核源码分析 手写数据库toadb 并发编程 个人主页&#xff1a;我的主页 座右铭&#xff1a;天行健&#xff0c;君子以自强不息&#xff1b;地势坤&#xff0c;君子以厚德载物. 数据库的物理存储层 简介 数据库的数据&#xff0c;在物理介质上…

【二叉树part04】| 110.平衡二叉树、257.二叉树的所以路径、404.左叶子之和

目录 ✿LeetCode110.平衡二叉树❀ ✿LeetCode257.二叉树的所有路径❀ ✿LeetCode404.左叶子之和❀ ✿LeetCode110.平衡二叉树❀ 链接&#xff1a;110.平衡二叉树 给定一个二叉树&#xff0c;判断它是否是高度平衡的二叉树。 本题中&#xff0c;一棵高度平衡二叉树定义为&…

Redis的数据类型及对应的数据结构(一)

Redis的数据类型和数据结构的对应关系 左边是 Redis 3.0版本的&#xff0c;也就是《Redis 设计与实现》这本书讲解的版本&#xff0c;右边是7.0,注意区别很大 数据类型包括&#xff1a;String、List、Hash、Set、ZSet 对应的底层数据结构入上图 String的应用场景 缓存对象 …

17.API Promise化 miniprogram-api-promise

目录 1 安装 2 配置 3 使用 1 安装 安装之后会在项目路径下出现这个目录&#xff0c;这个目录中又我们之前安装过的npm包 我们删除掉 miniprogram_npm 这个目录 然后点击构建 npm 构建之后就自动变成这样 2 配置 进入 app.js&#xff0c;然后写下面这些东西 3 使用

C++——auto关键字

目录 1. auto简介 2. auto的使用细则 3. auto的实际应用价值 1. auto简介 在早期C/C中auto的含义是&#xff1a;使用auto修饰的变量&#xff0c;是具有自动存储器的局部变量&#xff0c;但遗憾的是一直没有人去使用它&#xff0c;大家可思考下为什么&#xff1f; C11中&…

安装单机版openGauss

安装单机版openGauss 环境白名单查看ip地址问题解决python版本之间的问题yum指向2.7的编译 安装启动数据库 环境 下载python3.6.8 首先linux中的版本是默认的2.7.5.要下载3.6.8&#xff0c;需要用到wget&#xff0c; openGauss数据库基本操作&#xff08;超详细&#xff09; …

MATLAB 之 低层绘图操作和光照及材质处理

这里写目录标题 一、低层绘图操作1. 曲线对象2. 曲面对象3. 文本对象4. 其他核心对象4.1 区域块对象4.2 方框对象 二、光照和材质处理1. 光照处理2. 材质处理2.1 图形对象的反射特性2.2 material 函数 一、低层绘图操作 MATLAB 将曲线、曲面、文本等图形均视为对象&#xff0c…

五、一些经典的网络架构

一、Alexnet 2012年卷积神经网络的开篇鼻祖 但放到现在确实有很多的弊端和有待改进的地方 1&#xff0c;网络基本架构 8层网络&#xff0c;其中有5层卷积&#xff0c;3层全连接 网络层数的定义&#xff1a;主要看有没有参数运算的参与&#xff0c;例如卷积层和全连接就可以…

Xubuntu22.04之安装少儿编程scratch3.3(一百八十二)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 人生格言&#xff1a; 人生…

SPEC CPU 2006 gcc version 8.3.0 (Uos 8.3.0.3-3+rebuild) x86_64 源码编译tools 错误处理笔记

编译tools 拷贝tools到安装目录 cp /mnt/iso/tools /opt/speccpu2006/ -r 执行编译 su rootcd /opt/speccpu2006/tools/src sh -x buildtools 错误 undefined reference to __alloca 编辑./make-3.82/glob/glob.c&#xff0c;注释掉以下宏判断 you should not run config…

「网络编程」第二讲:socket套接字(四 - 完结)_ Linux任务管理与守护进程 | TCP协议通讯流程

「前言」文章是关于网络编程的socket套接字方面的&#xff0c;上一篇是网络编程socket套接字&#xff08;三&#xff09;&#xff0c;这篇续上篇文章的内容&#xff0c;下面开始讲解&#xff01; 「归属专栏」网络编程 「笔者」枫叶先生(fy) 「座右铭」前行路上修真我 「枫叶先…

linux之用户和用户组

在此之前我们需要先了解用户和用户组的区别 用户是我们可以登录的账号&#xff0c;而用户组是用户的小组&#xff0c;组也可以分为主组和附属组&#xff0c;主组是用户的主要组&#xff0c;附属而是用户的附加组 目录 1.添加新用户账号 2.用户口令的管理 3.用户组命令 1.添加…

被测系统架构与数据流分析

开源项目litemall系统架构(https://github.com/linlinjava/litemall) 角色与数据用户产品前端技术栈后端技术栈数据存储 开源项目Mall的系统架构(https://github.com/macrozheng/mall) 角色与数据用户产品前端技术栈后端技术栈服务治理技术栈监控技术栈大数据处理技术栈数据存…

校园视频AI分析预警系统 TesnorFlow

校园视频AI分析预警系统通过分布式TensorFlow模型训练&#xff0c;校园视频AI分析预警系统对学生的行为进行实时监测&#xff0c;当系统检测到学生出现打架、翻墙、倒地、抽烟等异常行为时&#xff0c;校园视频AI分析预警系统将自动发出警报提示相关人员及时采取措施。深度学习…

Elasticsearch 和数据架构:改进分析和存储的 4 个基本工具

作者&#xff1a;Emily McAlister 组织越来越依赖数据来做出有效的、基于证据的决策来推动业务成果。 无论是评估市场状况和改善客户体验、确保应用程序正常运行时间还是保护组织安全&#xff0c;来自多个来源&#xff08;包括消费者和内部系统&#xff09;的数据对于日常运营都…

开源URL短链接服务Shlink

最近一直有人在问&#xff0c;docker 查询注册表失败的问题&#xff0c;目前老苏验证可用的方法有 2 个&#xff1a; 方法一&#xff08;可用&#xff09;&#xff1a; 看过老苏以前文章的读者都知道&#xff0c;在遇到发布到 ghcr.io 的镜像时&#xff0c;老苏都会推荐用 do…

MacOS Ventura 13.4.1 (22F82) 带 OC 引导双分区黑苹果镜像

苹果今日向 Mac 电脑用户推送了 macOS 13.4.1 更新&#xff08;内部版本号&#xff1a;22F82&#xff09;&#xff0c;根据Apple的发布说明&#xff0c;该更新提供了重要的安全修复&#xff0c;并建议所有用户进行更新。Apple还为无法运行Ventura的用户发布了macOS 11.7.8和mac…