Skip to content

Commit fef55ef

Browse files
author
Greg Pfaff
committed
adding working carousel
1 parent 7023436 commit fef55ef

File tree

2 files changed

+63
-74
lines changed

2 files changed

+63
-74
lines changed

Carousel/index.js

Lines changed: 59 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,81 @@
1+
const cardCarousel = document.querySelector('.card_container');
12
const buttons = [].slice.call(document.querySelectorAll('.page'));
23
const cards = [].slice.call(document.querySelectorAll('.card'));
34

4-
let active = false;
5+
let count = 0;
6+
let previous;
57
let paused = false;
8+
let carouselInterval;
9+
10+
const onLoad = () => {
11+
buttons[0].classList.add('active');
12+
cards[0].classList.add('active');
13+
setTimeout(() => {
14+
console.log('👀 👀 👀 👀 👀');
15+
playAnimation();
16+
}, 100);
17+
}
618

7-
const rotateCards = () => {
8-
if (!paused) {
19+
function stopAnimation(cardCarousel) {
20+
console.log('⏱⏱⏱⏱⏱⏱⏱⏱');
21+
cardCarousel.classList.add('paused');
22+
return clearInterval(carouselInterval);
23+
}
924

10-
}
25+
function playAnimation() {
26+
console.log('🏃🏃🏃🏃🏃🏃🏃🏃🏃');
27+
clearInterval(carouselInterval);
28+
carouselInterval = setInterval(() => {
29+
autoLoop();
30+
}, 2500);
1131
}
1232

1333
const activeCards = (event) => {
14-
cards.forEach(card => {
15-
console.log(card.getAttribute('data-id'))
34+
cards.forEach((card) => {
1635
if (card.getAttribute('data-id') === event.getAttribute('data-id')) {
17-
card.style.zIndex = "1"
18-
card.classList.add('active');
19-
card.setAttribute('aria-selected', 'true');
20-
const random = Math.random() * 20 - 10;
21-
card.style.transform = `rotate(${random}deg) scale(1)`
36+
card.classList.add('active');
2237
} else {
23-
card.style.zIndex = "0"
24-
card.classList.remove('active');
25-
card.setAttribute('aria-selected', 'false');
38+
card.classList.remove('active');
2639
}
27-
})
28-
}
40+
});
41+
};
2942

3043
const activeButtons = (event) => {
31-
if (active == true) {
32-
paused = true;
33-
debugger;
34-
activeCards(event.target)
35-
buttons.forEach(button => {
36-
button.classList.remove('active')
37-
button.setAttribute('aria-selected', 'false');
38-
});
44+
paused = true;
45+
event.preventDefault();
46+
47+
if (paused) {
48+
stopAnimation(cardCarousel);
3949
}
40-
active = true;
41-
const activeButton = event.target;
4250

51+
buttons.forEach((button) => {
52+
button.classList.remove('active');
53+
button.setAttribute('aria-selected', 'false');
54+
});
55+
56+
const activeButton = event.target;
57+
activeCards(activeButton);
4358
activeButton.classList.add('active');
4459
activeButton.setAttribute('aria-selected', 'true');
60+
};
61+
62+
function autoLoop() {
63+
previous = count > 0 ? count - 1 : 0;
64+
buttons[previous].classList.toggle('active');
65+
cards[previous].classList.toggle('active');
66+
67+
if (count < buttons.length) {
68+
buttons[count].classList.add('active');
69+
cards[count].classList.add('active');
70+
count++;
71+
} else {
72+
count = 0;
73+
autoLoop();
74+
}
4575
}
4676

47-
buttons.forEach(button => button.addEventListener('click', activeButtons));
48-
77+
function startListening() { window.addEventListener('load', onLoad); }
4978

79+
buttons.forEach(button => button.addEventListener('click', activeButtons));
5080

51-
// var $carousel = $(".js-carousel");
52-
// var $carouselList = $(".js-carousel-list");
53-
// var $carouselItem = $(".js-carousel-item");
54-
55-
// var $btnNext = $(".js-carousel-btnNext");
56-
// var $btnPrev = $(".js-carousel-btnPrev");
57-
58-
// var $pagItem = $(".js-carousel-pagItem");
59-
// var $btnPag = $(".js-carousel-btnPag");
60-
61-
// var activeIndex = 0;
62-
// var maxIndex = $carouselItem.length - 1;
63-
64-
// var layout = function () {
65-
// $carouselItem.attr("aria-hidden", "true");
66-
// $carouselItem.eq(activeIndex).attr("aria-hidden", "false");
67-
68-
// $btnPag.attr("aria-selected", "false");
69-
// $btnPag.eq(activeIndex).attr("aria-selected", "true");
70-
// };
71-
72-
// var onNext = function () {
73-
// if (activeIndex !== maxIndex) {
74-
// activeIndex++;
75-
// layout();
76-
// }
77-
// };
78-
79-
// var onPrev = function () {
80-
// if (activeIndex !== 0) {
81-
// activeIndex--;
82-
// layout();
83-
// }
84-
// };
85-
86-
// var onPag = function (event) {
87-
// element = event.currentTarget;
88-
// activeIndex = $btnPag.index(element);
89-
// layout();
90-
// };
91-
92-
// $btnNext.on("click", onNext);
93-
// $btnPrev.on("click", onPrev);
94-
// $btnPag.on("click", onPag);
95-
96-
// layout();
81+
startListening();

Carousel/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ h1 {
2828
height: auto;
2929
transition-duration: 200ms;
3030
transition-timing-function: cubic-bezier(0, 0, 0, 0);
31+
z-index: 0;
32+
transform: scale(1);
3133
}
3234

3335
.card_pagination {
@@ -41,6 +43,8 @@ h1 {
4143
}
4244

4345
.card.active {
46+
z-index: 1;
47+
transform: scale(1.25);
4448
transition-duration: 200ms;
4549
transition-timing-function: cubic-bezier(.17, .67, .92, .3);
4650
}

0 commit comments

Comments
 (0)