Created the instance of swiper then implement it.
This commit is contained in:
parent
43d2c61505
commit
5bc0e0b265
53
scripts.js
53
scripts.js
|
@ -1,14 +1,27 @@
|
||||||
|
// Clone the logos-slide and append it
|
||||||
var copy = document.querySelector(".logos-slide").cloneNode(true);
|
var copy = document.querySelector(".logos-slide").cloneNode(true);
|
||||||
document.querySelector(".logos").appendChild(copy);
|
document.querySelector(".logos").appendChild(copy);
|
||||||
|
|
||||||
// Get all images, descriptions, pagination buttons
|
// Initialize Swiper instance
|
||||||
const images = document.querySelectorAll(".swiper-slide");
|
const swiper = new Swiper(".swiper-container", {
|
||||||
|
slidesPerView: 1,
|
||||||
|
spaceBetween: 10,
|
||||||
|
pagination: {
|
||||||
|
el: ".swiper-pagination",
|
||||||
|
clickable: true,
|
||||||
|
},
|
||||||
|
navigation: {
|
||||||
|
nextEl: ".swiper-button-next",
|
||||||
|
prevEl: ".swiper-button-prev",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get all descriptions and pagination buttons
|
||||||
const descriptions = document.querySelectorAll(".project-description");
|
const descriptions = document.querySelectorAll(".project-description");
|
||||||
const paginationButtons = document.querySelectorAll(".pagination-button");
|
const paginationButtons = document.querySelectorAll(".pagination-button");
|
||||||
|
const images = document.querySelectorAll(".swiper-slide img"); // Select all images within slides
|
||||||
|
|
||||||
let currentIndex = 0; // Start with the first slide
|
// Function to update the displayed content based on the current slide index
|
||||||
|
|
||||||
// Function to update the displayed content based on the current index
|
|
||||||
function updateContent(index) {
|
function updateContent(index) {
|
||||||
// Hide all descriptions
|
// Hide all descriptions
|
||||||
descriptions.forEach((desc) => {
|
descriptions.forEach((desc) => {
|
||||||
|
@ -25,24 +38,34 @@ function updateContent(index) {
|
||||||
paginationButtons.forEach((button) => {
|
paginationButtons.forEach((button) => {
|
||||||
button.classList.remove("active");
|
button.classList.remove("active");
|
||||||
});
|
});
|
||||||
paginationButtons[index].classList.add("active");
|
if (paginationButtons[index]) {
|
||||||
|
paginationButtons[index].classList.add("active");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move Swiper to the corresponding slide
|
||||||
|
swiper.slideTo(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Event listeners for image clicks
|
// Listen for Swiper slide change event
|
||||||
images.forEach((image, index) => {
|
swiper.on("slideChange", () => {
|
||||||
image.addEventListener("click", () => {
|
const currentIndex = swiper.realIndex;
|
||||||
currentIndex = index;
|
updateContent(currentIndex);
|
||||||
updateContent(currentIndex);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Event listeners for pagination buttons
|
// Event listeners for pagination buttons
|
||||||
paginationButtons.forEach((button, index) => {
|
paginationButtons.forEach((button, index) => {
|
||||||
button.addEventListener("click", () => {
|
button.addEventListener("click", () => {
|
||||||
currentIndex = index;
|
updateContent(index); // Update content and slide
|
||||||
updateContent(currentIndex);
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Event listeners for image clicks
|
||||||
|
images.forEach((image) => {
|
||||||
|
image.addEventListener("click", (event) => {
|
||||||
|
const index = parseInt(event.target.closest(".swiper-slide").getAttribute("data-index"), 10);
|
||||||
|
updateContent(index); // Update content and slide
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Initialize the content (first image and description are visible)
|
// Initialize the content (first image and description are visible)
|
||||||
updateContent(currentIndex);
|
updateContent(swiper.realIndex);
|
||||||
|
|
Loading…
Reference in a new issue