-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
188 lines (156 loc) · 5.42 KB
/
script.js
File metadata and controls
188 lines (156 loc) · 5.42 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Get all the panels on the page
const panels = document.querySelectorAll('.panel');
// Set the active panel based on scroll position
function setActivePanel() {
// Get the current scroll position
const scrollPosition = window.scrollY;
// Loop through each panel
panels.forEach((panel) => {
// Get the top and bottom positions of the panel
const panelTop = panel.offsetTop;
const panelBottom = panelTop + panel.offsetHeight;
// If the scroll position is within the panel, add the 'active' class
if (scrollPosition >= panelTop && scrollPosition < panelBottom) {
panel.classList.add('active');
// Get the first image element inside the active panel
const activeImage = panel.querySelector('img');
if (activeImage) {
// Create an Audio object and set its source to the src attribute of the image
const firstImage = panel.querySelector('img');
if (!firstImage.dataset.played) {
const sound = new Audio(firstImage.dataset.sound);
sound.play();
firstImage.dataset.played = true;
}
}
} else {
// If the scroll position is outside the panel, remove the 'active' class
panel.classList.remove('active');
}
});
}
// Function to animate scrolling between panels
function scrollToNextPanel(event) {
// Prevent the default behavior of clicking on an image
event.preventDefault();
// Find the current panel and the next panel
const currentPanel = event.currentTarget.closest('.panel');
const nextPanel = currentPanel.nextElementSibling;
// If there is a next panel, scroll to it
if (nextPanel) {
nextPanel.scrollIntoView({ behavior: 'smooth' });
}
}
// Loop through each panel and add a click event listener to its images
panels.forEach((panel) => {
const images = panel.querySelectorAll('img');
images.forEach((image) => {
image.addEventListener('click', scrollToNextPanel);
});
});
// Randomly change the color of the header text every second
const headerText = document.querySelector('.header');
const colors = ['#FF7F50', '#DC143C', '#00CED1', '#FFD700', '#8B008B'];
setInterval(() => {
const randomColor = colors[Math.floor(Math.random() * colors.length)];
headerText.style.color = randomColor;
}, 1000);
// Add a bouncy text effect to each word in the header
const words = headerText.innerText.split(' ');
headerText.innerHTML = words.map(word => `<span>${word}</span>`).join(' ');
const spans = document.querySelectorAll('.header span');
spans.forEach(span => {
span.addEventListener('mouseover', function(e) {
span.classList.add('animated', 'bounce');
});
span.addEventListener('animationend', function(e) {
span.classList.remove('animated', 'bounce');
});
});
// Remove the arrow when the user reaches the bottom of the page
$(document).ready(function() {
// Add a click event listener to the arrow
$('.arrow-container').click(function() {
// Animate the page scrolling down to the first panel
$('html, body').animate({
scrollTop: $('.panel1').offset().top
}, 1000);
});
// Check if the user has scrolled to the bottom of the page
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
// If at the bottom, hide the arrow
$('.arrow-container').fadeOut();
} else {
// If not at the bottom, show the arrow
$('.arrow-container').fadeIn();
}
});
});
// button to go to the top of the webpage
const scrollToTopButton = document.getElementById('scroll-to-top');
scrollToTopButton.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
setTimeout(() => {
location.reload();
}, 1000);
});
//displaying three images on one panel and controlling the display with arrow keys
panels.forEach(panel => {
let images = panel.querySelectorAll('img');
let activeIndex = 0;
images.forEach(image => {
image.classList.remove('active', 'prev', 'next');
let soundSrc = image.getAttribute('data-sound');
if (soundSrc) {
let audio = document.createElement('audio');
audio.src = soundSrc;
image.parentNode.insertBefore(audio, image.nextSibling);
}
});
images[activeIndex].classList.add('active');
function showImage(index) {
images[activeIndex].classList.remove('active');
images[index].classList.add('active');
if (index > activeIndex) {
images[activeIndex].classList.add('prev');
images[index].classList.remove('next');
} else {
images[activeIndex].classList.add('next');
images[index].classList.remove('prev');
}
activeIndex = index;
// Play sound
images.forEach((image, index) => {
let audio = image.nextSibling;
if (index === activeIndex && audio && audio.tagName === 'AUDIO') {
audio.currentTime = 0;
audio.play();
} else if (audio && audio.tagName === 'AUDIO') {
audio.pause();
audio.currentTime = 0;
}
});
}
document.addEventListener('keydown', event => {
if(panel.classList.contains('active')){
if (event.key === 'ArrowRight') {
let nextIndex = activeIndex + 1;
if (nextIndex >= images.length) {
}else{
showImage(nextIndex);
}
} else if (event.key === 'ArrowLeft') {
let prevIndex = activeIndex - 1;
if (prevIndex < 0) {
}else{
showImage(prevIndex);
}
}
}
});
});
window.addEventListener('scroll', setActivePanel);