逼真的HTML5树叶飘落动画

梦是一种欲望,想是一种行动。梦想是梦与想的结晶。当心灵趋于平静时,精神便是永恒!把欲望降到最低点,把理性升华到最高点,你会感受到:平安是福,清新是禄,寡欲是寿!

这款HTML5树叶飘落动画是基于webkit内核的,也就是说要在webkit内核的浏览器上才能使用这款动画。

源码下载 演示地址

HTML代码

XML/HTML Code复制内容到剪贴板
  1. <divid="container">
  2. <!--Thecontainerisdynamicallypopulatedusingtheinitfunctioninleaves.js-->
  3. <!--Itsdimensionsandpositionaredefinedusingitsidselectorinleaves.css-->
  4. <divid="leafContainer"></div>
  5. <!--itsappearance,dimensions,andpositionaredefinedusingitsidselectorinleaves.css-->
  6. <divid="message">
  7. <em>这是基于webkit的落叶动画</em>
  8. </div>
  9. </div>

CSS代码

CSS Code复制内容到剪贴板
  1. #container{
  2. position:relative;
  3. height:700px;
  4. width:500px;
  5. margin:10pxauto;
  6. overflow:hidden;
  7. border:4pxsolid#5C090A;
  8. background:#4E4226url('images/backgroundLeaves.jpg')no-repeattopleft;
  9. }
  10. /*DefinesthepositionanddimensionsoftheleafContainerdiv*/
  11. #leafContainer
  12. {
  13. position:absolute;
  14. width:100%;
  15. height:100%;
  16. }
  17. /*Definestheappearance,position,anddimensionsofthemessagediv*/
  18. #message
  19. {
  20. position:absolute;
  21. top:160px;
  22. width:100%;
  23. height:300px;
  24. background:transparenturl('images/textBackground.png')repeat-xcenter;
  25. color:#5C090A;
  26. font-size:220%;
  27. font-family:'Georgia';
  28. text-align:center;
  29. padding:20px10px;
  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. /*Setsthecolorofthe"Dino'sGardeningService"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. /*ThisCSSruleisappliedtoalldivelementsintheleafContainerdiv.
  53. ItstylesandanimateseachleafDiv.
  54. */
  55. #leafContainer>div
  56. {
  57. position:absolute;
  58. width:100px;
  59. height:100px;
  60. /*Weusethefollowingpropertiestoapplythefadeanddropanimationstoeachleaf.
  61. Eachofthesepropertiestakestwovalues.Thesevaluesrespectivelymatchasetting
  62. forfadeanddrop.
  63. */
  64. -webkit-animation-iteration-count:infinite,infinite;
  65. -webkit-animation-direction:normal,normal;
  66. -webkit-animation-timing-function:linear,ease-in;
  67. }
  68. /*ThisCSSruleisappliedtoallimgelementsdirectlyinsidedivelementswhichare
  69. directlyinsidetheleafContainerdiv.Inotherwords,itmatchesthe'img'elements
  70. insidetheleafDivswhicharecreatedinthecreateALeaf()function.
  71. */
  72. #leafContainer>div>img{
  73. position:absolute;
  74. width:100px;
  75. height:100px;
  76. /*WeusethefollowingpropertiestoadjusttheclockwiseSpinorcounterclockwiseSpinAndFlip
  77. animationsoneachleaf.
  78. ThecreateALeaffunctionintheLeaves.jsfiledetermineswhetheraleafhasthe
  79. clockwiseSpinorcounterclockwiseSpinAndFlipanimation.
  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. /*Hidesaleaftowardstheveryendoftheanimation*/
  87. @-webkit-keyframesfade
  88. {
  89. /*Showaleafwhileintoorbelow95percentoftheanimationandhideit,otherwise*/
  90. 0%{opacity:1;}
  91. 95%{opacity:1;}
  92. 100%{opacity:0;}
  93. }
  94. /*Makesaleaffallfrom-300to600pixelsinthey-axis*/
  95. @-webkit-keyframesdrop
  96. {
  97. /*Movealeafto-300pixelsinthey-axisatthestartoftheanimation*/
  98. 0%{-webkit-transform:translate(0px,-50px);}
  99. /*Movealeafto600pixelsinthey-axisattheendoftheanimation*/
  100. 100%{-webkit-transform:translate(0px,650px);}
  101. }
  102. /*Rotatesaleaffrom-50to50degreesin2Dspace*/
  103. @-webkit-keyframesclockwiseSpin
  104. {
  105. /*Rotatealeafby-50degreesin2Dspaceatthestartoftheanimation*/
  106. 0%{-webkit-transform:rotate(-50deg);}
  107. /*Rotatealeafby50degreesin2Dspaceattheendoftheanimation*/
  108. 100%{-webkit-transform:rotate(50deg);}
  109. }
  110. /*Flipsaleafandrotatesitfrom50to-50degreesin2Dspace*/
  111. @-webkit-keyframescounterclockwiseSpinAndFlip
  112. {
  113. /*Flipaleafandrotateitby50degreesin2Dspaceatthestartoftheanimation*/
  114. 0%{-webkit-transform:scale(-1,1)rotate(50deg);}
  115. /*Flipaleafandrotateitby-50degreesin2Dspaceattheendoftheanimation*/
  116. 100%{-webkit-transform:scale(-1,1)rotate(-50deg);}
  117. }

JavaScript代码

JavaScript Code复制内容到剪贴板
  1. /*Definethenumberofleavestobeusedintheanimation*/
  2. constNUMBER_OF_LEAVES=30;
  3. /*
  4. Calledwhenthe"FallingLeaves"pageiscompletelyloaded.
  5. */
  6. functioninit()
  7. {
  8. /*Getareferencetotheelementthatwillcontaintheleaves*/
  9. varcontainer=document.getElementById('leafContainer');
  10. /*Filltheemptycontainerwithnewleaves*/
  11. for(vari=0;i<NUMBER_OF_LEAVES;i++)
  12. {
  13. container.appendChild(createALeaf());
  14. }
  15. }
  16. /*
  17. Receivesthelowestandhighestvaluesofarangeand
  18. returnsarandomintegerthatfallswithinthatrange.
  19. */
  20. functionrandomInteger(low,high)
  21. {
  22. returnlow+Math.floor(Math.random()*(high-low));
  23. }
  24. /*
  25. Receivesthelowestandhighestvaluesofarangeand
  26. returnsarandomfloatthatfallswithinthatrange.
  27. */
  28. functionrandomFloat(low,high)
  29. {
  30. returnlow+Math.random()*(high-low);
  31. }
  32. /*
  33. ReceivesanumberandreturnsitsCSSpixelvalue.
  34. */
  35. functionpixelValue(value)
  36. {
  37. returnvalue+'px';
  38. }
  39. /*
  40. Returnsadurationvalueforthefallinganimation.
  41. */
  42. functiondurationValue(value)
  43. {
  44. returnvalue+'s';
  45. }
  46. /*
  47. Usesanimgelementtocreateeachleaf."Leaves.css"implementstwospin
  48. animationsfortheleaves:clockwiseSpinandcounterclockwiseSpinAndFlip.This
  49. functiondetermineswhichofthesespinanimationsshouldbeappliedtoeachleaf.
  50. */
  51. functioncreateALeaf()
  52. {
  53. /*Startbycreatingawrapperdiv,andanemptyimgelement*/
  54. varleafDiv=document.createElement('div');
  55. varimage=document.createElement('img');
  56. /*Randomlychoosealeafimageandassignittothenewlycreatedelement*/
  57. image.src='images/realLeaf'+randomInteger(1,5)+'.png';
  58. leafDiv.style.top="-100px";
  59. /*Positiontheleafatarandomlocationalongthescreen*/
  60. leafDiv.style.left=pixelValue(randomInteger(0,500));
  61. /*Randomlychooseaspinanimation*/
  62. varspinAnimationName=(Math.random()<0.5)?'clockwiseSpin':'counterclockwiseSpinAndFlip';
  63. /*Setthe-webkit-animation-namepropertywiththesevalues*/
  64. leafDiv.style.webkitAnimationName='fade,drop';
  65. image.style.webkitAnimationName=spinAnimationName;
  66. /*Figureoutarandomdurationforthefadeanddropanimations*/
  67. varfadeAndDropDuration=durationValue(randomFloat(5,11));
  68. /*Figureoutanotherrandomdurationforthespinanimation*/
  69. varspinDuration=durationValue(randomFloat(4,8));
  70. /*Setthe-webkit-animation-durationpropertywiththesevalues*/
  71. leafDiv.style.webkitAnimationDuration=fadeAndDropDuration+','+fadeAndDropDuration;
  72. varleafDelay=durationValue(randomFloat(0,5));
  73. leafDiv.style.webkitAnimationDelay=leafDelay+','+leafDelay;
  74. image.style.webkitAnimationDuration=spinDuration;
  75. //addthe<img>tothe<div>
  76. leafDiv.appendChild(image);
  77. /*Returnthisimgelementsoitcanbeaddedtothedocument*/
  78. returnleafDiv;
  79. }
  80. /*Callstheinitfunctionwhenthe"FallingLeaves"pageisfullloaded*/
  81. window.addEventListener('load',init,false);

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