8.下落的树叶.html 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. img {
  9. position: absolute;
  10. /* width: 200px; */
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <!-- <img src="./img/1.png" alt=""> -->
  16. <script>
  17. var screenWidth = document.body.clientWidth || document.documentElement.clientWidth;
  18. var screenHeight = document.body.clientHeight || document.documentElement.clientHeight;
  19. function Leaf() {
  20. this.width = Math.round(Math.random() * 100 + 100);
  21. this.top = 0;
  22. this.left = Math.random() * (screenWidth - this.width);
  23. this.urls = './img/'+ Math.floor(Math.random() * 4 + 1) +'.png'
  24. }
  25. // 绘制图片
  26. Leaf.prototype.init = function() {
  27. var imgs = document.createElement('img');
  28. imgs.src = this.urls;
  29. imgs.style.width = this.width + 'px';
  30. imgs.style.left = this.left + 'px';
  31. imgs.style.top = this.top + 'px';
  32. document.body.appendChild(imgs);
  33. }
  34. // 展示图片
  35. for(let i=0;i<20;i++) {
  36. let leaf = new Leaf();
  37. leaf.init();
  38. }
  39. </script>
  40. </body>
  41. </html>