Built a slider for the Projects section
This commit is contained in:
1 parent
fbd3415c1e
commit
3a4b43ec88
4 files changed
+412
-68
No files matched your search
@@ -24,7 +24,7 @@ const testimonials = [
|
||||
},
|
||||
{
|
||||
text: "Cosmically Creative built my website exactly how I envisioned it—clean, functional, and professional. They were easy to work with, quick to respond, and clearly cared about getting every detail right. The end result speaks for itself. From our first conversation, they took the time to understand my business and what I needed the site to accomplish. I never felt rushed or out of the loop—updates came regularly, and my feedback was always taken seriously. I'd recommend them to anyone looking for a web developer who actually listens.",
|
||||
name: "‐ Ten8Locker",
|
||||
name: "‐ Valor Design Labs",
|
||||
},
|
||||
];
|
||||
|
||||
@@ -80,6 +80,146 @@ document
|
||||
showSlide(currentSlide + 1);
|
||||
});
|
||||
|
||||
// Project Carousel
|
||||
const projectTrack = document.querySelector(".project-track");
|
||||
const projectCards = Array.from(
|
||||
projectTrack.querySelectorAll(".project-slide"),
|
||||
);
|
||||
let currentProject = 0;
|
||||
let isSliding = false;
|
||||
|
||||
const slideClasses = [
|
||||
"slide-active",
|
||||
"slide-prev",
|
||||
"slide-next",
|
||||
"slide-hidden-left",
|
||||
"slide-hidden-right",
|
||||
];
|
||||
|
||||
function getPositions(index, direction) {
|
||||
const total = projectCards.length;
|
||||
const prev = (index - 1 + total) % total;
|
||||
const next = (index + 1) % total;
|
||||
|
||||
const positions = new Map();
|
||||
positions.set(index, "slide-active");
|
||||
positions.set(prev, "slide-prev");
|
||||
positions.set(next, "slide-next");
|
||||
|
||||
for (let i = 0; i < total; i++) {
|
||||
if (!positions.has(i)) {
|
||||
if (direction === "next") {
|
||||
positions.set(i, "slide-hidden-left");
|
||||
} else if (direction === "prev") {
|
||||
positions.set(i, "slide-hidden-right");
|
||||
} else {
|
||||
positions.set(i, "slide-hidden-left");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return positions;
|
||||
}
|
||||
|
||||
function applyPositions(positions) {
|
||||
projectCards.forEach((card, i) => {
|
||||
card.classList.remove(...slideClasses);
|
||||
card.classList.add(positions.get(i));
|
||||
});
|
||||
}
|
||||
|
||||
function slideToProject(newIndex, direction) {
|
||||
if (isSliding || newIndex === currentProject) return;
|
||||
isSliding = true;
|
||||
|
||||
const total = projectCards.length;
|
||||
const oldPrev = (currentProject - 1 + total) % total;
|
||||
const oldNext = (currentProject + 1) % total;
|
||||
const newPrev = (newIndex - 1 + total) % total;
|
||||
const newNext = (newIndex + 1) % total;
|
||||
|
||||
// First: instantly move the incoming card to its off-screen start position
|
||||
// (no transition) so it can slide in from the correct side
|
||||
const incoming =
|
||||
direction === "next" ? projectCards[newNext] : projectCards[newPrev];
|
||||
|
||||
if (incoming) {
|
||||
incoming.style.transition = "none";
|
||||
incoming.classList.remove(...slideClasses);
|
||||
incoming.classList.add(
|
||||
direction === "next" ? "slide-hidden-right" : "slide-hidden-left",
|
||||
);
|
||||
// Force reflow so the position applies before the transition kicks in
|
||||
incoming.offsetHeight;
|
||||
incoming.style.transition = "";
|
||||
}
|
||||
|
||||
currentProject = newIndex;
|
||||
const positions = getPositions(newIndex, direction);
|
||||
applyPositions(positions);
|
||||
|
||||
// Unlock after transition finishes
|
||||
const activeCard = projectCards[newIndex];
|
||||
const onEnd = () => {
|
||||
isSliding = false;
|
||||
activeCard.removeEventListener("transitionend", onEnd);
|
||||
};
|
||||
activeCard.addEventListener("transitionend", onEnd);
|
||||
|
||||
// Safety fallback
|
||||
setTimeout(() => {
|
||||
isSliding = false;
|
||||
}, 700);
|
||||
}
|
||||
|
||||
// Initial state
|
||||
applyPositions(getPositions(0, null));
|
||||
|
||||
document.querySelector(".project-btn--prev").addEventListener("click", () => {
|
||||
const total = projectCards.length;
|
||||
const newIndex = (currentProject - 1 + total) % total;
|
||||
slideToProject(newIndex, "prev");
|
||||
});
|
||||
|
||||
document.querySelector(".project-btn--next").addEventListener("click", () => {
|
||||
const total = projectCards.length;
|
||||
const newIndex = (currentProject + 1) % total;
|
||||
slideToProject(newIndex, "next");
|
||||
});
|
||||
|
||||
// Project Carousel Swipe Support
|
||||
let touchStartX = 0;
|
||||
let touchEndX = 0;
|
||||
const swipeThreshold = 50;
|
||||
|
||||
projectTrack.addEventListener(
|
||||
"touchstart",
|
||||
(e) => {
|
||||
touchStartX = e.changedTouches[0].clientX;
|
||||
},
|
||||
{ passive: true },
|
||||
);
|
||||
|
||||
projectTrack.addEventListener(
|
||||
"touchend",
|
||||
(e) => {
|
||||
touchEndX = e.changedTouches[0].clientX;
|
||||
const diff = touchStartX - touchEndX;
|
||||
|
||||
if (Math.abs(diff) > swipeThreshold) {
|
||||
const total = projectCards.length;
|
||||
if (diff > 0) {
|
||||
const newIndex = (currentProject + 1) % total;
|
||||
slideToProject(newIndex, "next");
|
||||
} else {
|
||||
const newIndex = (currentProject - 1 + total) % total;
|
||||
slideToProject(newIndex, "prev");
|
||||
}
|
||||
}
|
||||
},
|
||||
{ passive: true },
|
||||
);
|
||||
|
||||
// Scroll Animations
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const observerOptions = { root: null, rootMargin: "0px", threshold: 0.5 };
|
||||
|
||||
Reference in new issue
Block a user