-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
91 lines (83 loc) · 2.46 KB
/
script.js
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
88
89
90
91
document.addEventListener("DOMContentLoaded", function () {
const menuToggle = document.querySelector(".menu-toggle");
const navList = document.querySelector("nav ul");
const Herolis = document.querySelector(".hero");
const topmen = document.querySelector(".top-menu");
menuToggle.addEventListener("click", function () {
navList.classList.toggle("show");
Herolis.classList.toggle("hide");
topmen.classList.toggle("top-men");
});
});
// JavaScript for infinite sliding the cards
const slider = document.getElementById("cardSlider");
let currentIndex = 0;
function showCard(index) {
const cardWidth = document.querySelector(".card1").offsetWidth + 20; // Consider margin-right
const newPosition = -index * cardWidth;
slider.style.transform = `translateX(${newPosition}px)`;
}
function cloneAndAppend() {
const firstCard = document.querySelector(".card1");
const clone = firstCard.cloneNode(true);
document.querySelector(".slider-wrapper").appendChild(clone);
}
// Clone the first card and append it
cloneAndAppend();
// Example: Auto slide every 3 seconds
function autoSlide() {
currentIndex++;
const cardCount = document.querySelectorAll(".card1").length;
if (currentIndex >= cardCount - 3) {
currentIndex = 0;
// Reset the position to the first card without animation
slider.style.transition = "none";
slider.style.transform = "translateX(0)";
setTimeout(() => {
// Enable transition after resetting position
slider.style.transition = "";
}, 0);
}
showCard(currentIndex);
}
function nextSlide() {
currentIndex++;
const cardCount = document.querySelectorAll(".card1").length;
if (currentIndex >= cardCount - 3) {
currentIndex = 0;
}
showCard(currentIndex);
}
function prevSlide() {
currentIndex--;
if (currentIndex < 0) {
const cardCount = document.querySelectorAll(".card1").length;
currentIndex = cardCount - 4;
}
showCard(currentIndex);
}
function touchMoveHandler(event) {
touchEndX = event.touches[0].clientX;
}
function touchEndHandler() {
const touchDelta = touchEndX - touchStartX;
if (Math.abs(touchDelta) > 50) {
if (touchDelta > 0) {
prevSlide();
} else {
nextSlide();
}
}
}
cardSlider.addEventListener("touchmove", function (event) {
// Your code here
const touchDelta = touchEndX - touchStartX;
if (Math.abs(touchDelta) > 50) {
if (touchDelta > 0) {
prevSlide();
} else {
nextSlide();
}
}
});
setInterval(autoSlide, 3000);