CSS3动画属性 animation详解(看完就会)

简介: CSS3动画属性 animation详解(看完就会)

keyframes

用 keyframes定义动画,写上动画要执行的内容, (类似类选择器)

@keyframes a {
            0% {
                width: 300px;
            }
            100% {
                width: 600px;
            }
        }

image.gif

这就是定义了一个动画变量名,从0%到100%也就是从开始到结束,也可以用from,to来表示,

@keyframes a {
            from {
                width: 300px;
            }
            to {
                width: 600px;
            }
        }

image.gif

里面写属性变化的值,不止可以分两个段也可以进行多段分割

@keyframes a {
            0% {
                width: 300px;
            }
            50% {
                width: 800px;
            }
            100% {
                width: 300px;
            }
        }

image.gif

,用百分数进行分割。

动画相关的值

@keyframes 规定动画

animation-name 规定 @keyframes 动画的名称(必须的)

animation-duration 规定动画完成一个周期所花费的秒或毫秒,默认是 0(必须的)

animation-timingfunction  规定动画的速度曲线,默认是“ease”

animation-delay 规定动画何时开始,默认是 0

animation-iteration-count 规定动画被播放的次数,默认是 1,还有 infinite

animation-direction 规定动画是否在下一周期逆向播放,默认是 "normal", alternate 逆播放

animation-play-state 规定动画是否正在运行或暂停。默认是 "running", 还有 "paused"

animation 所有动画属性的简写属性,除了animation-play-state 属性

animation-name animation-duration

<style>
        @keyframes a {
            0% {
                width: 300px;
            }
            50% {
                width: 800px;
            }
            100% {
                width: 300px;
            }
        }
        div {
            animation-name: a;
            animation-duration: 2s;
        }
    </style>
    <script>
    </script>
</head>
<body>
    <div style='background-Color:red;width: 300px;height:300px ;'></div>
</body>

image.gif

image.gif编辑

刷新之后就自动触发动画,执行的动画变量名是a,两秒内完成动画,也就是从0%到100%两秒内完成。

animation-timing-function animation-delay

linear

动画从头到尾的速度是相同的。

ease

默认。动画以低速开始,然后加快,在结束前变慢。

ease-in

动画以低速开始。

ease-out

动画以低速结束。

ease-in-out

动画以低速开始和结束。

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

在 cubic-bezier 函数中自己的值。可能的值是从 0 到 1 的数值

@keyframes a {
            0% {
                width: 300px;
            }
            50% {
                width: 800px;
            }
            100% {
                width: 300px;
            }
        }
        div {
            animation-name: a;
            animation-duration: 5s;
            animation-timing-function: ease-in-out;
            animation-delay: 2s;
        }

image.gif

image.gif编辑

animation-timing-function 动画开始和结束慢速

animation-delay 动画延迟两秒开始。

animation-iteration-count   animation-play-state

@keyframes a {
            0% {
                width: 300px;
            }
            50% {
                width: 800px;
            }
            100% {
                width: 300px;
            }
        }
        div {
            animation-name: a;
            animation-duration: 5s;
            animation-timing-function: ease-in-out;
            animation-delay: 2s;
            animation-iteration-count: infinite;
        }
        div:hover {
            animation-play-state: paused;
        }

image.gif

image.gif编辑

animation-iteration-count  循环次数,infinite重复循环

animation-play-state paused 停止动画  当鼠标移入动画停止

animation-direction  animation-fill-mode

animation-direction

normal 默认值。动画按正常播放。
reverse 动画反向播放。
alternate 动画在奇数次(1、3、5...)正向播放,在偶数次(2、4、6...)反向播放。
alternate-reverse 动画在奇数次(1、3、5...)反向播放,在偶数次(2、4、6...)正向播放。
initial 设置该属性为它的默认值。请参阅 initial
inherit 从父元素继承该属性。请参阅 inherit
@keyframes a {
            0% {
                width: 300px;
            }
            50% {
                width: 500px;
            }
            100% {
                width: 800px;
            }
        }
        div {
            animation-name: a;
            animation-duration: 5s;
            animation-timing-function: ease-in-out;
            animation-delay: 2s;
            animation-iteration-count: infinite;
            /* animation-fill-mode: forwards; */
            animation-direction: alternate;
        }
        div:hover {
            animation-play-state: paused;
        }

image.gif

image.gif编辑

animation-iteration-count:infinite  重复循环  animation-direction:alternate 在偶数次反向播放

