-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
126 lines (100 loc) · 4.17 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const buttons = document.querySelectorAll(".card-buttons button");
const sections = document.querySelectorAll(".card-section");
const card = document.querySelector(".card");
const handleButtonClick = e => {
const targetSection = e.target.getAttribute("data-section");
const section = document.querySelector(targetSection);
targetSection !== "#about" ?
card.classList.add("is-active") :
card.classList.remove("is-active");
card.setAttribute("data-state", targetSection);
sections.forEach(s => s.classList.remove("is-active"));
buttons.forEach(b => b.classList.remove("is-active"));
e.target.classList.add("is-active");
section.classList.add("is-active");
};
buttons.forEach(btn => {
btn.addEventListener("click", handleButtonClick);
});
//code for sayings below
const resolver = {
resolve: function resolve(options, callback) {
// The string to resolve
const resolveString = options.resolveString || options.element.getAttribute('data-target-resolver');
const combinedOptions = Object.assign({}, options, { resolveString: resolveString });
function getRandomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
function randomCharacter(characters) {
return characters[getRandomInteger(0, characters.length - 1)];
};
function doRandomiserEffect(options, callback) {
const characters = options.characters;
const timeout = options.timeout;
const element = options.element;
const partialString = options.partialString;
let iterations = options.iterations;
setTimeout(() => {
if (iterations >= 0) {
const nextOptions = Object.assign({}, options, { iterations: iterations - 1 });
// Ensures partialString without the random character as the final state.
if (iterations === 0) {
element.textContent = partialString;
} else {
// Replaces the last character of partialString with a random character
element.textContent = partialString.substring(0, partialString.length - 1) + randomCharacter(characters);
}
doRandomiserEffect(nextOptions, callback);
} else if (typeof callback === "function") {
callback();
}
}, options.timeout);
};
function doResolverEffect(options, callback) {
const resolveString = options.resolveString;
const characters = options.characters;
const offset = options.offset;
const partialString = resolveString.substring(0, offset);
const combinedOptions = Object.assign({}, options, { partialString: partialString });
doRandomiserEffect(combinedOptions, () => {
const nextOptions = Object.assign({}, options, { offset: offset + 1 });
if (offset <= resolveString.length) {
doResolverEffect(nextOptions, callback);
} else if (typeof callback === "function") {
callback();
}
});
};
doResolverEffect(combinedOptions, callback);
} };
const strings = [
' "It always seems impossible until it is done. - Nelson Mandela ',
'“Wisdom is not a product of schooling but of the lifelong attempt to acquire it.” ― Albert Einstein',
' “Sometimes it is the people no one can imagine anything of who do the things no one can imagine.” ― Alan Turing',
'......'];
let counter = 0;
const options = {
// Initial position
offset: 0,
// Timeout between each random character
timeout: 5,
// Number of random characters to show
iterations: 0,
// Random characters to pick from
characters: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'x', '#', '%', '&', '-', '+', '_', '?', '/', '\\', '='],
// String to resolve
resolveString: strings[counter],
// The element
element: document.querySelector('[data-target-resolver]') };
// Callback function when resolve completes
function callback() {
setTimeout(() => {
counter++;
if (counter >= strings.length) {
counter = 0;
}
let nextOptions = Object.assign({}, options, { resolveString: strings[counter] });
resolver.resolve(nextOptions, callback);
}, 10000);
}
resolver.resolve(options, callback);