CSS基础:浅聊动画

2024-06-18 21:18

本文主要是介绍CSS基础:浅聊动画,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

动画是CSS中最令人兴奋的颠覆性特征之一,可以通过设置多个节点来精确控制一个或者一组动画,常用来实现复杂的动画效果,相对于过渡,动画可以实现更多的变化,更多的控制,连续自动播放等效果。

需要两个步骤:

  • 先定义动画:也就是动画的内容以及给内容其一个名字

    @keyframes 动画名称{/*动画开始*/0%{行为}………………/*动画结束*/100%{行为}}
    /*使用百分比来规定变化的时间,也可以使用 from to   等同于 0% 100%*/
    
  • 调用动画:就是元素调用动画内容

选择器{animation-name:动画名称;/*动画执行花费时间*/animation-duration:持续时间;}

先看一个例子:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>测试文档</title><style>@keyframes test_move {0%{transform: translate(0px,0px);}100%{transform: translate(500px,500px);}}div{width: 400px;height: 400px;border-radius: 200px;background-color: #4a90e2;animation-name: test_move;animation-duration: 1s;}</style>
</head>
<body>
<div></div></body>
</html>

在这里插入图片描述

当然步骤还可以增加多个。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>测试文档</title><style>@keyframes test_move {0%{transform: translate(0px,0px);}25%{transform: translate(0px,500px);}50%{transform: translate(500px,500px);}75%{transform: translate(0px,500px);}100%{transform: translate(0px,0px);}}div{width: 400px;height: 400px;border-radius: 200px;background-color: #4a90e2;animation-name: test_move;animation-duration: 1s;}</style>
</head>
<body>
<div></div></body>
</html>

在这里插入图片描述

注意:

  • 里面的百分比是时间百分比。

动画属性

在看网页的时候,会发现有些网页的是某些效果是循环一直动的,当然有些不是视频或者flash而是通过动画的属性进行设置,现在看一下动画的一些属性

属性描述
@keyframes规定动画以及动画名称,这个很好理解,看例子就明白
animation-name元素调用动画名称的属性,(必有)
animation-duration完成动画所需要的时间,单位是秒或者毫秒,默认是0.(必有)
animation-timing-function规定动画运动的速度曲线,默认是ease。前面2D转换中也有类似,可以参考理解。
animation-delay规定动画延迟多久才开始执行,默认是0.
animation-iteration-count动画动画播放的次数默认是1,还可以为infinite(无限循环)
animation-direction规定动画在下一个周期是否逆向播放,默认是normal,当然也可以逆向:alternate
animation-play-state规定动画是否运行或者暂停,默认是runing,还可以设置pause
animation-fill-mode规定动画结束的状态,保持运动后位置为forwards回到起始backwards。

animation-name和animation-duration在演示的时候以及呈现了,现在演示一些剩下的属性。

animation-timing-function

这个属性是运动的速度,比如匀速和加速等。默认是ease。

看图:

在这里插入图片描述

演示:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>测试文档</title><style>@keyframes test_move {0%{transform: translate(0px,0px);}100%{transform: translate(500px,0px);}}div{width: 40px;height: 40px;border-radius: 20px;background-color: #4a90e2;animation-name: test_move;animation-duration: 4s;animation-timing-function: ease-in-out;}</style>
</head>
<body>
<div></div></body>
</html>

在这里插入图片描述

补充 运动轨迹step():

这个运行轨迹为什么要说,

@keyframes 动画名称{/*动画开始*/0%{行为}………………/*动画结束*/100%{行为}}
/*如果走十步,完成100% 可以写10% 20% 等,但是如下写更方便*/
animation-timing-function:step(10);

animation-delay

这个就是一个多久后再执行。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>测试文档</title><style>@keyframes test_move {0%{transform: translate(0px,0px);}100%{transform: translate(500px,0px);}}div{width: 40px;height: 40px;border-radius: 20px;background-color: #4a90e2;animation-name: test_move;animation-duration: 4s;animation-delay: 2s;}</style>
</head>
<body>
<div></div></body>
</html>

在这里插入图片描述

可以看出是延迟2秒后执行。

animation-fill-mode

可以看出所有的运动完毕后,都闪回到起点了,所有动画提供了这个属性。

在这里插入图片描述

演示:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>测试文档</title><style>@keyframes test_move {0%{transform: translate(0px,0px);}100%{transform: translate(500px,0px);}}div{width: 40px;height: 40px;border-radius: 20px;background-color: #4a90e2;animation-name: test_move;animation-duration: 4s;animation-fill-mode: forwards;}</style>
</head>
<body>
<div></div></body>
</html>

在这里插入图片描述

animation-iteration-count

在这里插入图片描述

演示:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>测试文档</title><style>@keyframes test_move {0%{transform: translate(0px,0px);}100%{transform: translate(500px,0px);}}div{width: 40px;height: 40px;border-radius: 20px;background-color: #4a90e2;animation-name: test_move;animation-duration: 4s;animation-iteration-count:infinite;}</style>
</head>
<body>
<div></div></body>
</html>

在这里插入图片描述

虽然可以可以无限次的运动,但是有没有发现一件事,就是动画结束后,又闪回起始点,如果需要来回移动的话就需要了解下面这个属性。

animation-direction

在这里插入图片描述

演示:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>测试文档</title><style>@keyframes test_move {0%{transform: translate(0px,0px);}100%{transform: translate(500px,0px);}}div{width: 40px;height: 40px;border-radius: 20px;background-color: #4a90e2;animation-name: test_move;animation-duration: 4s;animation-iteration-count: 2;/*定义的是下一次运行后才会返回所以就设置运行次数为2*/animation-direction:alternate;}</style>
</head>
<body>
<div></div></body>
</html>

在这里插入图片描述

animation-play-state

在这里插入图片描述

这个属性一般的时候伪元素一起用,比如当运动的元素用鼠标放上去的时候就停止,来先看演示:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>测试文档</title><style>@keyframes test_move {0%{transform: translate(0px,0px);}100%{transform: translate(500px,0px);}}div{width: 40px;height: 40px;border-radius: 20px;background-color: #4a90e2;animation-name: test_move;animation-duration: 4s;animation-iteration-count: infinite;/*定义的是下一次运行后才会返回所以就设置运行次数为2*/animation-direction:alternate;}div:hover{animation-play-state: paused;}</style>
</head>
<body>
<div></div></body>
</html>

在这里插入图片描述

复合写法

上面的属性自然可以复合写法。

格式一般如下:

animation:动画名称 持续时间 运动曲线  何时开始  播放次数  是否反方向 动画的状态

来一个例子,假设就是一个游戏而自己刷的怪的地点,加一个标志。

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>测试文档</title><style>.map{margin: 10px auto;width: 1600px;height: 800px;background : url("jpg/ditu.jpg") no-repeat center center;}.target{position: relative;top: 500px;left: 500px;}/*9a6e3a*/.spot{width: 10px;height:10px;background-color: #df5000;border-radius: 10px;}div [class^="shaw"]{position: absolute;top: 0px;left: 0px;width: 10px;height:10px;border-radius: 10px;box-shadow: 0px 0px 11px #df5000;animation: 1s biao infinite;}@keyframes biao {0%{transform: scale(1,1);}100%{transform: scale(3,3);}}div .shaw2{animation-delay: 0.5s;}div .shaw3{animation-delay: 1s;}</style>
</head>
<body>
<div class="map"><div class="target"><div class="spot"></div><div class="shaw1"></div><div class="shaw2"></div><div class="shaw3"></div></div></div></body>
</html>

在这里插入图片描述

这篇关于CSS基础:浅聊动画的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073076

相关文章

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

Vim使用基础篇

本文内容大部分来自 vimtutor,自带的教程的总结。在终端输入vimtutor 即可进入教程。 先总结一下,然后再分别介绍正常模式,插入模式,和可视模式三种模式下的命令。 目录 看完以后的汇总 1.正常模式(Normal模式) 1.移动光标 2.删除 3.【:】输入符 4.撤销 5.替换 6.重复命令【. ; ,】 7.复制粘贴 8.缩进 2.插入模式 INSERT

零基础STM32单片机编程入门(一)初识STM32单片机

文章目录 一.概要二.单片机型号命名规则三.STM32F103系统架构四.STM32F103C8T6单片机启动流程五.STM32F103C8T6单片机主要外设资源六.编程过程中芯片数据手册的作用1.单片机外设资源情况2.STM32单片机内部框图3.STM32单片机管脚图4.STM32单片机每个管脚可配功能5.单片机功耗数据6.FALSH编程时间,擦写次数7.I/O高低电平电压表格8.外设接口

vue, 左右布局宽,可拖动改变

1:建立一个draggableMixin.js  混入的方式使用 2:代码如下draggableMixin.js  export default {data() {return {leftWidth: 330,isDragging: false,startX: 0,startWidth: 0,};},methods: {startDragging(e) {this.isDragging = tr

ps基础入门

1.基础      1.1新建文件      1.2创建指定形状      1.4移动工具          1.41移动画布中的任意元素          1.42移动画布          1.43修改画布大小          1.44修改图像大小      1.5框选工具      1.6矩形工具      1.7图层          1.71图层颜色修改          1

vue项目集成CanvasEditor实现Word在线编辑器

CanvasEditor实现Word在线编辑器 官网文档:https://hufe.club/canvas-editor-docs/guide/schema.html 源码地址:https://github.com/Hufe921/canvas-editor 前提声明: 由于CanvasEditor目前不支持vue、react 等框架开箱即用版,所以需要我们去Git下载源码,拿到其中两个主

React+TS前台项目实战(十七)-- 全局常用组件Dropdown封装

文章目录 前言Dropdown组件1. 功能分析2. 代码+详细注释3. 使用方式4. 效果展示 总结 前言 今天这篇主要讲全局Dropdown组件封装,可根据UI设计师要求自定义修改。 Dropdown组件 1. 功能分析 (1)通过position属性,可以控制下拉选项的位置 (2)通过传入width属性, 可以自定义下拉选项的宽度 (3)通过传入classN

js+css二级导航

效果 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con

基于Springboot + vue 的抗疫物质管理系统的设计与实现

目录 📚 前言 📑摘要 📑系统流程 📚 系统架构设计 📚 数据库设计 📚 系统功能的具体实现    💬 系统登录注册 系统登录 登录界面   用户添加  💬 抗疫列表展示模块     区域信息管理 添加物资详情 抗疫物资列表展示 抗疫物资申请 抗疫物资审核 ✒️ 源码实现 💖 源码获取 😁 联系方式 📚 前言 📑博客主页:

vue+el国际化-东抄西鉴组合拳

vue-i18n 国际化参考 https://blog.csdn.net/zuorishu/article/details/81708585 说得比较详细。 另外做点补充,比如这里cn下的可以以项目模块加公共模块来细分。 import zhLocale from 'element-ui/lib/locale/lang/zh-CN' //引入element语言包const cn = {mess