zq-simple-carousel.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. (function (window, document) {
  2. 'use strict';
  3. var instanceCount = 0;
  4. function toArray(value) {
  5. return Array.prototype.slice.call(value || []);
  6. }
  7. function init(root, options) {
  8. if (!root || root.getAttribute('data-zq-carousel-ready') === 'true') {
  9. return null;
  10. }
  11. var settings = options || {};
  12. var track = root.querySelector('.bd > ul');
  13. var items = track ? toArray(track.children).filter(function (item) {
  14. return item.tagName === 'LI';
  15. }) : [];
  16. var indicators = toArray(root.querySelectorAll('.hd li'));
  17. var previous = root.querySelector('.prev');
  18. var next = root.querySelector('.next');
  19. var visible = Math.max(1, parseInt(settings.visible || '1', 10));
  20. var intervalTime = Math.max(1000, parseInt(settings.interval || '3000', 10));
  21. var index = 0;
  22. var timer = null;
  23. var instanceId = 'zq-carousel-' + (++instanceCount);
  24. if (!items.length) {
  25. return null;
  26. }
  27. items.forEach(function (item, itemIndex) {
  28. item.setAttribute('data-zq-carousel-slide', String(itemIndex));
  29. item.setAttribute('data-zq-original-display', item.style.display || '');
  30. });
  31. function pageCount() {
  32. return Math.ceil(items.length / visible);
  33. }
  34. function render() {
  35. var start = index * visible;
  36. var end = start + visible;
  37. items.forEach(function (item, itemIndex) {
  38. item.style.display = itemIndex >= start && itemIndex < end ? item.getAttribute('data-zq-original-display') : 'none';
  39. });
  40. indicators.forEach(function (indicator, indicatorIndex) {
  41. indicator.classList.toggle('on', indicatorIndex === index);
  42. });
  43. root.setAttribute('aria-label', settings.label || '内容轮播');
  44. root.setAttribute('data-zq-carousel-page', String(index));
  45. }
  46. function move(nextIndex) {
  47. var total = pageCount();
  48. index = (nextIndex + total) % total;
  49. render();
  50. }
  51. function stop() {
  52. if (timer) {
  53. window.clearInterval(timer);
  54. timer = null;
  55. }
  56. }
  57. function start() {
  58. stop();
  59. if (settings.autoPlay !== false && pageCount() > 1) {
  60. timer = window.setInterval(function () {
  61. move(index + 1);
  62. }, intervalTime);
  63. }
  64. }
  65. indicators.forEach(function (indicator, indicatorIndex) {
  66. indicator.setAttribute('role', 'button');
  67. indicator.setAttribute('tabindex', '0');
  68. indicator.setAttribute('aria-label', '切换到第 ' + (indicatorIndex + 1) + ' 组');
  69. indicator.addEventListener('click', function () {
  70. move(indicatorIndex);
  71. start();
  72. });
  73. indicator.addEventListener('keydown', function (event) {
  74. if (event.key === 'Enter' || event.key === ' ') {
  75. event.preventDefault();
  76. move(indicatorIndex);
  77. start();
  78. }
  79. });
  80. });
  81. if (previous) {
  82. previous.addEventListener('click', function (event) {
  83. event.preventDefault();
  84. move(index - 1);
  85. start();
  86. });
  87. }
  88. if (next) {
  89. next.addEventListener('click', function (event) {
  90. event.preventDefault();
  91. move(index + 1);
  92. start();
  93. });
  94. }
  95. root.id = root.id || instanceId;
  96. root.setAttribute('tabindex', '0');
  97. root.addEventListener('mouseenter', stop);
  98. root.addEventListener('mouseleave', start);
  99. root.addEventListener('focusin', stop);
  100. root.addEventListener('focusout', start);
  101. root.addEventListener('keydown', function (event) {
  102. if (event.key === 'ArrowLeft') {
  103. event.preventDefault();
  104. move(index - 1);
  105. } else if (event.key === 'ArrowRight') {
  106. event.preventDefault();
  107. move(index + 1);
  108. }
  109. });
  110. render();
  111. start();
  112. root.setAttribute('data-zq-carousel-ready', 'true');
  113. return {next: function () { move(index + 1); }, previous: function () { move(index - 1); }};
  114. }
  115. function initAll(scope) {
  116. toArray((scope || document).querySelectorAll('.slideBox')).forEach(function (root) {
  117. init(root, {visible: 1, interval: 3000, label: '首页横幅'});
  118. });
  119. toArray((scope || document).querySelectorAll('.picSlideBox')).forEach(function (root) {
  120. init(root, {visible: 5, interval: 4000, label: '图片列表'});
  121. });
  122. }
  123. window.ZQSimpleCarousel = {init: init, initAll: initAll};
  124. if (document.readyState === 'loading') {
  125. document.addEventListener('DOMContentLoaded', function () {
  126. initAll(document);
  127. });
  128. } else {
  129. initAll(document);
  130. }
  131. })(window, document);