Home Web Front-end HTML Tutorial CSS Common Styles (4) Animation

CSS Common Styles (4) Animation

Aug 18, 2016 am 08:58 AM

  Part 3 Common CSS Styles (3) This blog post has already introduced the transition and transform with animated effects in CSS. Today, let’s talk about animation in CSS. The addition of animation will make the animation effect more optimistic.

 animation

  The implementation of animation needs to be implemented through keyframes. keyframes (keyframes), similar to keyframes in flash. Keyframes have their own syntax rules. Their naming starts with "@keyframes", followed by the "name of the animation" plus a pair of curly brackets "{}". In the brackets are some style rules for different time periods. , a bit like our css style writing method. For a style rule in "@keyframes" that is composed of multiple percentages, such as between "0%" and "100%", we can create multiple percentages in this rule, and we give each percentage a Elements that need animation effects are added with different attributes, so that the elements can achieve a constantly changing effect, such as moving, changing the element's color, position, size, shape, etc. However, one thing to note is that we can use "fromt" and "to" to represent where an animation starts and ends. In other words, "from" is equivalent to "0%" and "to" is equivalent to "100%", it is worth mentioning that "0%" cannot omit the percent sign like other attribute values. We must add the percent sign ("%") here. If not, we This keyframes is invalid and has no effect. Because the unit of keyframes only accepts percentage values.

                                                                                        ics

Kernel typeWebkit(Chrome/Safari)Gecko(Firefox)Presto(Opera)Trident(IE)W3C

 

 Attribute description:

 1. animation-name: Retrieve or set the animation name applied to the object. It must be used in conjunction with the rule @keyframes. The animation name can be chosen freely, and the semantics will be better

 2. animation-duration: Retrieve or set the duration of object animation

 3. animation-timing-function: Retrieve or set the transition type of object animation

Value:

 linear: linear transition. Equivalent to Bezier curve (0.0, 0.0, 1.0, 1.0)

 ease: smooth transition. Equivalent to Bezier curve (0.25, 0.1, 0.25, 1.0)

 ease-in: from slow to fast. Equivalent to Bezier curve (0.42, 0, 1.0, 1.0)

 ease-out: from fast to slow. Equivalent to Bezier curve (0, 0, 0.58, 1.0)

 ease-in-out: from slow to fast to slow again. Equivalent to Bezier curve (0.42, 0, 0.58, 1.0)

 cubic-bezier(, , , ): A specific Bezier curve type, the 4 values ​​​​must be within the interval [0, 1]

 4. animation-iteration-count: Retrieve or set the number of cycles of object animation

  Value:

  infinite: infinite loop

  number: The specific number of cycles of the specified object animation

 5. animation-direction: Retrieve or set whether the object animation moves in reverse in the loop

  Value:

 Normal: normal direction

  Alternate: normal and reverse alternating

 6. animation-play-state: Retrieve or set the state of object animation

 running:Sports

 paused: pause

 7. animation-fill-mode: Retrieve or set the state of the object outside of the animation time

  Value:

  none: Default value. Do not set the state other than object animation

  forwards: Set the object state to the state at the end of the animation

 backwards: Set the object state to the state when the animation starts

 Both: Set the object status to the end or start state of the animation

 The following is an example for comprehensive explanation:

  CSS code:

