search
HomeWeb Front-endH5 TutorialRealistic HTML5 leaf falling animation_html5 tutorial skills

This HTML5 leaf falling animation is based on the webkit core, which means that this animation can only be used on a browser with a webkit core.

Source code download Demo address

HTML code

XML/HTML CodeCopy content to clipboard
  1. div id="container" >
  2. div id="leafContainer" >div>
  3. div id="message" >
  4. em> div>
  5. div>
  6. CSS code CSS CodeCopy content to clipboard
    1. #container {   
    2.     positionrelative;   
    3.     height700px;   
    4.     width500px;   
    5.     margin10px auto;   
    6.     overflowhidden;   
    7.     border4px solid #5C090A;   
    8.     background#4E4226 url('images/backgroundLeaves.jpg'no-repeat top left;   
    9. }   
    10.   
    11. /* Defines the position and dimensions of the leafContainer div */  
    12. #leafContainer    
    13. {   
    14.     positionabsolute;   
    15.     width: 100%;   
    16.     height: 100%;   
    17. }  
    18.   
    19. /* Defines the appearance, position, and dimensions of the message div */  
    20. #message   
    21. {   
    22.     positionabsolute;   
    23.     top160px;   
    24.     width: 100%;   
    25.     height300px;   
    26.     background:transparent url('images/textBackground.png'repeat-x center;   
    27.     color#5C090A;   
    28.     font-size: 220%;   
    29.     font-family'Georgia';   
    30.     text-aligncenter;   
    31.     padding20px 10px;   
    32.     -webkit-box-sizing: border-box;   
    33.     -webkit-background-size: 100% 100%;   
    34.     z-index: 1;   
    35. }   
    36.   
    37. p {   
    38.   margin15px;   
    39. }   
    40.   
    41. a   
    42. {   
    43.   color#5C090A;   
    44.   text-decorationnone;   
    45. }   
    46.   
    47. /* Sets the color of the "Dino's Gardening Service" message */  
    48. em    
    49. {   
    50.     font-weightbold;   
    51.     font-stylenormal;   
    52. }   
    53.   
    54. .phone {   
    55.   font-size: 150%;   
    56.   vertical-alignmiddle;   
    57. }  
    58.   
    59. /* This CSS rule is applied to all div elements in the leafContainer div.  
    60.    It styles and animates each leafDiv.  
    61. */  
    62. #leafContainer > div    
    63. {   
    64.     positionabsolute;   
    65.     width100px;   
    66.     height100px;   
    67.   
    68.     /* We use the following properties to apply the fade and drop animations to each leaf.  
    69.        Each of these properties takes two values. These values respectively match a setting  
    70.        for fade and drop.  
    71.     */  
    72.     -webkit-animation-iteration-count: infinite, infinite;   
    73.     -webkit-animation-directionnormalnormal;   
    74.     -webkit-animation-timing-function: linear, ease-in;   
    75. }  
    76.   
    77. /* This CSS rule is applied to all img elements directly inside div elements which are  
    78.    directly inside the leafContainer div. In other words, it matches the 'img' elements  
    79.    inside the leafDivs which are created in the createALeaf() function.  
    80. */  
    81. #leafContainer > div > img {   
    82.      positionabsolute;   
    83.      width100px;   
    84.      height100px;   
    85.   
    86.     /* We use the following properties to adjust the clockwiseSpin or counterclockwiseSpinAndFlip  
    87.        animations on each leaf.  
    88.        The createALeaf function in the Leaves.js file determines whether a leaf has the   
    89.        clockwiseSpin or counterclockwiseSpinAndFlip animation.  
    90.     */  
    91.      -webkit-animation-iteration-count: infinite;   
    92.      -webkit-animation-direction: alternate;   
    93.      -webkit-animation-timing-function: ease-in-out;   
    94.      -webkit-transform-origin: 50% -100%;   
    95. }   
    96.   
    97. /* Hides a leaf towards the very end of the animation */  
    98. @-webkit-keyframes fade   
    99. {   
    100.     /* Show a leaf while into or below 95 percent of the animation and hide it, otherwise */  
    101.     0%   { opacity: 1; }   
    102.     95%  { opacity: 1; }   
    103.     100% { opacity: 0; }   
    104. }  
    105.   
    106. /* Makes a leaf fall from -300 to 600 pixels in the y-axis */  
    107. @-webkit-keyframes drop   
    108. {   
    109.     /* Move a leaf to -300 pixels in the y-axis at the start of the animation */  
    110.     0%   { -webkit-transform: translate(0px, -50px); }   
    111.     /* Move a leaf to 600 pixels in the y-axis at the end of the animation */  
    112.     100% { -webkit-transform: translate(0px650px); }   
    113. }   
    114.   
    115. /* Rotates a leaf from -50 to 50 degrees in 2D space */  
    116. @-webkit-keyframes clockwiseSpin   
    117. {   
    118.     /* Rotate a leaf by -50 degrees in 2D space at the start of the animation */  
    119.     0%   { -webkit-transform: rotate(-50deg); }   
    120.     /*  Rotate a leaf by 50 degrees in 2D space at the end of the animation */  
    121.     100% { -webkit-transform: rotate(50deg); }   
    122. }   
    123.   
    124. /* Flips a leaf and rotates it from 50 to -50 degrees in 2D space */  
    125. @-webkit-keyframes counterclockwiseSpinAndFlip    
    126. {   
    127.     /* Flip a leaf and rotate it by 50 degrees in 2D space at the start of the animation */  
    128.     0%   { -webkit-transform: scale(-1, 1) rotate(50deg); }   
    129.     /* Flip a leaf and rotate it by -50 degrees in 2D space at the end of the animation */  
    130.     100% { -webkit-transform: scale(-1, 1) rotate(-50deg); }   
    131. }   