就形成了  长度 一直来回变化的效果

animation-fill-mode  

保持 最终状态 forwards 原位置 backwards

image.gif

image.gif编辑

 animation-fill-mode: forwards  宽度到达800后 停止动画 保持在动画执行完毕的状态 不会恢复

animation

直接写出所有的相关动画属性(简写),除了animation-play-state 属性。

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

 

@keyframes a {
            0% {
                width: 300px;
            }
            50% {
                width: 500px;
            }
            100% {
                width: 800px;
            }
        }
        div {
            animation: a 5s ease-in-out 2s infinite alternate;
        }
        div:hover {
            animation-play-state: paused;
        }

image.gif

image.gif编辑

 

相关文章
|
7天前
|
前端开发
了解 css中 backface-visibility 属性
了解 css中 backface-visibility 属性
15 0
|
9天前
|
前端开发
CSS常用滤镜属性讲解
本文介绍了CSS滤镜的几种常用效果,包括高斯模糊、亮度和对比度调整、灰度化、图像反转、透明度调整、饱和度调整及复古滤镜等。例如,使用`blur(10px)`可使图像产生模糊效果,包裹层设置`overflow: hidden`可避免边缘模糊;`brightness(150%)`和`contrast(150%)`分别增强图像亮度和对比度;`grayscale(1)`将图像转为灰度;`invert(1)`实现图像颜色完全反转;`opacity(0.5)`调整图像半透明;`saturate(350%)`增加饱和度;`sepia(100%)`营造复古深褐色调;`hue-rotate(180deg)`改变
18 4
CSS常用滤镜属性讲解
|
9天前
|
前端开发
了解 css中 backface-visibility 属性
`backface-visibility` 是一个CSS属性,用于3D转换时控制元素背面的可见性,以优化渲染性能。默认情况下,背面被隐藏以减少不必要的渲染。此属性有两值:`visible` 表示背面可见;`hidden`(默认值)则隐藏背面。 通过双面卡片案例演示了其用法:两个重叠盒子,一个显示图片,另一个显示文字且初始状态为背面朝外。鼠标悬停时,图片盒子翻转显示背面,文字盒子翻转显示正面,同时设置 `backface-visibility: hidden` 以确保背面不被渲染。 兼容性良好,广泛支持现代浏览器。
20 2
了解 css中 backface-visibility 属性
|
5天前
|
移动开发 JavaScript 前端开发
基于CSS3、原生JS、Vue3.0技术各自实现序列帧动画效果
这篇文章展示了如何使用纯CSS3、原生JavaScript以及Vue 3.0技术来实现序列帧动画效果,并通过代码示例和动画效果展示了每种方法的实现过程和最终效果。
12 0
|
6天前
|
前端开发 UED 开发者
使用 CSS 的 line-height 属性来提高可读性
使用 CSS 的 line-height 属性来提高可读性
6 0
|
8天前
|
前端开发
CSS常用滤镜属性讲解
CSS常用滤镜属性讲解
17 0
|
1月前
|
前端开发
学习css的clip-path属性
【7月更文挑战第1天】了解CSS `clip-path`属性,用于定义元素显示区域的裁剪形状,如圆形、椭圆、多边形、矩形及SVG路径。通过配合过渡和动画,可创建动态交互效果。例如,`clip-path: circle(radius at center);`用于创建圆形裁剪,`polygon`用于自定义多边形。还可以使用`path()`引用SVG路径数据。[资源推荐:bennettfeely.com/clippy/](https://bennettfeely.com/clippy/),提供交互式工具测试不同形状。
39 0
学习css的clip-path属性
|
20天前
|
开发框架 前端开发 JavaScript
循序渐进BootstrapVue,开发公司门户网站(2)--- 使用wow.js动画组件以及自定义的CSS样式处理动态效果
循序渐进BootstrapVue,开发公司门户网站(2)--- 使用wow.js动画组件以及自定义的CSS样式处理动态效果
|
1月前
|
前端开发
css动画(仿微信聊天页面)
css动画(仿微信聊天页面)
|
1月前
|
前端开发 JavaScript C++
CSS 【详解】样式选择器(含ID、类、标签、通配、属性、伪类、伪元素、Content属性、子代、后代、兄弟、相邻兄弟、交集、并集等选择器)
CSS 【详解】样式选择器(含ID、类、标签、通配、属性、伪类、伪元素、Content属性、子代、后代、兄弟、相邻兄弟、交集、并集等选择器)
26 0