-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
152 lines (129 loc) · 4.91 KB
/
script.js
File metadata and controls
152 lines (129 loc) · 4.91 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
// Display Mobile Menu
const menu = document.querySelector('#mobile-menu');
const menuLinks = document.querySelector('.navbar-menu');
const mobileMenu = () => {
menu.classList.toggle('is-active');
menuLinks.classList.toggle('active');
}
menu.addEventListener('click', mobileMenu);
/*** Form Handling ***
Purpose:
- When the user submits the form, the name and state they
entered should be added to the list of participants.
***/
// Step 1: Add your query for the submit RSVP button here
let connectButton = document.getElementById("connect-button");
let count = 3;
const addParticipant = (event) => {
// Step 2: Write your code to manipulate the DOM here
event.preventDefault();
const nameInput = document.getElementById("name").value;
const emailInput = document.getElementById("email").value;
const homeStateInput = document.getElementById("home-state").value;
const entry = document.createElement("p");
entry.textContent= "✨ " + nameInput + " from " + homeStateInput + " wants to learn more!"
const connectParticipants = document.querySelector(".connect-participants");
connectParticipants.appendChild(entry);
document.getElementById("name").value = "";
document.getElementById("email").value = "";
document.getElementById("home-state").value = "";
const countElement = document.getElementById("connect-count");
countElement.remove();
count = count + 1;
const countEntry = document.createElement("p");
countEntry.id = 'connect-count';
countEntry.textContent= "🌆 " + count + " people have joined!";
connectParticipants.appendChild(countEntry);
}
// Step 3: Add a click event listener to the submit RSVP button here
connectButton.addEventListener("click", addParticipant);
// Light Mode Toggle
document.addEventListener('DOMContentLoaded', function() {
// Create light switch button
const lightSwitch = document.createElement('button');
lightSwitch.id = 'light-switch';
lightSwitch.innerHTML = '💡';
document.body.appendChild(lightSwitch);
// Create stars
createStars();
// Handle light switch
lightSwitch.addEventListener('click', function() {
document.body.classList.toggle('light-mode');
if (document.body.classList.contains('light-mode')) {
lightSwitch.innerHTML = '🌙 ';
} else {
lightSwitch.innerHTML = '💡';
}
});
});
// Function to create stars in the background
function createStars() {
const numStars = 200;
for (let i = 0; i < numStars; i++) {
let star = document.createElement('div');
star.className = 'star';
// Random position
let x = Math.random() * 100;
let y = Math.random() * 100;
// Random size
let size = Math.random() * 2;
// Random animation delay
let delay = Math.random() * 5;
star.style.left = x + 'vw';
star.style.top = y + 'vh';
star.style.width = size + 'px';
star.style.height = size + 'px';
star.style.animation = `twinkle 3s infinite ${delay}s`;
document.body.appendChild(star);
}
}
// Toggle Switch
const toggleSwitch = document.getElementById('toggle-switch');
const image = document.querySelector('.light-pollution-img');
toggleSwitch.addEventListener('change', function() {
if (toggleSwitch.checked) {
image.src = "img/light-polluted-sky1.jpg";
} else {
image.src = 'img/sky1.png';
}
});
/*** Scroll Animations ***
Purpose:
- Use this starter code to add scroll animations to your website.
***/
// Step 1: Select all elements with the class 'revealable'.
let revealableContainers = document.querySelectorAll('.revealable');
// Step 2: Write function to reveal elements when they are in view.
const reveal = () => {
for (let i = 0; i < revealableContainers.length; i++) {
let current = revealableContainers[i];
// Get current height of container and window
let windowHeight = window.innerHeight;
let topOfRevealableContainer = revealableContainers[i].getBoundingClientRect().top;
let revealDistance = parseInt(getComputedStyle(current).getPropertyValue('--reveal-distance'), 10);
// If the container is within range, add the 'active' class to reveal
if (topOfRevealableContainer < windowHeight - revealDistance) {
current.classList.add('active');
}
// If the container is not within range, hide it by removing the 'active' class
else {
current.classList.remove('active');
}
}
}
// Step 3: Whenever the user scrolls, check if any containers should be revealed
window.addEventListener('scroll', reveal);
document.addEventListener("DOMContentLoaded", function () {
const details = document.querySelectorAll("details");
details.forEach((detail) => {
detail.addEventListener("toggle", function () {
if (this.open) {
details.forEach((other) => {
if (other !== this) {
other.removeAttribute("open");
}
});
}
});
});
});