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