-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
104 lines (79 loc) · 2.24 KB
/
script.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
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
// mobile: when navbar item is clicked, will close
function navbarFunc() {
document.getElementById('off-navmenu').checked = false
}
// get the element
const text = document.querySelector('.typing-text');
// make a words array
const words = [
"algorithms?",
"data structures?",
"sample code?",
"we got em' all.",
":D"
];
// start typing effect
setTyper(text, words);
function setTyper(element, words) {
const LETTER_TYPE_DELAY = 70;
const WORD_STAY_DELAY = 1500;
const DIRECTION_FORWARDS = 0;
const DIRECTION_BACKWARDS = 1;
var direction = DIRECTION_FORWARDS;
var wordIndex = 0;
var letterIndex = 0;
var wordTypeInterval;
startTyping();
function startTyping() {
wordTypeInterval = setInterval(typeLetter, LETTER_TYPE_DELAY);
}
function typeLetter() {
const word = words[wordIndex];
if (direction == DIRECTION_FORWARDS) {
letterIndex++;
if (letterIndex == word.length) {
direction = DIRECTION_BACKWARDS;
clearInterval(wordTypeInterval);
setTimeout(startTyping, WORD_STAY_DELAY);
}
} else if (direction == DIRECTION_BACKWARDS) {
letterIndex--;
if (letterIndex == 0) {
nextWord();
}
}
const textToType = word.substring(0, letterIndex);
element.textContent = textToType;
}
function nextWord() {
letterIndex = 0;
direction = DIRECTION_FORWARDS;
wordIndex++;
if (wordIndex == words.length) {
wordIndex = 0;
}
}
}
setTyper(text, words);
var scrollToTopBtn = document.querySelector(".scrollToTopBtn")
var rootElement = document.documentElement
function handleScroll() {
// Do something on scroll
var scrollTotal = rootElement.scrollHeight - rootElement.clientHeight
if ((rootElement.scrollTop / scrollTotal ) > 0.40) {
// Show button
scrollToTopBtn.classList.add("showBtn")
} else {
// Hide button
scrollToTopBtn.classList.remove("showBtn")
}
}
function scrollToTop() {
// Scroll to top logic
rootElement.scrollTo({
top: 0,
behavior: "smooth"
})
}
scrollToTopBtn.addEventListener("click", scrollToTop)
document.addEventListener("scroll", handleScroll)