-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
59 lines (50 loc) · 1.63 KB
/
app.js
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
// for navigation bar
const menu = document.querySelector(".hamburger-menu");
const navLinks = document.querySelector(".nav-links");
menu.addEventListener("click",() => {
navLinks.classList.toggle("nav-links-show")
})
// for type-writing effect in landing page
const TypeWriter = function(txtElement,words,wait = 3000){
this.txtElement = txtElement;
this.words = words;
this.wordIndex = 0;
this.isDeleting = false;
this.txt = "";
this.wait = parseInt(wait,10);
this.type();
}
document.addEventListener("DOMContentLoaded",init);
// defining type method in constructor function
TypeWriter.prototype.type = function(){
const current = this.wordIndex % this.words.length;
const fullText = this.words[current];
if(this.isDeleting){
this.txt = fullText.substring(0,this.txt.length - 1);
}
else{
this.txt = fullText.substring(0,this.txt.length + 1);
}
this.txtElement.innerHTML = `<span class = "give-cursor">${this.txt}</span>`;
// for typeSpeed thing
let typeSpeed = 300;
if(this.isDeleting){
typeSpeed /= 2;
}
if(!this.isDeleting && this.txt === fullText){
typeSpeed = this.wait;
this.isDeleting = true;
}
else if(this.isDeleting && this.txt === ""){
typeSpeed = 500;
this.wordIndex++;
this.isDeleting = false;
}
setTimeout(() => this.type(),typeSpeed);
}
function init(){
const txtElement = document.querySelector(".text-type");
const words = JSON.parse(txtElement.getAttribute("data-words"));
const wait = txtElement.getAttribute("data-wait");
new TypeWriter(txtElement,words,wait)
}