-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
119 lines (97 loc) · 3.06 KB
/
script.js
File metadata and controls
119 lines (97 loc) · 3.06 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
const hamburger = document.querySelector("#hamburger");
const closeBtn = document.querySelector("#closebtn");
const navlinks = document.querySelector("#navlinks");
//open mobile nav
hamburger.addEventListener("click", () => {
navlinks.classList.remove("hidden");
hamburger.classList.add("hidden");
closeBtn.classList.remove("hidden");
});
//close mobile nav
closeBtn.addEventListener("click", () => {
navlinks.classList.add("hidden");
closeBtn.classList.add("hidden");
hamburger.classList.remove("hidden");
});
console.log("navbar added and close button added and hamburger button hidden");
const navItems = document.querySelectorAll(".nav-item");
navItems.forEach(item => {
item.addEventListener("click", () =>{
//set active state
navItems.forEach(el => {
el.classList.remove('scale:105', 'text-cyan-500');
});
item.classList.add('scale:105', 'text-cyan-500');
//close mobile nav if open
if(window.innerWidth < 768){ // close on small screen
navlinks.classList.add("hidden");
closeBtn.classList.add("hidden");
hamburger.classList.remove("hidden");
}
});
});
// Type writing effect
const phrases = [
"Frontend Developer",
"Web Developer",
"Coder",
"Future MERN Stack Developer"
];
const textElement = document.getElementById('text');
let phraseIndex = 0;
let charIndex = 0;
let isDeleting = false;
const typingSpeed = 100;
const pauseTime = 1000;
function type() {
const currentPhrase = phrases[phraseIndex];
if (isDeleting) {
charIndex--;
} else {
charIndex++;
}
textElement.innerHTML = currentPhrase.substring(0, charIndex);
if (!isDeleting && charIndex === currentPhrase.length) {
setTimeout(() => isDeleting = true, pauseTime);
} else if (isDeleting && charIndex === 0) {
isDeleting = false;
phraseIndex = (phraseIndex + 1) % phrases.length;
}
setTimeout(type, isDeleting ? typingSpeed / 2 : typingSpeed);
}
type();
const form = document.querySelector('form');
const inputs = form.querySelectorAll('input, textarea');
const btn = form.querySelector('button[type="submit"]');
function checkFormValidity() {
let allFilled = true;
inputs.forEach(input => {
if (!input.value.trim()) allFilled = false;
});
btn.disabled = !allFilled;
btn.classList.toggle('opacity-50', !allFilled);
btn.classList.toggle('cursor-not-allowed', !allFilled);
}
inputs.forEach(input => {
input.addEventListener('input', checkFormValidity);
});
checkFormValidity();
//scroll top button
const scrollTopBtn = document.getElementById("scroll-top-btn");
window.onscroll = function(){
scrollFunction();
};
function scrollFunction() {
if (document.body.scrollTop > 300 || document.documentElement.scrollTop > 300) {
scrollTopBtn.style.display = "block";
} else {
scrollTopBtn.style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
scrollTopBtn.addEventListener('click', ()=>{
window.scrollTo({
top: 0,
behaviour: 'smooth'
});
});