使用CSS3 +JS实现图片预加载的动画效果

效果图:

代码:


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>图片预加载</title>
  6. </head>
  7. <style>
  8. .content {
  9. width: 600px;
  10. margin: auto;
  11. }
  12. img {
  13. max-width: 100%;
  14. }
  15. .loading {
  16. width: 150px;
  17. height: 40px;
  18. margin: 50px auto;
  19. }
  20. .loading span {
  21. display: inline-block;
  22. width: 8px;
  23. height: 100%;
  24. border-radius: 4px;
  25. background: lightgreen;
  26. margin-left: 5px;
  27. -webkit-animation: load 2s ease infinite;
  28. }
  29. @-webkit-keyframes load {
  30. 0%,
  31. 100% {
  32. height: 40px;
  33. background: lightgreen;
  34. }
  35. 50% {
  36. height: 70px;
  37. margin: -15px 0;
  38. margin-left: 5px;
  39. background: lightblue;
  40. }
  41. }
  42. .loading span:nth-child(2) {
  43. -webkit-animation-delay: 0.2s;
  44. }
  45. .loading span:nth-child(3) {
  46. -webkit-animation-delay: 0.4s;
  47. }
  48. .loading span:nth-child(4) {
  49. -webkit-animation-delay: 0.6s;
  50. }
  51. .loading span:nth-child(5) {
  52. -webkit-animation-delay: 0.8s;
  53. }
  54. .loading span:nth-child(6) {
  55. -webkit-animation-delay: 1s;
  56. }
  57. </style>
  58. <body>
  59. <div class="content">
  60. <div class="content_choose zero">
  61. <img class="p0" src="">
  62. <div class="loading">
  63. <span></span>
  64. <span></span>
  65. <span></span>
  66. <span></span>
  67. <span></span>
  68. <span></span>
  69. </div>
  70. </div>
  71. <div class="content_choose one"> <img class="p1" src="">
  72. <div class="loading">
  73. <span></span>
  74. <span></span>
  75. <span></span>
  76. <span></span>
  77. <span></span>
  78. <span></span>
  79. </div>
  80. </div>
  81. <div class="content_choose two"> <img class="p2" src="">
  82. <div class="loading">
  83. <span></span>
  84. <span></span>
  85. <span></span>
  86. <span></span>
  87. <span></span>
  88. <span></span>
  89. </div>
  90. </div>
  91. <div class="content_choose three"><img class="p3" src="">
  92. <div class="loading">
  93. <span></span>
  94. <span></span>
  95. <span></span>
  96. <span></span>
  97. <span></span>
  98. <span></span>
  99. </div>
  100. </div>
  101. <div class="content_choose one"> <img class="p4" src="">
  102. <div class="loading">
  103. <span></span>
  104. <span></span>
  105. <span></span>
  106. <span></span>
  107. <span></span>
  108. <span></span>
  109. </div>
  110. </div>
  111. <div class="content_choose two"> <img class="p5" src="">
  112. <div class="loading">
  113. <span></span>
  114. <span></span>
  115. <span></span>
  116. <span></span>
  117. <span></span>
  118. <span></span>
  119. </div>
  120. </div>
  121. <div class="content_choose three"><img class="p6" src="">
  122. <div class="loading">
  123. <span></span>
  124. <span></span>
  125. <span></span>
  126. <span></span>
  127. <span></span>
  128. <span></span>
  129. </div>
  130. </div>
  131. </div>
  132. </body>
  133. <script src="jquery-1.11.3.min.1.js"></script>
  134. <script>
  135. /*添加要加载的图片*/
  136. var gifList = ["list/service-service01.png", "list/service-service02.png", "list/service-service03.png",
  137. "list/service-service04.png", "list/service-service05.png", "list/service-service06.png", "list/service-service07.png"
  138. ];
  139. for (var i = 0; i < gifList.length; i++) {
  140. (function(index) {
  141. loadImage(gifList[index], function() {
  142. var image = $(".p" + index);
  143. image.next().remove();
  144. image.attr("src", gifList[index]).hide().fadeIn(500);
  145. })
  146. })(i)
  147. }
  148. function loadImage(url, callback) {
  149. var image = new Image;
  150. image.onload = function() {
  151. image.onload = null,
  152. callback(image)
  153. },
  154. image.src = url;
  155. }
  156. </script>
  157. </html>

 

文章来源: albertyang.blog.csdn.net,作者:Albert Yang,版权归原作者所有,如需转载,请联系作者。

原文链接:albertyang.blog.csdn.net/article/details/103456064

(完)