-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
145 lines (115 loc) · 4.28 KB
/
Copy pathscript.js
File metadata and controls
145 lines (115 loc) · 4.28 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const message = ["Hi, I'm Janiya Richardson!"]
const speed = 50
let textPosition = 0;
function typewriter() {
document.querySelector("#welcome").innerHTML = message[0].substring(0, textPosition) + '<span>\u25AE</span>'
if(textPosition++ != message[0].length)
setTimeout(typewriter, speed)
}
window.addEventListener("load", typewriter)
function toggleMenu() {
const navLinks = document.getElementById("navLinks");
navLinks.classList.toggle("active");
}
document.addEventListener("DOMContentLoaded", function() {
const carousel = document.querySelector(".carousel");
const arrowBtns = document.querySelectorAll(".wrapper i");
const wrapper = document.querySelector(".wrapper");
const firstCard = carousel.querySelector(".card");
const firstCardWidth = firstCard.offsetWidth;
let isDragging = false,
startX,
startScrollLeft,
timeoutId;
const dragStart = (e) => {
isDragging = true;
carousel.classList.add("dragging");
startX = e.pageX;
startScrollLeft = carousel.scrollLeft;
};
const dragging = (e) => {
if (!isDragging) return;
// Calculate the new scroll position
const newScrollLeft = startScrollLeft - (e.pageX - startX);
// Check if the new scroll position exceeds
// the carousel boundaries
if (newScrollLeft <= 0 || newScrollLeft >=
carousel.scrollWidth - carousel.offsetWidth) {
// If so, prevent further dragging
isDragging = false;
return;
}
// Otherwise, update the scroll position of the carousel
carousel.scrollLeft = newScrollLeft;
};
const dragStop = () => {
isDragging = false;
carousel.classList.remove("dragging");
};
const autoPlay = () => {
// Return if window is smaller than 800
if (window.innerWidth < 800) return;
// Calculate the total width of all cards
const totalCardWidth = carousel.scrollWidth;
// Calculate the maximum scroll position
const maxScrollLeft = totalCardWidth - carousel.offsetWidth;
// If the carousel is at the end, stop autoplay
if (carousel.scrollLeft >= maxScrollLeft) return;
// Autoplay the carousel after every 2500ms
timeoutId = setTimeout(() =>
carousel.scrollLeft += firstCardWidth, 2500);
};
carousel.addEventListener("mousedown", dragStart);
carousel.addEventListener("mousemove", dragging);
document.addEventListener("mouseup", dragStop);
wrapper.addEventListener("mouseenter", () =>
clearTimeout(timeoutId));
wrapper.addEventListener("mouseleave", autoPlay);
// Add event listeners for the arrow buttons to
// scroll the carousel left and right
arrowBtns.forEach(btn => {
btn.addEventListener("click", () => {
carousel.scrollLeft += btn.id === "left" ?
-firstCardWidth : firstCardWidth;
});
});
});
let slideIndex = 0;
// Function to show slides
function showSlides() {
let i;
const slides = document.getElementsByClassName("mySlides");
const dots = document.getElementsByClassName("dot");
// Hide all slides
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
// Remove the "active" class from all dots
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
// Increment the slide index
slideIndex++;
if (slideIndex > slides.length) {
slideIndex = 1; // Wrap around to the first slide
}
// Show the current slide and highlight the corresponding dot
slides[slideIndex - 1].style.display = "block";
dots[slideIndex - 1].className += " active";
// Automatically move to the next slide after 20 seconds
setTimeout(showSlides, 20000);
}
// Function to manually navigate to the next/previous slide
function plusSlides(n) {
slideIndex += n - 1; // Adjust the index for manual navigation
showSlides();
}
// Function to manually navigate to a specific slide
function currentSlide(n) {
slideIndex = n - 1; // Adjust the index for manual navigation
showSlides();
}
// Start the slideshow when the page loads
window.onload = function () {
showSlides();
};