File size: 2,476 Bytes
036b3a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const container = document.getElementById("carousel-container");
const elements = container.querySelectorAll(".testimonial-card");
const endicators = document.querySelectorAll(".endicator");

const options = {
  root: container,
  rootMargin: "0px",
  threshold: 1,
};

// From specs, there's only ever gonna be one card fully visible at a time
let visibleCardNum = -1;
const observer = new IntersectionObserver((entries) => {
  entries.forEach((entry) => {
    if (entry.isIntersecting) {
      visibleCardNum = Number(entry.target.id.split("-")[2]);
    }
  });
}, options);

const boundsObserver = new IntersectionObserver((entries) => {
  entries.forEach((entry) => {
    const bound = entry.target.dataset.bound;
    if (entry.isIntersecting) {
      if (bound === "left") {
        document.getElementById("slideLeft").disabled = true;
      }

      if (bound === "right") {
        document.getElementById("slideRight").disabled = true;
      }
    } else {
      if (bound === "left") {
        document.getElementById("slideLeft").disabled = false;
      }

      if (bound === "right") {
        document.getElementById("slideRight").disabled = false;
      }
    }
  });
}, options);

endicators.forEach((element) => boundsObserver.observe(element));

elements.forEach((element) => {
  observer.observe(element);
});

function updateButtonState() {
  if (container.scrollLeft <= 0) {
    document.getElementById("slideLeft").disabled = true;
  } else {
    document.getElementById("slideLeft").disabled = false;
  }

  if (container.scrollLeft >= container.scrollWidth - container.clientWidth) {
    document.getElementById("slideRight").disabled = true;
  } else {
    document.getElementById("slideRight").disabled = false;
  }
}

document.getElementById("slideRight").addEventListener("click", function () {
  const scrollTo = document.getElementById(
    `testimonial-card-${visibleCardNum + 1}`
  );

  if (scrollTo) {
    const x = container.scrollLeft + scrollTo.offsetLeft - container.offsetLeft;
    container.scrollBy({ left: x, behavior: "smooth" });
    // updateButtonState();
  }
});

document.getElementById("slideLeft").addEventListener("click", function () {
  const scrollTo = document.getElementById(
    `testimonial-card-${visibleCardNum - 1}`
  );

  if (scrollTo) {
    const x = container.scrollLeft - scrollTo.offsetLeft + container.offsetLeft;
    container.scrollBy({ left: -x, behavior: "smooth" });
    // updateButtonState();
  }
});