homework03.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. margin: 10px auto;
  11. border: 1px solid black;
  12. overflow: hidden;
  13. }
  14. #buttons {
  15. width: 800px;
  16. margin: 10px auto;
  17. text-align: center;
  18. }
  19. #add, #fla {
  20. border: none;
  21. outline: none;
  22. width: 80px;
  23. height: 30px;
  24. background-color: red;
  25. color: white;
  26. font-size: 16px;
  27. cursor: pointer;
  28. }
  29. .small {
  30. width: 80px;
  31. height: 80px;
  32. float: left;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div id="container"></div>
  38. <div id="buttons">
  39. <button id="add">添加</button>
  40. <button id="fla">闪烁</button>
  41. </div>
  42. <script src="js/mylib.js"></script>
  43. <script>
  44. var bigDiv = document.querySelector('#container');
  45. var addButton = document.querySelector('#add');
  46. addButton.addEventListener('click', function() {
  47. var smallDiv = document.createElement('div');
  48. smallDiv.className = 'small';
  49. // smallDiv.style.width = '80px';
  50. // smallDiv.style.height = '80px';
  51. // smallDiv.style.float = 'left';
  52. smallDiv.style.backgroundColor = randomColor();
  53. bigDiv.insertBefore(smallDiv, bigDiv.firstChild);
  54. });
  55. var flaButton = document.querySelector('#fla');
  56. var isFlashing = false;
  57. var timerId;
  58. flaButton.addEventListener('click', function(evt) {
  59. isFlashing = !isFlashing;
  60. if (isFlashing) {
  61. timerId = window.setInterval(function() {
  62. var divs = document.querySelectorAll('#container>div');
  63. for (var i = 0; i < divs.length; i += 1) {
  64. divs[i].style.backgroundColor = randomColor();
  65. }
  66. }, 200);
  67. flaButton.textContent = '暂停';
  68. } else {
  69. window.clearInterval(timerId);
  70. flaButton.textContent = '闪烁';
  71. }
  72. });
  73. </script>
  74. </body>
  75. </html>