search
HomeWeb Front-endCSS TutorialDetailed explanation of transformation and animation in CSS3

Recently I was learning to make mobile pages and made a small demo of the WeChat page, which used a lot of CSS3new content, including the new deformations and animations of CSS3 . In fact, this CSS3 animation effect can also be achieved with JS, but CSS3 can turn on hardware acceleration and have better performance.

(The browser prefix is ​​not written below, please add it yourself if necessary~~)

The realization of common animation effects in CSS3 actually mainly relies on transition and animation. Usually, these two are used in conjunction with the new attribute transform in CSS3.

So what are these three things actually used for? I draw a simple conclusion based on my own understanding.

transform: Transform the element;

transition: When the attribute of the element changes, give it a transition animation effect;

animation: Do complex animation.

Let’s talk about it in detail below.

The first thing to talk about is the syntax of transform:

transform:none | transform-functions ;

none is its default value, and the following transform-functions refers to the transformation function;

There are two types of transformation functions: 2D and 3D; // Related syntax can be found at http://www.w3school.com.cn/cssref/pr_transform.asp

Among them, 2D includes: (the deformation is based on the center of the element, that is, 50% of the X axis and Y axis)

 translate() //Including translateX() and translateY(); Function : Move the element according to the XOY coordinates (the right side of the

#rotate() // Function: rotate the element;

skew() // Including skewX() and skewY(); Function: tilt the element.

And 3D has more Z-axis. The relevant syntax can be found at

W3C

