-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
140 lines (118 loc) · 4.33 KB
/
scripts.js
File metadata and controls
140 lines (118 loc) · 4.33 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
/*=============== SHOW MENU ===============*/
const navMenu = document.getElementById('nav-menu'),
navToggle = document.getElementById('nav-toggle'),
navClose = document.getElementById('nav-close');
/*===== MENU SHOW =====*/
// Validate if constant exists
if (navToggle) {
navToggle.addEventListener('click', (e) => {
e.stopPropagation(); // Prevent this click from triggering the document click
navMenu.classList.toggle('show-menu');
// When opening the menu, make sure the hamburger animation stays in sync
if (navMenu.classList.contains('show-menu')) {
if (navToggle.querySelector('.ham1') && !navToggle.querySelector('.ham1').classList.contains('active')) {
navToggle.querySelector('.ham1').classList.add('active');
}
} else {
if (navToggle.querySelector('.ham1') && navToggle.querySelector('.ham1').classList.contains('active')) {
navToggle.querySelector('.ham1').classList.remove('active');
}
}
});
}
/*===== MENU HIDDEN =====*/
// Validate if constant exists
if (navClose) {
navClose.addEventListener('click', () => {
navMenu.classList.remove('show-menu');
// Update hamburger icon state
if (navToggle.querySelector('.ham1') && navToggle.querySelector('.ham1').classList.contains('active')) {
navToggle.querySelector('.ham1').classList.remove('active');
}
});
}
/*===== ACTIVE AND REMOVE MENU =====*/
const navLink = document.querySelectorAll('.nav-link');
function linkAction() {
/*Active link*/
navLink.forEach((n) => n.classList.remove('active'));
this.classList.add('active');
/*Remove menu mobile*/
const navMenu = document.getElementById('nav-menu');
navMenu.classList.remove('show-menu');
// Update hamburger icon state
if (navToggle.querySelector('.ham1') && navToggle.querySelector('.ham1').classList.contains('active')) {
navToggle.querySelector('.ham1').classList.remove('active');
}
}
navLink.forEach((n) => n.addEventListener('click', linkAction));
// Prevent menu from closing when clicking inside the menu
if (navMenu) {
navMenu.addEventListener('click', (e) => {
e.stopPropagation();
});
}
// Close the menu when clicking outside
document.addEventListener('click', (event) => {
if (navMenu && navMenu.classList.contains('show-menu')) {
navMenu.classList.remove('show-menu');
// Update hamburger icon state
if (navToggle.querySelector('.ham1') && navToggle.querySelector('.ham1').classList.contains('active')) {
navToggle.querySelector('.ham1').classList.remove('active');
}
}
});
/*===== CONTACT-TEXTAREA =====*/
let textArea = document.getElementById('textbox');
let characterCounter = document.getElementById('char_count');
const maxNumOfChars = 250; // Updated from 100 to 250
const countCharacters = () => {
if (!textArea) return; // Guard clause if element doesn't exist
let numOfEnteredChars = textArea.value.length;
let counter = maxNumOfChars - numOfEnteredChars;
if (characterCounter) {
characterCounter.textContent = counter + '/250'; // Updated from /100 to /250
if (counter < 0) {
characterCounter.style.color = 'var(--ninth-color)';
} else if (counter < 50) { // Updated from 20 to 50
characterCounter.style.color = 'var(--fifteenth-color)';
} else {
characterCounter.style.color = 'var(--second-color)';
}
}
};
if (textArea) {
textArea.addEventListener('input', countCharacters);
// Set maxlength attribute
textArea.setAttribute('maxlength', maxNumOfChars);
// Initialize counter on page load
countCharacters();
}
/*===== BACK TO TOP BUTTON =====*/
// Create the back to top button element
const createBackToTopButton = () => {
const button = document.createElement('button');
button.innerHTML = '<i class="uil uil-arrow-up"></i>';
button.className = 'back-to-top-btn';
button.id = 'backToTopBtn';
button.title = 'Back to top';
document.body.appendChild(button);
return button;
};
// Initialize the button
const backToTopBtn = createBackToTopButton();
// Show/hide the button based on scroll position
window.addEventListener('scroll', () => {
if (window.scrollY > 300) {
backToTopBtn.classList.add('show');
} else {
backToTopBtn.classList.remove('show');
}
});
// Scroll to top when the button is clicked
backToTopBtn.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});