9.轮播.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var picture = document.querySelectorAll(".pictures");
  2. var points = document.querySelectorAll("#list li");
  3. var prev = document.getElementById("prev");
  4. var next = document.getElementById("next");
  5. var container = document.getElementById("container");
  6. var currentIndex = 0;
  7. function autoPlay(num) {
  8. for (var i = 0; i < points.length; i++) {
  9. picture[i].className = 'pictures';
  10. points[i].className = '';
  11. }
  12. picture[num].className = 'pictures choose';
  13. points[num].className = 'active';
  14. }
  15. // 点击按钮切换图片
  16. for (var i = 0; i < points.length; i++) {
  17. points[i].index = i;
  18. points[i].onclick = function () {
  19. currentIndex = this.index;
  20. autoPlay(currentIndex);
  21. }
  22. }
  23. // 点击上一页
  24. prev.onclick = function () {
  25. currentIndex--;
  26. if (currentIndex < 0) {
  27. currentIndex = picture.length - 1;
  28. }
  29. autoPlay(currentIndex);
  30. }
  31. // 点击下一页
  32. next.onclick = function () {
  33. currentIndex++;
  34. if (currentIndex > picture.length - 1) {
  35. currentIndex = 0;
  36. }
  37. autoPlay(currentIndex);
  38. }
  39. // 自动播放
  40. var timer = setInterval(function () {
  41. next.onclick()
  42. }, 1000)
  43. // 鼠标滑入
  44. container.onmousemove = function () {
  45. prev.style.display = 'block';
  46. next.style.display = 'block';
  47. clearInterval(timer);
  48. }
  49. // 鼠标滑出
  50. container.onmouseout = function () {
  51. prev.style.display = 'none';
  52. next.style.display = 'none';
  53. timer = setInterval(function () {
  54. next.onclick()
  55. }, 1000)
  56. }