carousel.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. let carouselWidth, carousel;
  2. +function init() {
  3. let carouselRoot = document.createElement('div');
  4. carouselRoot.classList.add('root');
  5. carousel = document.querySelector("#Carousel");
  6. try {
  7. carouselWidth = Number(carousel.dataset.width);
  8. } catch (err) {
  9. console.log("%c ERROR ", "background-color:red;color:white;", err);
  10. carouselWidth = 600;
  11. }
  12. carousel.style.width = `${carouselWidth}px`;
  13. carousel.parentElement.append(carouselRoot);
  14. carouselRoot.append(carousel);
  15. if (carousel && carousel.children) {
  16. let inner = document.createElement('div'),
  17. index = 0;
  18. let images = carousel.querySelectorAll("[data-image]");
  19. for (let image of images) {
  20. let img = document.createElement("img");
  21. img.src = image.dataset.image;
  22. img.style.width = `${carouselWidth}px`;
  23. img.dataset.index = index++;
  24. inner.append(img);
  25. }
  26. inner.classList.add('inner', 'loaded');
  27. while (carousel.children.length > 0) {
  28. carousel.children[0].remove();
  29. }
  30. carousel.append(inner);
  31. let nextImage = function () {
  32. inner.style.transition = `left 200ms ease 0s`;
  33. inner.style.left = `-${carouselWidth * 2}px`;
  34. try {
  35. setDots(Number(inner.children[2].dataset.index));
  36. } catch (err) {
  37. console.log("%c ERROR ", "background-color:red;color:white;", err);
  38. }
  39. setTimeout(function (e, w) {
  40. e.style.transition = 'none';
  41. e.append(e.firstElementChild);
  42. e.style.left = `-${w}px`;
  43. }, 200, inner, carouselWidth);
  44. };
  45. let interval = setInterval(function () {
  46. if (inner.clientHeight != 0) {
  47. carousel.style.height = `${inner.clientHeight}px`;
  48. inner.style.position = 'absolute';
  49. inner.style.left = `-${carouselWidth}px`;
  50. inner.classList.remove('loaded');
  51. clearInterval(interval);
  52. }
  53. }, 100);
  54. let autoInterval = setInterval(nextImage, 5000);
  55. // Кнопки переключения
  56. let left = document.createElement('div'),
  57. right = document.createElement('div');
  58. left.classList.add('left');
  59. right.classList.add('right');
  60. left.addEventListener('click', function () {
  61. inner.style.transition = `left 200ms ease 0s`;
  62. inner.style.left = `0px`;
  63. try {
  64. setDots(Number(inner.children[0].dataset.index));
  65. } catch (err) {
  66. console.log("%c ERROR ", "background-color:red;color:white;", err);
  67. }
  68. setTimeout(function (e, w) {
  69. e.style.transition = 'none';
  70. e.prepend(e.lastElementChild);
  71. e.style.left = `-${w}px`;
  72. }, 200, inner, carouselWidth);
  73. clearInterval(autoInterval);
  74. autoInterval = setInterval(nextImage, 5000);
  75. });
  76. right.addEventListener('click', function () {
  77. nextImage();
  78. clearInterval(autoInterval);
  79. autoInterval = setInterval(nextImage, 5000);
  80. });
  81. carousel.append(left, right);
  82. if ("dots" in carousel.dataset && carousel.dataset.dots == "true") {
  83. // Навигационные точки
  84. // Размер одной точки 30x30px
  85. // Если ширина карусели меньше чем КоличествоКартинок * 30, тогда точки не показывать
  86. if (images.length * 30 < carouselWidth) {
  87. let dots = document.createElement('div');
  88. for (let i = 0; i < images.length; i++) {
  89. let dot = document.createElement('div');
  90. dot.classList.add('dot');
  91. dots.append(dot);
  92. }
  93. dots.classList.add('dots');
  94. dots.children[1].classList.add('active');
  95. carouselRoot.append(dots);
  96. }
  97. }
  98. }
  99. }();
  100. function setDots(index) {
  101. let activeDots = document.querySelectorAll(".dots > .dot.active");
  102. for (let ad of activeDots) {
  103. ad.classList.remove('active');
  104. }
  105. let dots = document.querySelector(".dots");
  106. dots.children[index].classList.add("active");
  107. }