search
HomeWeb Front-endHTML Tutorial动画animation的三个应用(漂浮的白云、旋转的星球、正方体合成) - 小火柴的蓝色理想

前面的话

  前面介绍过动画animation的详细用法,本文主要介绍动画animation的三个效果

 

漂浮的白云

【效果演示】

【简要介绍】

  漂浮的白云主要通过远景白云和近景白云来实现立体漂浮效果。远景和近景分别使用两张背景图片,通过改变其背景定位来实现白云移动效果,通过设置不同的动画持续时间来实现交错漂浮的效果

【主要代码】

<span style="color: #800000;">.box</span>{<span style="color: #ff0000;">
    position</span>:<span style="color: #0000ff;"> relative</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 300px</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 500px</span>;
}<span style="color: #800000;">    
.in1,.in2</span>{<span style="color: #ff0000;">
    position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 100%</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 100%</span>;<span style="color: #ff0000;">
    background-size</span>:<span style="color: #0000ff;">cover</span>;<span style="color: #ff0000;">
    animation</span>:<span style="color: #0000ff;"> move 100s infinite linear alternate</span>;
}<span style="color: #800000;">
@keyframes move</span>{<span style="color: #ff0000;">
    100%{background-position</span>:<span style="color: #0000ff;"> 500% 0</span>;}<span style="color: #800000;">
}
.in1</span>{<span style="color: #ff0000;">
    background-image</span>:<span style="color: #0000ff;"> url('http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/cloud.png')</span>;   
}<span style="color: #800000;">
.in2</span>{<span style="color: #ff0000;">
    background-image</span>:<span style="color: #0000ff;"> url('http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/cloud1.png')</span>;<span style="color: #ff0000;">
    animation-duration</span>:<span style="color: #0000ff;"> 10s</span>;
}
<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="box"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="in1"</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="in2"</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span></span>

源码查看

 

旋转的星球

【效果演示】

【简要介绍】

  旋转的星球主要通过rotate()旋转函数来实现。实际上,蓝色的地球和黑色的月球并没有发生旋转,只是其父级旋转形成的视觉上的旋转效果

【代码演示】

<span style="color: #800000;">.box</span>{<span style="color: #ff0000;">
    transform</span>:<span style="color: #0000ff;"> scale(0.5)</span>;<span style="color: #ff0000;">
    position</span>:<span style="color: #0000ff;"> relative</span>;<span style="color: #ff0000;">
    padding</span>:<span style="color: #0000ff;"> 1px</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 300px</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 300px</span>;
}<span style="color: #800000;">    
.sunline</span>{<span style="color: #ff0000;">
    position</span>:<span style="color: #0000ff;">relative</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 400px</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 400px</span>;<span style="color: #ff0000;">
    border</span>:<span style="color: #0000ff;"> 2px solid black</span>;<span style="color: #ff0000;">
    border-radius</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;"> 50px 0 0 50px</span>;<span style="color: #ff0000;">
    display</span>:<span style="color: #0000ff;"> flex</span>;<span style="color: #ff0000;">
    animation</span>:<span style="color: #0000ff;"> rotate 10s infinite linear</span>;
}<span style="color: #800000;">
.sun</span>{<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;"> auto</span>;<span style="color: #ff0000;">
    background-color</span>:<span style="color: #0000ff;"> red</span>;<span style="color: #ff0000;">
    border-radius</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;">
    box-shadow</span>:<span style="color: #0000ff;"> 5px 5px 10px red,-5px -5px 10px red,5px -5px 10px red,-5px 5px 10px red</span>;
}<span style="color: #800000;">
.earthline</span>{<span style="color: #ff0000;">
    position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;">
    right</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
    top</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 200px</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 200px</span>;<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;"> -100px -100px 0 0</span>;<span style="color: #ff0000;">
    border</span>:<span style="color: #0000ff;"> 1px solid black</span>;<span style="color: #ff0000;">
    border-radius</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;">
    display</span>:<span style="color: #0000ff;"> flex</span>;<span style="color: #ff0000;">
    animation</span>:<span style="color: #0000ff;"> rotate 2s infinite linear</span>;
}<span style="color: #800000;">
.earth</span>{<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;"> auto</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 50px</span>;<span style="color: #ff0000;">
    background-color</span>:<span style="color: #0000ff;"> blue</span>;<span style="color: #ff0000;">
    border-radius</span>:<span style="color: #0000ff;"> 50%</span>;
}<span style="color: #800000;">
.moon</span>{<span style="color: #ff0000;">
    position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;">
    left</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
    top</span>:<span style="color: #0000ff;"> 50%</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 20px</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 20px</span>;<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;"> -10px 0 0 -10px</span>;<span style="color: #ff0000;">
    background-color</span>:<span style="color: #0000ff;"> black</span>;<span style="color: #ff0000;">
    border-radius</span>:<span style="color: #0000ff;"> 50%</span>;
}<span style="color: #800000;">
@keyframes rotate</span>{<span style="color: #ff0000;">
    100%{transform</span>:<span style="color: #0000ff;">rotate(360deg)</span>;}<span style="color: #800000;">
}</span>
<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="box"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="sunline"</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="sun"</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="earthline"</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="earth"</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
            <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="moon"</span><span style="color: #0000ff;">></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span></span></span></span></span>