. Here we recommend an article written by Zhang Xinxu (http://www.zhangxinxu.com/ w

ordpress/2012/09/css3-3d-transform-perspective-animate-transition/), the explanation of transformation is easy to understand. The syntax mentioned in the article can be used with the following simulator for better results~ (http://fangyexu.com/tool-CSS3Inspector.html) Let me summarize a few points: 1 . When performing 3D deformation, the deformed element must first be wrapped with two layers of labels, one as a stage and one as a container.

舞台
    容器
        元素
        元素
        ...
//My personal understanding is that if the elements on the stage are treated as a whole, you can add only one layer of labels, that is, treat the stage as a container

舞台(容器)
    元素
    元素
    ...
2. On the CSS of the stage, set perspective (sight distance), which means the assumed distance of people from the stage. Set transform-style: preserve-3d on the CSS of the container to enable 3D

view

so that the child elements of the container are rendered in 3D space. //If the stage is used as a container, perspective and transform-style are written together.

So, can the base point of deformation only be the center of the element? no. The transform-origin attribute can change the base point of the transformation.


The keywords of transform-origin are generally

top

/

bottom / left / right / center / top left / top right etc. Yes, everyone is familiar with it. Some of the individual keywords are actually abbreviations, such as top = top center = center top; Then there is the transition animation transition.


The trigger condition is

:hover

/

:focus / :active / :checked / Event in JS

语法: transition: 指定过渡的CSS属性 、 过渡所需时间 、 过渡的函数(动画的速度控制) 、 开始的延迟时间

指定过渡的CSS属性:all / 指定样式 / none (若省略不写,则为 all ,none一般用于清空动画)

过渡所需时间:单位s / ms (默认为0)

过渡的函数: ease(由快到慢 默认值) / linear(匀速) / ease-in(加速) / ease-out(减速) / ease-in-out(先加速后减速)/ cubic-bezier(三次贝塞尔曲线)

延迟时间:单位s / ms (默认为0)

(写transition的时候,先写 动画时间 , 再写延迟时间)


举个例子吧,博雅今年秋招的笔试题:

  用 CSS3 实现一个半径25px的圆 进行速度由快到慢的300px滚动。

p{ width:50px; height:50px; border-radius:25px; background:#000; transition:1s ease-out;}
p:active{ transform:translateX(300px) rotate(780deg);}

贼简单吧~ 这里有几个细节,第一个就是 transition 是放在元素上,而不是 active 上,如果放在active上,就没有回去的动作了,大家可以试一下。

                第二个就是,transform多个属性的时候,中间用空格来隔开,如果用逗号的话就没反应了。

但如果我在 :active 上加上 transition,

p{ width:50px; height:50px; border-radius:25px; background:#000; transition:1s ease-out;}
p:active{ transform:translateX(300px) rotate(780deg);transition:2s ease-in;}

这样按住的时候,就会执行 active 里面的transition,也就是去的时候用时 2s ,加速运动;而回来的时候执行 p 里的transition ,用时1s 减速运动。


最后就到 animation 了~ animation也是做动画的,不过功能比 transition 更加强大,因为animation可以控制动画的每一步,而transition只能控制开头和结尾。

语法 animation: 关键帧动画名字 、 动画时间 、 动画播放方式(函数) 、 延迟时间 、 循环次数 、 播放方向 、 播放状态 、 动画时间外的属性 ;

关键帧动画名字:就是你要执行的动画的名字,这里要先知道关键帧的语法

@keyframes name{
     0%{
       ...       
    }   
    50%{
       ...
    }
    100%{
       ...
    }  
}

这里的关键帧的名字就是name ,然后百分比的就是动画的进度,可以根据需要设置不同的百分比,再设置不同的动画。

动画时间:和transition一样~

动画播放方式(函数):和transition的过渡的函数一样~

延迟时间:和transition一样~

循环次数:动画播放的次数,默认为1,通常为整数,如果为 infinite,则无限次地播放;

播放方向:默认为normal,就是正常地向前播放,还有一个值是 alternate ,就是先正向播放,再反向播放,不断地交替;

播放状态:running(默认) 、 paused // 像播音乐一样可以通过动作来暂停动画;

动画时间外的属性: none(默认) 、 forwards 、 backwards 、both;

  举个例子吧:

@keyframes color{
    0%{ background:yellow}
    100%{ background:blue}
}
p{ background:black}

  none:    动画开始前:黑  ; 动画结束后:黑

  forwards:  动画开始前:黑 ; 动画结束后:蓝

  backwards: 动画开始之前:黄 ; 动画结束后:黑

  both:    动画开始前:黄 ; 动画结束后:蓝

就是这么简单~


最后,说一下这三个属性在JS中的写法:

transform:

obj.style.transform = "translateX(100px) scale(2)";
obj.style.webkitTransform = "translateX(100px) scale(2)";//带浏览器前缀

transition:

obj.style.transition = "1s";
obj.style.webKitTransition = "1s";//带浏览器前缀

animation:

obj.style.animation = "name 1s ";//1、关键帧先在css写好;2、兼容写法在关键帧里面写;

The above is the detailed content of Detailed explanation of transformation and animation in CSS3. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
css怎么隐藏元素但不占空间css怎么隐藏元素但不占空间Jun 01, 2022 pm 07:15 PM

两种方法:1、利用display属性,只需给元素添加“display:none;”样式即可。2、利用position和top属性设置元素绝对定位来隐藏元素,只需给元素添加“position:absolute;top:-9999px;”样式。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

css3什么是自适应布局css3什么是自适应布局Jun 02, 2022 pm 12:05 PM

自适应布局又称“响应式布局”,是指可以自动识别屏幕宽度、并做出相应调整的网页布局;这样的网页能够兼容多个不同的终端,而不是为每个终端做一个特定的版本。自适应布局是为解决移动端浏览网页而诞生的,能够为使用不同终端的用户提供很好的用户体验。

css3如何实现鼠标点击图片放大css3如何实现鼠标点击图片放大Apr 25, 2022 pm 04:52 PM

实现方法:1、使用“:active”选择器选中鼠标点击图片的状态;2、使用transform属性和scale()函数实现图片放大效果,语法“img:active {transform: scale(x轴放大倍数,y轴放大倍数);}”。

css3动画效果有变形吗css3动画效果有变形吗Apr 28, 2022 pm 02:20 PM

css3中的动画效果有变形;可以利用“animation:动画属性 @keyframes ..{..{transform:变形属性}}”实现变形动画效果,animation属性用于设置动画样式,transform属性用于设置变形样式。

css3怎么设置动画旋转速度css3怎么设置动画旋转速度Apr 28, 2022 pm 04:32 PM

在css3中,可以利用“animation-timing-function”属性设置动画旋转速度,该属性用于指定动画将如何完成一个周期,设置动画的速度曲线,语法为“元素{animation-timing-function:速度属性值;}”。

css3线性渐变可以实现三角形吗css3线性渐变可以实现三角形吗Apr 25, 2022 pm 02:47 PM

css3线性渐变可以实现三角形;只需创建一个45度的线性渐变,设置渐变色为两种固定颜色,一个是三角形的颜色,另一个为透明色即可,语法“linear-gradient(45deg,颜色值,颜色值 50%,透明色 50%,透明色 100%)”。

一文了解CSS3中的新特性 ::target-text 选择器一文了解CSS3中的新特性 ::target-text 选择器Apr 12, 2022 am 11:24 AM

本篇文章带大家一起深入了解一下CSS3中的新特性::target-text 选择器,聊聊该选择器的作用和使用方法,希望对大家有所帮助!

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool