欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

逼真的HTML5树叶飘落动画_html5教程技巧

程序员文章站 2022-03-20 19:31:03
...
这款HTML5树叶飘落动画是基于webkit内核的,也就是说要在webkit内核的浏览器上才能使用这款动画。

逼真的HTML5树叶飘落动画_html5教程技巧

源码下载 演示地址

HTML代码

XML/HTML Code复制内容到剪贴板
  1. div id="container">
  2. div id="leafContainer">div>
  3. div id="message">
  4. em>这是基于webkit的落叶动画em>
  5. div>
  6. div>

CSS代码

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

JavaScript代码

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

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

逼真的HTML5树叶飘落动画_html5教程技巧

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频


相关标签: HTML5 树叶飘落 动画

上一篇: OpenCV NO.2

下一篇: 逼真的HTML5 3D水波动画 可多视角浏览代码图文介绍

推荐阅读