<span style="font-family: 'Microsoft YaHei'; font-size: 15px;"><span style="color: #800000;">    #animation
            </span>{<span style="color: #ff0000;">
                width</span>:<span style="color: #0000ff;"> 250px</span>;<span style="color: #ff0000;">
                height</span>:<span style="color: #0000ff;"> 250px</span>;<span style="color: #ff0000;">
                background-color</span>:<span style="color: #0000ff;"> brown</span>;<span style="color: #ff0000;">
                opacity</span>:<span style="color: #0000ff;"> 0.5</span>;<span style="color: #ff0000;">
                position</span>:<span style="color: #0000ff;">absolute</span>;<span style="color: #ff0000;">                
                left</span>:<span style="color: #0000ff;">40%</span>;<span style="color: #ff0000;">
                overflow</span>:<span style="color: #0000ff;"> hidden</span>;
                
            }<span style="color: #800000;">
            #animation span
            </span>{<span style="color: #ff0000;">
                font-family</span>:<span style="color: #0000ff;"> "微软雅黑"</span>;<span style="color: #ff0000;">
                font-size</span>:<span style="color: #0000ff;"> 20px</span>;<span style="color: #ff0000;">
                color</span>:<span style="color: #0000ff;"> #ccc</span>;<span style="color: #ff0000;">
                opacity</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
                display</span>:<span style="color: #0000ff;"> block</span>;<span style="color: #ff0000;">
                margin</span>:<span style="color: #0000ff;"> 30px</span>;
                
            }<span style="color: #800000;">
            #text1:hover
            </span>{<span style="color: #ff0000;">
                -moz-animation-play-state</span>:<span style="color: #0000ff;">paused</span>;/*鼠标经过时暂停动画*/<span style="color: #ff0000;">
                -webkit-animation-play-state</span>:<span style="color: #0000ff;">paused</span>;<span style="color: #ff0000;">
                -o-animation-play-state</span>:<span style="color: #0000ff;">paused</span>;<span style="color: #ff0000;">
                -ms-animation-play-state</span>:<span style="color: #0000ff;">paused</span>;<span style="color: #ff0000;">
                animation-play-state</span>:<span style="color: #0000ff;">paused</span>;

            }<span style="color: #800000;">
            #text2:hover
            </span>{<span style="color: #ff0000;">
                -moz-animation-play-state</span>:<span style="color: #0000ff;">paused</span>;<span style="color: #ff0000;">
                -webkit-animation-play-state</span>:<span style="color: #0000ff;">paused</span>;<span style="color: #ff0000;">
                -o-animation-play-state</span>:<span style="color: #0000ff;">paused</span>;<span style="color: #ff0000;">
                -ms-animation-play-state</span>:<span style="color: #0000ff;">paused</span>;<span style="color: #ff0000;">
                animation-play-state</span>:<span style="color: #0000ff;">paused</span>;

            }<span style="color: #800000;">
            #text1
            </span>{<span style="color: #ff0000;">
                
                -webkit-animation-name</span>:<span style="color: #0000ff;">animation1</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">动画名称</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
                -webkit-animation-duration</span>:<span style="color: #0000ff;">4s</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">动画持续时间</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
                -webkit-animation-timing-function</span>:<span style="color: #0000ff;">ease-in</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">变化由慢到快</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
                -webkit-animation-delay</span>:<span style="color: #0000ff;"> 2s</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">过了2S后开始动画</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
                -webkit-animation-iteration-count</span>:<span style="color: #0000ff;"> infinite</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">设置动画无限播放</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
                -webkit-transform</span>:<span style="color: #0000ff;"> translate(55px)</span>;<span style="color: #ff0000;">
                
                animation-name</span>:<span style="color: #0000ff;">animation1</span>;<span style="color: #ff0000;">
                animation-delay</span>:<span style="color: #0000ff;"> 4s</span>;<span style="color: #ff0000;">
                animation-timing-function</span>:<span style="color: #0000ff;"> ease-in</span>;<span style="color: #ff0000;">
                animation-delay</span>:<span style="color: #0000ff;"> 2s</span>;<span style="color: #ff0000;">
                animation-iteration-count</span>:<span style="color: #0000ff;"> infinite</span>;<span style="color: #ff0000;">
                transform</span>:<span style="color: #0000ff;"> translate(55px)</span>;<span style="color: #ff0000;">
                
                -ms-animation-name</span>:<span style="color: #0000ff;">animation1</span>;<span style="color: #ff0000;">
                -ms-animation-duration</span>:<span style="color: #0000ff;">4s </span>;<span style="color: #ff0000;">
                -ms-animation-timing-function</span>:<span style="color: #0000ff;"> ease-in</span>;<span style="color: #ff0000;">
                -ms-animation-delay</span>:<span style="color: #0000ff;"> 2s</span>;<span style="color: #ff0000;">
                -ms-animation-iteration-count</span>:<span style="color: #0000ff;"> infinite</span>;<span style="color: #ff0000;">
                -ms-transform</span>:<span style="color: #0000ff;"> translate(55px)</span>;<span style="color: #ff0000;">
                
                -moz-animation-name</span>:<span style="color: #0000ff;">animation1</span>;<span style="color: #ff0000;">
                -moz-animation-delay</span>:<span style="color: #0000ff;">4s </span>;<span style="color: #ff0000;">
                -moz-animation-timing-function</span>:<span style="color: #0000ff;"> ease-in</span>;<span style="color: #ff0000;">
                -moz-animation-delay</span>:<span style="color: #0000ff;"> 2s</span>;<span style="color: #ff0000;">
                -moz-animation-iteration-count</span>:<span style="color: #0000ff;"> infinite</span>;<span style="color: #ff0000;">    
                -moz-transform</span>:<span style="color: #0000ff;"> translate(55px)</span>;
            }<span style="color: #800000;">
            #text2
            </span>{<span style="color: #ff0000;">
                -webkit-animation-name</span>:<span style="color: #0000ff;">animation2</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">动画名称</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
                -webkit-animation-duration</span>:<span style="color: #0000ff;">4s</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">动画持续时间</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
                -webkit-animation-timing-function</span>:<span style="color: #0000ff;">ease-in</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">变化由慢到快</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
                -webkit-animation-delay</span>:<span style="color: #0000ff;"> 2s</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">过了2S后开始动画</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
                -webkit-animation-iteration-count</span>:<span style="color: #0000ff;"> infinite</span>;<span style="color: #008000;">/*</span><span style="color: #008000;">设置动画无限播放</span><span style="color: #008000;">*/</span><span style="color: #ff0000;">
                -webkit-transform</span>:<span style="color: #0000ff;"> translate(60px)</span>;<span style="color: #ff0000;">
                
                animation-name</span>:<span style="color: #0000ff;">animation2</span>;<span style="color: #ff0000;">
                animation-delay</span>:<span style="color: #0000ff;"> 4s</span>;<span style="color: #ff0000;">
                animation-timing-function</span>:<span style="color: #0000ff;"> ease-in</span>;<span style="color: #ff0000;">
                animation-delay</span>:<span style="color: #0000ff;"> 2s</span>;<span style="color: #ff0000;">
                animation-iteration-count</span>:<span style="color: #0000ff;"> infinite</span>;<span style="color: #ff0000;">
                transform</span>:<span style="color: #0000ff;"> translate(60px)</span>;<span style="color: #ff0000;">
                
                -ms-animation-name</span>:<span style="color: #0000ff;">animation2</span>;<span style="color: #ff0000;">
                -ms-animation-duration</span>:<span style="color: #0000ff;">4s </span>;<span style="color: #ff0000;">
                -ms-animation-timing-function</span>:<span style="color: #0000ff;"> ease-in</span>;<span style="color: #ff0000;">
                -ms-animation-delay</span>:<span style="color: #0000ff;"> 2s</span>;<span style="color: #ff0000;">
                -ms-animation-iteration-count</span>:<span style="color: #0000ff;"> infinite</span>;<span style="color: #ff0000;">
                -ms-transform</span>:<span style="color: #0000ff;"> translate(60px)</span>;<span style="color: #ff0000;">
                
                -moz-animation-name</span>:<span style="color: #0000ff;">animation2</span>;<span style="color: #ff0000;">
                -moz-animation-delay</span>:<span style="color: #0000ff;">4s </span>;<span style="color: #ff0000;">
                -moz-animation-timing-function</span>:<span style="color: #0000ff;"> ease-in</span>;<span style="color: #ff0000;">
                -moz-animation-delay</span>:<span style="color: #0000ff;"> 2s</span>;<span style="color: #ff0000;">
                -moz-animation-iteration-count</span>:<span style="color: #0000ff;"> infinite</span>;<span style="color: #ff0000;">    
                -moz-transform</span>:<span style="color: #0000ff;"> translate(60px)</span>;        
                    
            }<span style="color: #800000;">
            @-webkit-keyframes animation1
            </span>{<span style="color: #ff0000;">
                0%{-webkit-transform</span>:<span style="color: #0000ff;"> translate(-10px)</span>;<span style="color: #ff0000;">opacity</span>:<span style="color: #0000ff;"> 0</span>;}<span style="color: #800000;">
                20%</span>{<span style="color: #ff0000;">-webkit-transform</span>:<span style="color: #0000ff;"> translate(25px)</span>;<span style="color: #ff0000;">opacity</span>:<span style="color: #0000ff;"> 0.5</span>;}<span style="color: #800000;">
                45%</span>{<span style="color: #ff0000;">-webkit-transform</span>:<span style="color: #0000ff;"> translate(45px)</span>;<span style="color: #ff0000;">opacity</span>:<span style="color: #0000ff;"> 1</span>;}<span style="color: #800000;">
                100%</span>{<span style="color: #ff0000;">-webkit-transform</span>:<span style="color: #0000ff;"> translate(60px)</span>;<span style="color: #ff0000;">opacity</span>:<span style="color: #0000ff;"> 0.8</span>;}<span style="color: #800000;">
                
            }
            @-webkit-keyframes animation2
            </span>{<span style="color: #ff0000;">
                0%{-webkit-transform</span>:<span style="color: #0000ff;"> translate(280px)</span>;<span style="color: #ff0000;">opacity</span>:<span style="color: #0000ff;"> 0</span>;}<span style="color: #800000;">
                30%</span>{<span style="color: #ff0000;">-webkit-transform</span>:<span style="color: #0000ff;"> translate(200px)</span>;<span style="color: #ff0000;">opacity</span>:<span style="color: #0000ff;"> 0.5</span>;}<span style="color: #800000;">
                65%</span>{<span style="color: #ff0000;">-webkit-transform</span>:<span style="color: #0000ff;"> translate(130px)</span>;<span style="color: #ff0000;">opacity</span>:<span style="color: #0000ff;"> 1</span>;}<span style="color: #800000;">                
                100%</span>{<span style="color: #ff0000;">-webkit-transform</span>:<span style="color: #0000ff;"> translate(60px)</span>;<span style="color: #ff0000;">opacity</span>:<span style="color: #0000ff;">0.8</span>;}<span style="color: #800000;">
            }</span></span>
Copy after login

  HTML代码: 

<span style="font-family: 'Microsoft YaHei'; font-size: 16px;"><span style="color: #000000;"><div id="animation">
    </span><span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="text1"</span><span style="color: #0000ff;">></span>这是ly婠婠的博客<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="text2"</span><span style="color: #0000ff;">></span>欢迎访问和评论!<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>            
<span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span>
Copy after login

效果如下:

解析说明:

在这个例子中,效果如上图。这里主要是利用animation和translate来达到一个文字渐进的效果。translate的作用是让文字根据给定值发生平移。animation则利用关键帧和百分比数值来将平移过程细分成几个帧,然后设置持续时间,一帧帧连接起来形成动画。

Writing
-webkit-animation
-moz-animation
-ms-animation
animation
Statement of this Website
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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles