-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtyping-animation.js
68 lines (57 loc) · 2.14 KB
/
typing-animation.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
var TxtType = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtType.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
this.txt = fullTxt.substring(0, this.txt.length + 1);
this.el.innerHTML = '<span class="wrap">' + this.txt + '</span>';
var that = this;
var delta = 45 - Math.random() * 15;
if (this.isDeleting) {
delta /= 2;
}
if (!this.isDeleting && this.txt === fullTxt) {
if (this.loopNum < this.toRotate.length - 1) {
delta = this.period;
}
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload = function() {
var elements = document.getElementsByClassName('typewrite');
for (var i = 0; i < elements.length; i++) {
var toRotate = elements[i].getAttribute('data-type');
var period = elements[i].getAttribute('data-period');
if (toRotate) {
new TxtType(elements[i], JSON.parse(toRotate), period);
}
}
// INJECT CSS
var css = document.createElement('style');
css.type = 'text/css';
css.innerHTML = '.typewrite > .wrap { border-right: 0.08em solid #000 }';
document.body.appendChild(css);
// Determine the delay for the color transition based on your typing transition
var typingSpeed = 45; // Adjust this value based on your typing speed
var fullTxt = elements[elements.length - 1].getAttribute('data-type');
var colorTransitionDelay = fullTxt.length * typingSpeed;
setTimeout(function() {
document.body.style.transition = 'background-color 1s ease, color 1s ease';
document.body.style.backgroundColor = '#eef2e4'; // Replace with desired background color
document.body.style.color = 'black'; // Replace with desired text color
}, colorTransitionDelay);
};