Skip to content

Commit

Permalink
timer
Browse files Browse the repository at this point in the history
  • Loading branch information
StefaBodnia committed Mar 11, 2024
1 parent 5dd1617 commit 6e44a12
Showing 1 changed file with 24 additions and 46 deletions.
70 changes: 24 additions & 46 deletions assets/js/slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,57 +194,36 @@ document.addEventListener("DOMContentLoaded", () => {

let currentIndex = 0;

const startAutoplay = () => {
autoplayInterval = setInterval(() => {
nextSlide();
}, intervalDuration);
};
const insertImages = () => {
images.forEach((image) => {
const img = document.createElement("img");
img.src = folder + image.name;
slider.appendChild(img);
img.style.height = image.size * 100 + "%";
});

const intervalDuration = 2000; // Set the interval duration in milliseconds (e.g., 5000ms = 5 seconds)
let autoplayInterval;

const stopAutoplay = () => {
clearInterval(autoplayInterval);
const startAutoplay = () => {
autoplayInterval = setInterval(() => {
nextSlide();
}, intervalDuration);
};

// Start autoplay when the document is ready
startAutoplay();
const stopAutoplay = () => {
clearInterval(autoplayInterval);
};

// Start autoplay when the document is ready
startAutoplay();

// Stop autoplay on mouseover and resume on mouseleave
sliderContainer.addEventListener("mouseover", stopAutoplay);
sliderContainer.addEventListener("mouseleave", () => {
if (!isTouchDevice()) {
startAutoplay();
}
});
sliderContainer.addEventListener("mouseover", stopAutoplay);
sliderContainer.addEventListener("mouseleave", startAutoplay);

};

// Touch event listeners for horizontal scrolling on mobile
let touchStartX = 0;
let touchEndX = 0;

sliderContainer.addEventListener("touchstart", (e) => {
touchStartX = e.touches[0].clientX;
stopAutoplay(); // Stop autoplay on touchstart
});

sliderContainer.addEventListener("touchmove", (e) => {
touchEndX = e.touches[0].clientX;
});

sliderContainer.addEventListener("touchend", () => {
const swipeThreshold = 50; // Adjust the threshold as needed
const swipeDistance = touchEndX - touchStartX;

if (swipeDistance > swipeThreshold) {
prevSlide();
} else if (swipeDistance < -swipeThreshold) {
nextSlide();
}

startAutoplay(); // Resume autoplay on touchend
});

// Function to check if the device is a touch device
function isTouchDevice() {
return "ontouchstart" in window || navigator.msMaxTouchPoints;
}


const updateSlider = () => {
Expand Down Expand Up @@ -312,5 +291,4 @@ document.addEventListener("DOMContentLoaded", () => {




});

0 comments on commit 6e44a12

Please sign in to comment.