| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- document.addEventListener("DOMContentLoaded", () => {
- const testimonialsGrids = document.querySelectorAll('[data-testimonials]');
- testimonialsGrids.forEach((grid) => {
- let commentCards = grid.querySelectorAll('[data-comment]');
- const leftArrow = grid.querySelector('[data-nav-arrow="prev"]');
- const rightArrow = grid.querySelector('[data-nav-arrow="next"]');
-
- let autoInterval;
- let autoTimeoutReset;
- let allComments = [];
- const totalComments = commentCards.length;
- const AUTOPLAY_INTERVAL = 5000;
- const RESET_DELAY = 5000;
- function getCommentsData() {
- const comments = grid.querySelectorAll('[data-comment]');
- allComments = [];
- comments.forEach((comment) => {
- const clonedComment = comment.cloneNode(true);
- allComments.push(clonedComment);
- });
- return allComments;
- }
- function updateComments() {
- removeAllComments();
- allComments.forEach((commentData, index) => {
- if (index < 3) {
- addCommentToDOM(commentData);
- }
- if (index === (totalComments - 1)) {
- addCommentToDOM(commentData, 'start');
- }
- if (index === 3) {
- addCommentToDOM(commentData, 'end');
- }
- });
- commentCards = grid.querySelectorAll('[data-comment]');
- positionCommentsWithTranslate();
- }
- function addCommentToDOM(comment, position = 'end') {
- const card = comment.cloneNode(true);
- if (position === 'start') {
- grid.insertBefore(card, grid.firstChild);
- } else {
- grid.appendChild(card);
- }
- return card;
- }
- function positionCommentsWithTranslate(gap = 24) {
- const cardWidth = commentCards[0].offsetWidth;
- const offset = -(cardWidth + gap) * 2;
- commentCards.forEach((card, index) => {
- const translateX = offset + (cardWidth + gap) * index;
- card.style.position = 'absolute';
- card.style.visibility = 'visible';
- card.style.transform = `translateX(${translateX}px)`;
- });
- }
- window.addEventListener('resize', () => {
- positionCommentsWithTranslate();
- });
- function removeAllComments() {
- grid.innerHTML = '';
- }
- function removeCommentFromDOM(index) {
- if (index < 0 || index >= commentCards.length) {
- return false;
- }
- commentCards[index].remove();
- return true;
- }
- // Autoplay functions
- function startAutoPlay() {
- if (autoInterval) return;
-
- autoInterval = setInterval(() => {
- moveLeft();
- }, AUTOPLAY_INTERVAL);
- }
- function stopAutoPlay() {
- if (autoInterval) {
- clearInterval(autoInterval);
- autoInterval = null;
- }
- }
- function resetAutoPlayTimeout() {
- clearTimeout(autoTimeoutReset);
- stopAutoPlay();
-
- autoTimeoutReset = setTimeout(() => {
- startAutoPlay();
- }, RESET_DELAY);
- }
- function moveLeft() {
- removeCommentFromDOM(0);
- const reorganizeComments = [];
- for (let index = 0; index < totalComments - 1; index++) {
- reorganizeComments[index] = allComments[index + 1];
- }
- reorganizeComments[totalComments - 1] = allComments[0];
- allComments = reorganizeComments;
- const card = addCommentToDOM(allComments[3], 'end');
- commentCards = grid.querySelectorAll('[data-comment]');
- positionCommentsWithTranslate();
- card.style.visibility = 'hidden';
- setTimeout(() => {
- card.style.visibility = 'visible';
- }, 1000);
- }
- function moveRight() {
- removeCommentFromDOM(commentCards.length - 1);
- const reorganizeComments = [];
- reorganizeComments[0] = allComments[totalComments - 1];
- for (let index = 0; index < totalComments - 1; index++) {
- reorganizeComments[index + 1] = allComments[index];
- }
- allComments = reorganizeComments;
- const card = addCommentToDOM(allComments[0], 'start');
- commentCards = grid.querySelectorAll('[data-comment]');
- positionCommentsWithTranslate();
- card.style.visibility = 'hidden';
- setTimeout(() => {
- card.style.visibility = 'visible';
- }, 1000);
- }
- if (leftArrow) {
- leftArrow.addEventListener('click', () => {
- moveLeft();
- resetAutoPlayTimeout();
- });
- }
- if (rightArrow) {
- rightArrow.addEventListener('click', () => {
- moveRight();
- resetAutoPlayTimeout();
- });
- }
- // Pause autoplay on hover
- grid.addEventListener('mouseenter', () => {
- stopAutoPlay();
- });
- grid.addEventListener('mouseleave', () => {
- startAutoPlay();
- });
- function initial() {
- getCommentsData();
- updateComments();
- }
- window.addEventListener('load', () => {
- initial();
- positionCommentsWithTranslate();
-
- // Start autoplay after 1 second
- setTimeout(() => startAutoPlay(), 1000);
- });
- });
- });
|