Table of Contents
The @keyframes
The Animation
Add browser prefix:
矩形变圆形实例
基本元素
Declare Keyframes
应用动画
使用Timing-Function
Last Things
Home Web Front-end HTML Tutorial First experience with css Animation_html/css_WEB-ITnose

First experience with css Animation_html/css_WEB-ITnose

Jun 24, 2016 am 11:45 AM

More and more websites are using animation, whether they are displayed in GIF, SVG, WebGL, video background or other forms. Appropriate use of animation can make a website more lively and interactive, adding an extra layer of feedback and experience for users.

In this tutorial I will introduce you to CSS animations; which have become more and more popular as browser support improves, CSS animations are very performant in doing some things. After covering the basics, we'll build a quick example: animating a rectangle turning into a circle.

See the demo here

@keyframes and animation Introduction

The main components of css animation: @keyframes, css rules for creating animations. Think of @keyframes as a timeline of animation steps. In @keyframes, you can define these steps, each with a different style statement.

The second step is to enable the CSS animation to run. You need to bind a selector to @keyframes. Based on these steps, all code declared by @keyframes will be parsed and the new style will be initialized.

The @keyframes

Here we will set the steps for animation. The properties of @keyframes are as follows:

  • The name of the selector (in this example is tutsFade) .
  • Steps: 0%-100%; from (equal to 0%) and to (equal to 100%).
  • CSS style: to be applied at each stage Style.
  • Example:

    @keyframes tutsFade {

    0% {

    opacity: 1 ;

    }

    100% {

    opacity: 0 ;

    }

    }

    or:

    @keyframes tutsFade {

    from {

    opacity: 1 ;

    }

    to {

    opacity: 0 ;

    }

    }

    Abbreviation:

    @keyframes tutsFade {

    to {

    opacity: 0 ;

    }

    }

    The above code applies an opacity transition to the element, from opacity: 1 to opacity: 0. The effect achieved by the above three methods is the same.

    The Animation

    animation attributes:

  • animation-name: @keyframes name (in this case tutsFade).
  • animation-duration: time interval, total length of animation from start to end.
  • animation-timing-function: set animation speed (linear | ease | ease-in | ease-out | ease-in- out | cubic-bezier ).
  • animation-delay: The delay before the animation starts.
  • animation-iteration-count: How many times it will iterate during the animation.
  • animation-direction: Change the direction of the loop, from start to end, end to start, or both.
  • animation-fill-mode: Specify the style to be applied to the element when the animation ends (none | forwards | backwards | both ).
  • For example:

    .element {

    animation-name: tutsFade;

    animation-duration: 4 s;

    animation-delay: 1 s;

    animation-iteration-count: infinite;

    animation-timing-function: linear;

    animation-direction: alternate;

    }

    abbreviated as:

    .element {

    animation: tutsFade 4 s 1 s linear infinite alternate;

    }

    The above code will create a blinking effect, 1 second animation delay, 4 second animation interval, alternating directions and infinite linear loop iteration.

    Add browser prefix:

    At work, we need to use browser-specific prefixes to ensure the best browser support. Standard prefix application:

  • Chrome & Safari: -webkit-
  • Firefox: -moz-
  • Opera: -o-
  • Internet Explorer: -ms-
  • 动画属性使用了浏览器前缀的形式:

    .element {

         -webkit-animation: tutsFade 4 s 1 s infinite linear alternate;

         -moz-animation: tutsFade 4 s 1 s infinite linear alternate;

         -ms-animation: tutsFade 4 s 1 s infinite linear alternate;

         -o-animation: tutsFade 4 s 1 s infinite linear alternate;

         animation: tutsFade 4 s 1 s infinite linear alternate;

    }

    @keyframes的前缀使用:

    @-webkit-keyframes tutsFade { /* your style */ }

    @-moz-keyframes tutsFade { /* your style */ }

    @-ms-keyframes tutsFade { /* your style */ }

    @-o-keyframes tutsFade { /* your style */ }

    @keyframes tutsFade { /* your style */ }

     

    更多浏览器前缀: http://css3please.com/

     

    多动画

    使用逗号分割添加多动画。为tutsFade 元素添加回转,我们要声明一个额外的@keyframes然后绑定到元素上:

    .element {

       animation: tutsFade 4 s 1 s infinite linear alternate, tutsRotate 4 s 1 s infinite linear alternate;

    }

    @keyframes tutsFade {

       to {

         opacity: 0 ;

       }

    }

    @keyframes tutsRotate {

       to {

         transform: rotate( 180 deg);

       }

    }

     

    ------------------------------------分割线--------------------------------------------------------------------

    矩形变圆形实例

    这个例子中总共有五个步骤,每个步骤将为元素定义一个圆角,一个回转和不同的背景色,下面是实现的步骤和代码。

    基本元素

    首先创建一个标记,动画的元素。甚至不用class,仅仅只用一个div:

    Then use element selection to add styles to the div:

    div {

    width : 200px ;

    height : 200px ;

    background-color: coral;

    }

    Declare Keyframes

    Define a @keyframes named square-to-circle. The five steps are as follows:

    @keyframes square-to-circle {

    0% {}

    25% {}

    50% {}

    75% {}

    100% {}

    }

    You need to define some styles for each step, start by defining rounded corners for each rectangle corner:

    @-webkit-keyframes square-to- circle {

    0% {

    border-radius: 0 0 0 0 ;

    }

    25% {

    border-radius: 50% 0 0 0 ;

    }

    50% {

    border-radius: 50% 50% 0 0 ;

    }

    75% {

    border-radius: 50% 50% 50% 0 ;

    }

    100% {

    border-radius: 50% ;

    }

    }

    Then define a different background color for each step:

    @keyframes square -to- circle {

    0% {

    border-radius: 0 0 0 0 ;

    background :coral;

    }

    25 % {

    border-radius: 50% 0 0 0 ;

    background: darksalmon;

    }

    50% {

    border-radius: 50% 50% 0 0 ;

    background :indianred; % 50% 0 ;

    background :lightcoral;

    }

    100% {

    border-radius: 50%;

    background :darksalmon;

    }

    }

    Rotate the DIV to add a little visual effect:

    @keyframes square-to- circle {

       0%  {

         border-radius: 0 0 0 0 ;

         background :coral;

         transform:rotate( 0 deg);

       }

       25%  {

         border-radius: 50% 0 0 0 ;

         background :darksalmon;

         transform:rotate( 45 deg);

       }

       50%  {

         border-radius: 50% 50% 0 0 ;

         background :indianred;

         transform:rotate( 90 deg);

       }

       75%  {

         border-radius: 50% 50% 50% 0 ;

         background :lightcoral;

         transform:rotate( 135 deg);

       }

       100% { 

         border-radius: 50% ;

         background :darksalmon;

         transform:rotate( 180 deg);

       }

    }

    应用动画

    定义了square-to-circle动画后,需要将它应用到div上:

    div {

       width : 200px ;

       height : 200px ;

       background-color : coral;

       animation: square-to- circle 2 s 1 s infinite alternate; 

    }

    上面使用了简写的动画属性,它们的状态是:

  • 动画名:square-to-circle.
  • 动画间隔:2s.
  • 动画延迟:1s.
  • 动画循环次数是无限的.
  • 动画运行方向是交替的, 首先从开始到结束,然后返回到最开始,然后到结束,循环往复。
  • 使用Timing-Function

    可以为animation添加的最后一个属性是animation-timing-function.定义移动的速度,加速或者减速。这个函数可以是一个非常详细的值,尴尬的手动计算,但是有很多免费的网站为timing-function提供资源和在线定制。

    例如:CSS Easing Animation Tool,现在来计算我们的定时功能。

    运用立方贝塞尔函数为square-to-circle动画添加伸缩效果。

    div {

       width : 200px ;

       height : 200px ;

       background-color : coral;

       animation: square-to- circle 2 s 1 s infinite cubic-bezier( 1 ,. 015 ,. 295 , 1.225 ) alternate; 

    }

    The final code without using the browser prefix ( -webkit- , -moz- , -ms- , -o- ) is as follows:

    div {

    width : 200px;

    height: 200px;

    background-color: coral;

    animation: square-to- circle 2 s . 5 s infinite cubic-bezier( 1 , . 015 ,. 295 , 1.225 ) alternate;

    }

    @keyframes square-to- circle {

    0% {

    border-radius: 0 0 0 0 ;

    background :coral;

    transform:rotate( 0 deg);

    }

    25% {

    ; border-radius: 50% 0 0 0 ;

    background: darksalmon;

    transform:rotate( 45 deg);

    }

    50% {

    border-radius: 50% 50% 0 0 ;

    background :indianred;

    transform:rotate( 90 deg);

    }

    75% {

    border-radius: 50% 50% 50% 0 ;

    background: lightcoral;

    transform: rotate( 135 deg);

    }

    100% {

    border-radius: 50%;

    background: darksalmon;

    transform:rotate( 180 deg);

    }

    }

    Last Things

    View in Modern Everything runs fine in the browser, but rendering objects in Firefox is a bit lacking and the edges appear jagged:

    Fortunately, there is a workaround. After adding a transparent outline to the div, Firefox will render perfectly!

    outline: 1px solid transparent; >

    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

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
    3 weeks 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)

    Hot Topics

    Java Tutorial
    1671
    14
    PHP Tutorial
    1276
    29
    C# Tutorial
    1256
    24
    HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

    The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

    The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

    HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

    HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

    The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

    The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

    HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

    The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

    HTML vs. CSS and JavaScript: Comparing Web Technologies HTML vs. CSS and JavaScript: Comparing Web Technologies Apr 23, 2025 am 12:05 AM

    HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

    HTML: Is It a Programming Language or Something Else? HTML: Is It a Programming Language or Something Else? Apr 15, 2025 am 12:13 AM

    HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

    What is the difference between <strong>, <b> tags and <em>, <i> tags? What is the difference between <strong>, <b> tags and <em>, <i> tags? Apr 28, 2025 pm 05:42 PM

    The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.

    See all articles