main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. window.addEventListener('load', function() {
  2. var leftArrow = this.document.querySelector('.lf');
  3. var rightArrow = this.document.querySelector('.lr');
  4. // 鼠标移动到左右箭头的位置更换图片 有灰色背景的图片
  5. leftArrow.addEventListener('mouseenter', function() {
  6. this.style.backgroundPosition = '0 0';
  7. });
  8. leftArrow.addEventListener('mouseleave', function() {
  9. this.style.backgroundPosition = '-83px 0';
  10. });
  11. rightArrow.addEventListener('mouseenter', function() {
  12. this.style.backgroundPosition = '-42px 0';
  13. });
  14. rightArrow.addEventListener('mouseleave', function() {
  15. this.style.backgroundPosition = '-123px 0';
  16. });
  17. // 获取图片 和 小点
  18. var imgs = this.document.querySelectorAll('.img');
  19. var dots = this.document.querySelector('.dots').querySelectorAll('span');
  20. // 给图片设置index 属性,好判断当前的图片是哪一张
  21. for (let i = 0; i < imgs.length; i++) {
  22. imgs[i].setAttribute('data-index', i);
  23. }
  24. // 获取当前图片 和 图片的index(数组下标)
  25. var current = this.document.querySelector('.current');
  26. var currentIndex = current.getAttribute('data-index');
  27. // 左箭头的点击事件,点击了就返回前一张图片
  28. // 如果当前图片为第一张那么需要更换到最后一张图片也就是第四张
  29. leftArrow.addEventListener('click', function() {
  30. if (currentIndex > 0) {
  31. imgs[currentIndex].classList.remove('current');
  32. dots[currentIndex].classList.remove('square');
  33. imgs[--currentIndex].classList.add('current');
  34. dots[currentIndex].classList.add('square');
  35. } else {
  36. imgs[currentIndex].classList.remove('current');
  37. dots[currentIndex].classList.remove('square');
  38. currentIndex = 4;
  39. imgs[currentIndex].classList.add('current');
  40. dots[currentIndex].classList.add('square');
  41. }
  42. });
  43. // 点击右箭头下一张图片切换
  44. // 如果当前为第五张图片,那么切换回第一张图片
  45. rightArrow.addEventListener('click', changeImage);
  46. var timer = this.setInterval(changeImage, 8000);
  47. function changeImage() {
  48. if (currentIndex < 4) {
  49. imgs[currentIndex].classList.remove('current');
  50. dots[currentIndex].classList.remove('square');
  51. imgs[++currentIndex].classList.add('current');
  52. dots[currentIndex].classList.add('square');
  53. } else {
  54. imgs[currentIndex].classList.remove('current');
  55. dots[currentIndex].classList.remove('square');
  56. currentIndex = 0;
  57. imgs[currentIndex].classList.add('current');
  58. dots[currentIndex].classList.add('square');
  59. }
  60. };
  61. // 小圆点的点击事件
  62. for (let k = 0; k < dots.length; k++) {
  63. dots[k].setAttribute('data-index', k);
  64. dots[k].addEventListener('click', function() {
  65. var index = this.getAttribute('data-index');
  66. if (index != currentIndex) {
  67. imgs[currentIndex].classList.remove('current');
  68. dots[currentIndex].classList.remove('square');
  69. imgs[index].classList.add('current');
  70. dots[index].classList.add('square');
  71. currentIndex = index;
  72. }
  73. })
  74. }
  75. });