| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- (function (window, document) {
- 'use strict';
- var instanceCount = 0;
- function toArray(value) {
- return Array.prototype.slice.call(value || []);
- }
- function init(root, options) {
- if (!root || root.getAttribute('data-zq-carousel-ready') === 'true') {
- return null;
- }
- var settings = options || {};
- var track = root.querySelector('.bd > ul');
- var items = track ? toArray(track.children).filter(function (item) {
- return item.tagName === 'LI';
- }) : [];
- var indicators = toArray(root.querySelectorAll('.hd li'));
- var previous = root.querySelector('.prev');
- var next = root.querySelector('.next');
- var visible = Math.max(1, parseInt(settings.visible || '1', 10));
- var intervalTime = Math.max(1000, parseInt(settings.interval || '3000', 10));
- var index = 0;
- var timer = null;
- var instanceId = 'zq-carousel-' + (++instanceCount);
- if (!items.length) {
- return null;
- }
- items.forEach(function (item, itemIndex) {
- item.setAttribute('data-zq-carousel-slide', String(itemIndex));
- item.setAttribute('data-zq-original-display', item.style.display || '');
- });
- function pageCount() {
- return Math.ceil(items.length / visible);
- }
- function render() {
- var start = index * visible;
- var end = start + visible;
- items.forEach(function (item, itemIndex) {
- item.style.display = itemIndex >= start && itemIndex < end ? item.getAttribute('data-zq-original-display') : 'none';
- });
- indicators.forEach(function (indicator, indicatorIndex) {
- indicator.classList.toggle('on', indicatorIndex === index);
- });
- root.setAttribute('aria-label', settings.label || '内容轮播');
- root.setAttribute('data-zq-carousel-page', String(index));
- }
- function move(nextIndex) {
- var total = pageCount();
- index = (nextIndex + total) % total;
- render();
- }
- function stop() {
- if (timer) {
- window.clearInterval(timer);
- timer = null;
- }
- }
- function start() {
- stop();
- if (settings.autoPlay !== false && pageCount() > 1) {
- timer = window.setInterval(function () {
- move(index + 1);
- }, intervalTime);
- }
- }
- indicators.forEach(function (indicator, indicatorIndex) {
- indicator.setAttribute('role', 'button');
- indicator.setAttribute('tabindex', '0');
- indicator.setAttribute('aria-label', '切换到第 ' + (indicatorIndex + 1) + ' 组');
- indicator.addEventListener('click', function () {
- move(indicatorIndex);
- start();
- });
- indicator.addEventListener('keydown', function (event) {
- if (event.key === 'Enter' || event.key === ' ') {
- event.preventDefault();
- move(indicatorIndex);
- start();
- }
- });
- });
- if (previous) {
- previous.addEventListener('click', function (event) {
- event.preventDefault();
- move(index - 1);
- start();
- });
- }
- if (next) {
- next.addEventListener('click', function (event) {
- event.preventDefault();
- move(index + 1);
- start();
- });
- }
- root.id = root.id || instanceId;
- root.setAttribute('tabindex', '0');
- root.addEventListener('mouseenter', stop);
- root.addEventListener('mouseleave', start);
- root.addEventListener('focusin', stop);
- root.addEventListener('focusout', start);
- root.addEventListener('keydown', function (event) {
- if (event.key === 'ArrowLeft') {
- event.preventDefault();
- move(index - 1);
- } else if (event.key === 'ArrowRight') {
- event.preventDefault();
- move(index + 1);
- }
- });
- render();
- start();
- root.setAttribute('data-zq-carousel-ready', 'true');
- return {next: function () { move(index + 1); }, previous: function () { move(index - 1); }};
- }
- function initAll(scope) {
- toArray((scope || document).querySelectorAll('.slideBox')).forEach(function (root) {
- init(root, {visible: 1, interval: 3000, label: '首页横幅'});
- });
- toArray((scope || document).querySelectorAll('.picSlideBox')).forEach(function (root) {
- init(root, {visible: 5, interval: 4000, label: '图片列表'});
- });
- }
- window.ZQSimpleCarousel = {init: init, initAll: initAll};
- if (document.readyState === 'loading') {
- document.addEventListener('DOMContentLoaded', function () {
- initAll(document);
- });
- } else {
- initAll(document);
- }
- })(window, document);
|