源码查看 

 

正方体合成

【效果演示】

【简要介绍】

  该效果主要通过设置计算后的延迟时间来达到正方体的各个边顺序动画的效果。一次动画结束后,通过触发animationend事件重置animation-name来实现重复动画的效果

【代码演示】

<span style="color: #800000;">ul</span>{<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
    padding</span>:<span style="color: #0000ff;"> 0</span>;<span style="color: #ff0000;">
    list-style</span>:<span style="color: #0000ff;"> none</span>;
}<span style="color: #800000;">
.box</span>{<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;">
    perspective</span>:<span style="color: #0000ff;"> 500px</span>;<span style="color: #ff0000;">
    margin</span>:<span style="color: #0000ff;"> 50px 0 0 50px</span>;
}<span style="color: #800000;">    
.list</span>{<span style="color: #ff0000;">
    position</span>:<span style="color: #0000ff;"> relative</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;">
    background-color</span>:<span style="color: #0000ff;"> blue</span>;<span style="color: #ff0000;">
    transform-style</span>:<span style="color: #0000ff;"> preserve-3d</span>;<span style="color: #ff0000;">
    transform-origin</span>:<span style="color: #0000ff;"> 0 0 0</span>;<span style="color: #ff0000;">
    animation</span>:<span style="color: #0000ff;"> rotate 1s  10s 3 both linear</span>;
}<span style="color: #800000;">
.in</span>{<span style="color: #ff0000;">
    position</span>:<span style="color: #0000ff;"> absolute</span>;<span style="color: #ff0000;">
    height</span>:<span style="color: #0000ff;"> 100px</span>;<span style="color: #ff0000;">
    width</span>:<span style="color: #0000ff;"> 100px</span>;
}<span style="color: #800000;">
.list .in:nth-child(6)</span>{<span style="color: #ff0000;">
    background-color</span>:<span style="color: #0000ff;"> pink</span>;<span style="color: #ff0000;">
    transform-origin</span>:<span style="color: #0000ff;"> top</span>;<span style="color: #ff0000;">
    animation</span>:<span style="color: #0000ff;"> in6 2s both</span>;
}<span style="color: #800000;">
.list .in:nth-child(5)</span>{<span style="color: #ff0000;">
    background-color</span>:<span style="color: #0000ff;"> lightgreen</span>;<span style="color: #ff0000;">
    transform-origin</span>:<span style="color: #0000ff;"> right</span>;<span style="color: #ff0000;">
    animation</span>:<span style="color: #0000ff;"> in5 2s 2s both</span>;
}<span style="color: #800000;">
.list .in:nth-child(4)</span>{<span style="color: #ff0000;">
    background-color</span>:<span style="color: #0000ff;"> lightblue</span>;<span style="color: #ff0000;">
    transform-origin</span>:<span style="color: #0000ff;"> bottom</span>;<span style="color: #ff0000;">
    animation</span>:<span style="color: #0000ff;"> in4 2s 4s both</span>;
}<span style="color: #800000;">
.list .in:nth-child(3)</span>{<span style="color: #ff0000;">
    background-color</span>:<span style="color: #0000ff;"> lightcoral</span>;<span style="color: #ff0000;">
    transform-origin</span>:<span style="color: #0000ff;"> left</span>;<span style="color: #ff0000;">
    animation</span>:<span style="color: #0000ff;"> in3 2s 6s both</span>;
}<span style="color: #800000;">
.list .in:nth-child(2)</span>{<span style="color: #ff0000;">
    background-color</span>:<span style="color: #0000ff;"> lightcyan</span>;<span style="color: #ff0000;">
    animation</span>:<span style="color: #0000ff;"> in2 2s 8s both</span>;
}<span style="color: #800000;">
.list .in:nth-child(1)</span>{<span style="color: #ff0000;">background-color</span>:<span style="color: #0000ff;"> lightsalmon</span>;}<span style="color: #800000;">
.box:hover .list</span>{<span style="color: #ff0000;">animation-play-state</span>:<span style="color: #0000ff;"> paused</span>;}<span style="color: #800000;">
.box:hover .in</span>{<span style="color: #ff0000;">animation-play-state</span>:<span style="color: #0000ff;"> paused</span>;}<span style="color: #800000;">
@keyframes in6</span>{<span style="color: #ff0000;">100%{transform</span>:<span style="color: #0000ff;"> rotateX(90deg)</span>;}<span style="color: #800000;">}
@keyframes in5</span>{<span style="color: #ff0000;">100%{transform</span>:<span style="color: #0000ff;"> rotateY(90deg)</span>;}<span style="color: #800000;">}
@keyframes in4</span>{<span style="color: #ff0000;">100%{transform</span>:<span style="color: #0000ff;"> rotateX(-90deg)</span>;}<span style="color: #800000;">}
@keyframes in3</span>{<span style="color: #ff0000;">100%{transform</span>:<span style="color: #0000ff;"> rotateY(-90deg)</span>;}<span style="color: #800000;">}
@keyframes in2</span>{<span style="color: #ff0000;">100%{transform</span>:<span style="color: #0000ff;"> translateZ(100px)</span>;}<span style="color: #800000;">}
@keyframes rotate</span>{<span style="color: #ff0000;">100%{transform</span>:<span style="color: #0000ff;"> rotate3d(1,1,1,360deg)</span>;}<span style="color: #800000;">}</span>
<span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="box"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">ul </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="list"</span><span style="color: #ff0000;"> id</span><span style="color: #0000ff;">="list"</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="in"</span><span style="color: #0000ff;">></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="in"</span><span style="color: #0000ff;">></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="in"</span><span style="color: #0000ff;">></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="in"</span><span style="color: #0000ff;">></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="in"</span><span style="color: #0000ff;">></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="in"</span><span style="color: #0000ff;">></span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"></span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span>
list.addEventListener('animationend',<span style="color: #0000ff;">function</span><span style="color: #000000;">(e){
    e </span>= e ||<span style="color: #000000;"> event;
    </span><span style="color: #0000ff;">var</span> target = e.target ||<span style="color: #000000;"> e.srcElement;
    </span><span style="color: #0000ff;">if</span>(target.nodeName == 'UL'<span style="color: #000000;">){
        list.style.animationName </span>= 'none'<span style="color: #000000;">;
        </span><span style="color: #0000ff;">var</span> children = list.getElementsByTagName('li'<span style="color: #000000;">);
        </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">var</span> i = 0; i ){
            children[i].style.animationName = 'none'<span style="color: #000000;">;
        }
        setTimeout(</span><span style="color: #0000ff;">function</span><span style="color: #000000;">(){
            list.style.animationName </span>= 'rotate'<span style="color: #000000;">;
            </span><span style="color: #0000ff;">var</span> children = list.getElementsByTagName('li'<span style="color: #000000;">);
            </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">var</span> i = 0; i ){
                children[i].style.animationName = 'in' + (i+1<span style="color: #000000;">);
            }        
        },</span>100<span style="color: #000000;">);        
    }
},</span><span style="color: #0000ff;">false</span>);

源码查看

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
Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

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

Video Face Swap

Video Face Swap

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

Hot Article

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools