homework03.html 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. #container {
  8. width: 800px;
  9. height: 400px;
  10. border: 1px solid black;
  11. margin: 0 auto;
  12. }
  13. #panel {
  14. width: 800px;
  15. margin: 10px auto;
  16. text-align: center;
  17. }
  18. .piece {
  19. width: 80px;
  20. height: 80px;
  21. float: left;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="container"></div>
  27. <div id="panel">
  28. <button id="addBtn">添加</button>
  29. <button id="flaBtn">闪烁</button>
  30. </div>
  31. <script src="js/mylib.js"></script>
  32. <script>
  33. function randomColor() {
  34. var r = parseInt(Math.random() * 256);
  35. var g = parseInt(Math.random() * 256);
  36. var b = parseInt(Math.random() * 256);
  37. return 'rgb(' + r + ',' + g + ',' + b +')';
  38. }
  39. (function() {
  40. var counter = 0;
  41. bind($('addBtn'), 'click', function() {
  42. if (counter < 50) {
  43. counter += 1;
  44. var div = document.createElement('div');
  45. div.className = 'piece';
  46. div.style.backgroundColor = randomColor();
  47. $('container').appendChild(div);
  48. }
  49. });
  50. var timer = 0;
  51. bind($('flaBtn'), 'click', function() {
  52. if (!timer) {
  53. timer = setInterval(function() {
  54. var allPieces = $('container').children;
  55. for (var i = 0; i < allPieces.length; i += 1) {
  56. allPieces[i].style.backgroundColor = randomColor();
  57. }
  58. }, 200);
  59. }
  60. });
  61. }());
  62. </script>
  63. </body>
  64. </html>