4.轮播.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. var pictures = document.querySelectorAll(".selected");
  2. var point = document.querySelectorAll(".point li");
  3. var prev = document.getElementById("prev");
  4. var next = document.getElementById("next");
  5. var list = document.getElementById("list");
  6. var current = 0;
  7. // 封装切换方法
  8. function autoPlay(ind) {
  9. console.log(ind,'下标')
  10. for(var i=0;i<point.length;i++) {
  11. pictures[i].className = "selected"
  12. point[i].className = "";
  13. }
  14. pictures[ind].className = "selected choose";
  15. point[ind].className = "active";
  16. }
  17. // 点击point 切换图片
  18. for(var i=0;i<point.length;i++) {
  19. point[i].index = i;
  20. point[i].onclick = function() {
  21. current = this.index;
  22. autoPlay(current);
  23. }
  24. }
  25. // 点击切换下一张
  26. next.onclick = function() {
  27. current++;
  28. if(current == point.length) {
  29. current = 0;
  30. }
  31. autoPlay(current);
  32. }
  33. // 点击切换上一张
  34. prev.onclick = function() {
  35. // console.log(point.length)
  36. current--;
  37. if(current < 0) {
  38. current = point.length - 1;
  39. }
  40. autoPlay(current);
  41. }
  42. // 自动播放
  43. var timer =setInterval(function(){
  44. next.onclick()
  45. },1000)
  46. // 鼠标移入停止播放
  47. list.onmousemove = function() {
  48. prev.style.display = 'block';
  49. next.style.display = 'block';
  50. clearInterval(timer);
  51. }
  52. // 鼠标移出继续播放
  53. list.onmouseout = function() {
  54. prev.style.display = 'none';
  55. next.style.display = 'none';
  56. timer =setInterval(function(){
  57. next.onclick()
  58. },1000)
  59. }