6.轮播.js 1.5 KB

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