7.轮播.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var picture = document.querySelectorAll(".selected");
  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 current = 0;
  7. // 切换方法
  8. function autoPlay(ind) {
  9. console.log(ind);
  10. // 清空所有样式
  11. for(var i=0;i<points.length;i++) {
  12. points[i].className = '';
  13. picture[i].className = 'selected';
  14. }
  15. // 添加对应项样式
  16. points[ind].className = 'active';
  17. picture[ind].className = 'selected choose'
  18. }
  19. // 点击points 切换图片
  20. for(var i=0;i<points.length;i++) {
  21. points[i].index = i;
  22. points[i].onclick = function() {
  23. current = this.index;
  24. autoPlay(current)
  25. }
  26. }
  27. // 点击上一页
  28. prev.onclick = function() {
  29. current--;
  30. if(current < 0) {
  31. current = picture.length - 1;
  32. }
  33. autoPlay(current);
  34. }
  35. // 点击下一页
  36. next.onclick = function() {
  37. current++;
  38. if(current > picture.length - 1) {
  39. current = 0;
  40. }
  41. autoPlay(current);
  42. }
  43. // 自动轮播
  44. var timer = setInterval(function(){
  45. next.onclick()
  46. },1000);
  47. // 鼠标划过
  48. container.onmousemove = function() {
  49. prev.style.display = 'block';
  50. next.style.display = 'block';
  51. clearInterval(timer);
  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. }