JavaScript代码

JavaScript Code复制内容到剪贴板
  1. /* Define the number of leaves to be used in the animation */  
  2. const NUMBER_OF_LEAVES = 30;   
  3.   
  4. /*   
  5.     Called when the "Falling Leaves" page is completely loaded.  
  6. */  
  7. function init()   
  8. {   
  9.     /* Get a reference to the element that will contain the leaves */  
  10.     var container = document.getElementById('leafContainer');   
  11.     /* Fill the empty container with new leaves */  
  12.     for (var i = 0; i 
  13.     {   
  14.         container.appendChild(createALeaf());   
  15.     }   
  16. }   
  17.   
  18. /*  
  19.     Receives the lowest and highest values of a range and  
  20.     returns a random integer that falls within that range.  
  21. */  
  22. function randomInteger(low, high)   
  23. {   
  24.     return low   Math.floor(Math.random() * (high - low));   
  25. }   
  26.   
  27. /*  
  28.    Receives the lowest and highest values of a range and  
  29.    returns a random float that falls within that range.  
  30. */  
  31. function randomFloat(low, high)   
  32. {   
  33.     return low   Math.random() * (high - low);   
  34. }   
  35.   
  36. /*  
  37.     Receives a number and returns its CSS pixel value.  
  38. */  
  39. function pixelValue(value)   
  40. {   
  41.     return value   'px';   
  42. }  
  43.   
  44. /*  
  45.     Returns a duration value for the falling animation.  
  46. */  
  47.   
  48. function durationValue(value)   
  49. {   
  50.     return value   's';   
  51. }  
  52.   
  53. /*  
  54.     Uses an img element to create each leaf. "Leaves.css" implements two spin   
  55.     animations for the leaves: clockwiseSpin and counterclockwiseSpinAndFlip. This  
  56.     function determines which of these spin animations should be applied to each leaf.  
  57.  
  58. */  
  59. function createALeaf()   
  60. {   
  61.     /* Start by creating a wrapper div, and an empty img element */  
  62.     var leafDiv = document.createElement('div');   
  63.     var image = document.createElement('img');   
  64.   
  65.     /* Randomly choose a leaf image and assign it to the newly created element */  
  66.     image.src = 'images/realLeaf'   randomInteger(1, 5)   '.png';   
  67.   
  68.     leafDiv.style.top = "-100px";   
  69.   
  70.     /* Position the leaf at a random location along the screen */  
  71.     leafDiv.style.left = pixelValue(randomInteger(0, 500));   
  72.   
  73.     /* Randomly choose a spin animation */  
  74.     var spinAnimationName = (Math.random() 'clockwiseSpin' : 'counterclockwiseSpinAndFlip';   
  75.   
  76.     /* Set the -webkit-animation-name property with these values */  
  77.     leafDiv.style.webkitAnimationName = 'fade, drop';   
  78.     image.style.webkitAnimationName = spinAnimationName;   
  79.   
  80.     /* Figure out a random duration for the fade and drop animations */  
  81.     var fadeAndDropDuration = durationValue(randomFloat(5, 11));   
  82.   
  83.     /* Figure out another random duration for the spin animation */  
  84.     var spinDuration = durationValue(randomFloat(4, 8));   
  85.     /* Set the -webkit-animation-duration property with these values */  
  86.     leafDiv.style.webkitAnimationDuration = fadeAndDropDuration   ', '   fadeAndDropDuration;   
  87.   
  88.     var leafDelay = durationValue(randomFloat(0, 5));   
  89.     leafDiv.style.webkitAnimationDelay = leafDelay   ', '   leafDelay;   
  90.   
  91.     image.style.webkitAnimationDuration = spinDuration;   
  92.   
  93.     // add the Realistic HTML5 leaf falling animation_html5 tutorial skills to the 
      
  94.     leafDiv.appendChild(image);   
  95.   
  96.     /* Return this img element so it can be added to the document */  
  97.     return leafDiv;   
  98. }   
  99.   
  100. /* Calls the init function when the "Falling Leaves" page is full loaded */  
  101. window.addEventListener('load', init, false);   

以上就是本文的全部内容,希望对大家学习有所帮助。

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
H5: The Future of Web Content and DesignH5: The Future of Web Content and DesignMay 01, 2025 am 12:12 AM

H5 (HTML5) will improve web content and design through new elements and APIs. 1) H5 enhances semantic tagging and multimedia support. 2) It introduces Canvas and SVG, enriching web design. 3) H5 works by extending HTML functionality through new tags and APIs. 4) Basic usage includes creating graphics using it, and advanced usage involves WebStorageAPI. 5) Developers need to pay attention to browser compatibility and performance optimization.

