5.轮播图.js 1.4 KB

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