homework01.html 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. #adv {
  12. width: 940px;
  13. margin: 0 auto;
  14. }
  15. #adv ul {
  16. width: 120px;
  17. height: 30px;
  18. margin: 0 auto;
  19. position: relative;
  20. top: -30px;
  21. }
  22. #adv li {
  23. width: 30px;
  24. height: 30px;
  25. list-style: none;
  26. float: left;
  27. color: #ccc;
  28. cursor: pointer;
  29. }
  30. #adv li:first-child {
  31. color: lightseagreen;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <div id="adv">
  37. <img src="img/slide-1.jpg" alt="">
  38. <ul>
  39. <li class="dot">●</li>
  40. <li class="dot">●</li>
  41. <li class="dot">●</li>
  42. <li class="dot">●</li>
  43. </ul>
  44. </div>
  45. <script>
  46. var img = document.querySelector('#adv>img');
  47. var items = document.querySelectorAll('#adv li');
  48. var timerId = 0;
  49. for (var i = 0; i < items.length; i += 1) {
  50. items[i].index = i;
  51. items[i].addEventListener('mouseover', function(evt) {
  52. index = evt.target.index;
  53. changeItemsColor(index);
  54. img.src = 'img/' + images[index];
  55. if (timerId != 0) {
  56. window.clearInterval(timerId);
  57. timerId = 0;
  58. }
  59. });
  60. items[i].addEventListener('mouseout', startIt);
  61. }
  62. var images = ['slide-1.jpg', 'slide-2.jpg', 'slide-3.jpg', 'slide-4.jpg'];
  63. var index = 0;
  64. startIt();
  65. function startIt() {
  66. if (timerId == 0) {
  67. timerId = window.setInterval(function() {
  68. index += 1;
  69. index %= images.length;
  70. changeItemsColor(index);
  71. img.src = 'img/' + images[index];
  72. }, 2000);
  73. }
  74. }
  75. function changeItemsColor(index) {
  76. for (var i = 0; i < items.length; i += 1) {
  77. items[i].style.color = '#ccc';
  78. }
  79. items[index].style.color = 'lightseagreen';
  80. }
  81. </script>
  82. </body>
  83. </html>