H5: New Features and Capabilities for Web DevelopmentH5: New Features and Capabilities for Web DevelopmentApr 29, 2025 am 12:07 AM

H5 brings a number of new functions and capabilities, greatly improving the interactivity and development efficiency of web pages. 1. Semantic tags such as enhance SEO. 2. Multimedia support simplifies audio and video playback through and tags. 3. Canvas drawing provides dynamic graphics drawing tools. 4. Local storage simplifies data storage through localStorage and sessionStorage. 5. The geolocation API facilitates the development of location-based services.

H5: Key Improvements in HTML5H5: Key Improvements in HTML5Apr 28, 2025 am 12:26 AM

HTML5 brings five key improvements: 1. Semantic tags improve code clarity and SEO effects; 2. Multimedia support simplifies video and audio embedding; 3. Form enhancement simplifies verification; 4. Offline and local storage improves user experience; 5. Canvas and graphics functions enhance the visualization of web pages.

HTML5: The Standard and its Impact on Web DevelopmentHTML5: The Standard and its Impact on Web DevelopmentApr 27, 2025 am 12:12 AM

The core features of HTML5 include semantic tags, multimedia support, offline storage and local storage, and form enhancement. 1. Semantic tags such as, etc. to improve code readability and SEO effect. 2. Simplify multimedia embedding with labels. 3. Offline storage and local storage such as ApplicationCache and LocalStorage support network-free operation and data storage. 4. Form enhancement introduces new input types and verification properties to simplify processing and verification.

H5 Code Examples: Practical Applications and TutorialsH5 Code Examples: Practical Applications and TutorialsApr 25, 2025 am 12:10 AM

H5 provides a variety of new features and functions, greatly enhancing the capabilities of front-end development. 1. Multimedia support: embed media through and elements, no plug-ins are required. 2. Canvas: Use elements to dynamically render 2D graphics and animations. 3. Local storage: implement persistent data storage through localStorage and sessionStorage to improve user experience.

The Connection Between H5 and HTML5: Similarities and DifferencesThe Connection Between H5 and HTML5: Similarities and DifferencesApr 24, 2025 am 12:01 AM

H5 and HTML5 are different concepts: HTML5 is a version of HTML, containing new elements and APIs; H5 is a mobile application development framework based on HTML5. HTML5 parses and renders code through the browser, while H5 applications need to run containers and interact with native code through JavaScript.

The Building Blocks of H5 Code: Key Elements and Their PurposeThe Building Blocks of H5 Code: Key Elements and Their PurposeApr 23, 2025 am 12:09 AM

Key elements of HTML5 include,,,,,, etc., which are used to build modern web pages. 1. Define the head content, 2. Used to navigate the link, 3. Represent the content of independent articles, 4. Organize the page content, 5. Display the sidebar content, 6. Define the footer, these elements enhance the structure and functionality of the web page.

HTML5 and H5: Understanding the Common UsageHTML5 and H5: Understanding the Common UsageApr 22, 2025 am 12:01 AM

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

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 Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.