123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- let carouselWidth, carousel;
- +function init() {
- let carouselRoot = document.createElement('div');
- carouselRoot.classList.add('root');
- carousel = document.querySelector("#Carousel");
- try {
- carouselWidth = Number(carousel.dataset.width);
- } catch (err) {
- console.log("%c ERROR ", "background-color:red;color:white;", err);
- carouselWidth = 600;
- }
- carousel.style.width = `${carouselWidth}px`;
- carousel.parentElement.append(carouselRoot);
- carouselRoot.append(carousel);
- if (carousel && carousel.children) {
- let inner = document.createElement('div'),
- index = 0;
- let images = carousel.querySelectorAll("[data-image]");
- for (let image of images) {
- let img = document.createElement("img");
- img.src = image.dataset.image;
- img.style.width = `${carouselWidth}px`;
- img.dataset.index = index++;
- inner.append(img);
- }
- inner.classList.add('inner', 'loaded');
- while (carousel.children.length > 0) {
- carousel.children[0].remove();
- }
- carousel.append(inner);
- let nextImage = function () {
- inner.style.transition = `left 200ms ease 0s`;
- inner.style.left = `-${carouselWidth * 2}px`;
- try {
- setDots(Number(inner.children[2].dataset.index));
- } catch (err) {
- console.log("%c ERROR ", "background-color:red;color:white;", err);
- }
- setTimeout(function (e, w) {
- e.style.transition = 'none';
- e.append(e.firstElementChild);
- e.style.left = `-${w}px`;
- }, 200, inner, carouselWidth);
- };
- let interval = setInterval(function () {
- if (inner.clientHeight != 0) {
- carousel.style.height = `${inner.clientHeight}px`;
- inner.style.position = 'absolute';
- inner.style.left = `-${carouselWidth}px`;
- inner.classList.remove('loaded');
- clearInterval(interval);
- }
- }, 100);
- let autoInterval = setInterval(nextImage, 5000);
- // Кнопки переключения
- let left = document.createElement('div'),
- right = document.createElement('div');
- left.classList.add('left');
- right.classList.add('right');
- left.addEventListener('click', function () {
- inner.style.transition = `left 200ms ease 0s`;
- inner.style.left = `0px`;
- try {
- setDots(Number(inner.children[0].dataset.index));
- } catch (err) {
- console.log("%c ERROR ", "background-color:red;color:white;", err);
- }
- setTimeout(function (e, w) {
- e.style.transition = 'none';
- e.prepend(e.lastElementChild);
- e.style.left = `-${w}px`;
- }, 200, inner, carouselWidth);
- clearInterval(autoInterval);
- autoInterval = setInterval(nextImage, 5000);
- });
- right.addEventListener('click', function () {
- nextImage();
- clearInterval(autoInterval);
- autoInterval = setInterval(nextImage, 5000);
- });
- carousel.append(left, right);
- if ("dots" in carousel.dataset && carousel.dataset.dots == "true") {
- // Навигационные точки
- // Размер одной точки 30x30px
- // Если ширина карусели меньше чем КоличествоКартинок * 30, тогда точки не показывать
- if (images.length * 30 < carouselWidth) {
- let dots = document.createElement('div');
- for (let i = 0; i < images.length; i++) {
- let dot = document.createElement('div');
- dot.classList.add('dot');
- dots.append(dot);
- }
- dots.classList.add('dots');
- dots.children[1].classList.add('active');
- carouselRoot.append(dots);
- }
- }
- }
- }();
- function setDots(index) {
- let activeDots = document.querySelectorAll(".dots > .dot.active");
- for (let ad of activeDots) {
- ad.classList.remove('active');
- }
- let dots = document.querySelector(".dots");
- dots.children[index].classList.add("active");
- }
|