Home Web Front-end HTML Tutorial 【css3】Learn about Transform, Transition and Animation through image carousel_html/css_WEB-ITnose

【css3】Learn about Transform, Transition and Animation through image carousel_html/css_WEB-ITnose

Jun 24, 2016 am 11:48 AM

The author likes to pursue visual enjoyment, although he often sacrifices performance and incompatibility, but he still enjoys it. This article will briefly understand Transform, Transition and Animation under CSS3 through demo demonstrations one by one.

This article needs to achieve the effect: (Please use chrome to open )

  • Picture carousel
  • Automatic picture carousel
  • Transform

    According to my understanding, transform, like width, height, and background, are all attributes of dom. The difference is that it is under css3. Compared with ?, it can move, scale, rotate, and adjust the original dom elements. Stretching or stretching is similar to some APIs on canvas. It seems that transform can do something that can only be done by js.

    Transform is divided into 2D transformation and 3D transformation. There are good introductions and examples in w3school. For details, please click: CSS3 2D transformation CSS3 3D transformation

    In order to facilitate the search, I put the Save a screenshot here:

    Transition

    The explanation of transition is transition. My understanding is the transformation between CSS, but this transformation is very smooth. Similar to animation. For example, the class of a certain DOM is classA at the beginning, and after some operation (such as being clicked), it becomes classB. If there is no transition, the transformation between classes is very fast, and it is completed instantly like a machine. However, With transition, this will be a very gentle and smooth process.

    We use demo to explain how to use transition.

    Write the following html file:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css"></style>  <img src='http://hanzichi.github.io/img/img0.png' />
    Copy after login

    It opens to a very simple picture, add css:

    img {  -webkit-transition: all 1s ease-in-out 0s;}img:hover {  -webkit-transform:     rotate(360deg)    scale(0.5, 0.5);  opacity: 0;}
    Copy after login

    Please click on the effect: transition transformation (ps: All demos are not compatible, please use chrome to open it )

    Is it very simple? In the demo, you only need to set the original attributes and attributes of the image (img tag) after hovering, and the transition process is all handled by transition! The transition is added under a certain element (the transition of the demo is added under the img tag), as if a listener is set. Once the attribute value of the element is about to change, the set attributes in the transition will be automatically checked. Once If a match is found, a smooth transition will be made.

    Transition has 4 properties, syntax: transition: property duration timing-function delay The 4 properties from front to back can be understood as "transition animation transformation properties" ", "Transition time spent", "Speed ​​change of transition process" and "Waiting time before transition starts" (default value: all 0 ease 0 The first two are required and the last two can be omitted).

    If not all attributes need to be smoothly transitioned, or each attribute transition time, speed and other settings have different requirements, you can separate the attributes to be transitioned with commas, and the demo can be written as:

    img {  -webkit-transition:     -webkit-transform 1s ease-in-out 0s,    opacity 1s ease-in-out 1s;}img:hover {  -webkit-transform:     rotate(360deg)    scale(0.5, 0.5);  opacity: 0;}
    Copy after login

    If you have to write the four attributes of transition separately, you can do this:

    img {  -webkit-transition-property: -webkit-transform, opacity;  -webkit-transition-duration: 1s;  -webkit-transition-timing-function: ease-in-out;  -webkit-transition-delay: 0s;}
    Copy after login

    I put the last three attributes together and just After writing a value (because the values ​​are the same), you can also write them separately like properties, separated by commas.

    Here we will introduce the value of timing-function. Six major values: (still stolen picture w3cschool)

    Summary: Generally, transition is used in dom class transformation. You can write the mechanical transformation first, and then add the transition effect.

    Animation

    The explanation of Animation is animation, an enhanced version of transition.

    If transition can achieve certain js effects, animation is more like js. Similar to writing a canvas special effect, the total time of the special effect, for example, we can specify when and what kind of scene should appear, and the conversion between each scene is completely responsible for CSS3 itself, and keyframes are like defining a js method.

    Mainly used when a certain element requires n consecutive css transformations. A simple demo is as follows: animation

    When we create animation in @keyframes, please bind it to a selector, otherwise the animation effect will not be produced.

    You can bind an animation to a selector by specifying at least two of the following CSS3 animation properties:

  • Specify the name of the animation
  • Specify the duration of the animation
  • Demo code:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style type="text/css">  img {    /*-webkit-animation: myfirst 5s;*/  }    @-webkit-keyframes myfirst  {    0% {      -webkit-transform:         rotate(0deg)        scale(1, 1);      opacity: 1;    }        50% {      -webkit-transform:         rotate(360deg)        scale(0.5, 0.5);      opacity: 0.5;    }        100% {      -webkit-transform:         rotate(0deg)        scale(1, 1)        translate(300px, 200px);      opacity: 1;    }  }  img {    -webkit-animation: myfirst 5s linear 0s 1 alternate;    /*停在结束位置*/    -webkit-animation-fill-mode: forwards;  }</style>  <img src='http://hanzichi.github.io/img/img0.png' />
    Copy after login

    For more information, please click CSS3 animation

    Specific application: Image carousel

    For similar applications, you can first write the code without transition, and then add transition between class conversions.

    The demo (automatic image rotation) has only a few lines of core code related to transition, while js simply assigns values ​​to the elements’ classes, and the animation process is all completed by css3!

    img {  position: absolute;  -webkit-transition: all 2s ease-out;}.disappear {  opacity: 0;}.show {  opacity: 1;}
    Copy after login

    When the img class is converted from show to disappear or from disappear to show (class transformation under the img tag), the transition animation set by the transition will be executed.

    The implementation of the other demo is also similar. If you are interested, you can refer to the source code: Please click on the source code

    Summary

    Generally speaking, transform just adds some attributes to the dom, and if combined with transition or animation, some animation effects can be achieved. I think transition is more commonly used in practice, and can be used with pseudo-classes or click events.

    The poster does not have a deep understanding of the above. If there is any discrepancy, please point it out.

    If you want to know more, you can refer to this article Introduction and Application Demonstration of CSS3 Transitions, Transforms and 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