-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypewriter.js
50 lines (40 loc) · 1.31 KB
/
typewriter.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
(function($) {
var defaults = {
cursor: true,
callback: function() {},
typeSpeedInms: 100
}
var options = {}, elm;
var showCursor = function(){
var css = ".typewriter::after{animation: blink 1s infinite; content:'|'; color:black; display:inline-block}@keyframes blink{to{opacity: .0;}}",
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
$(elm).addClass("typewriter")
}
$.fn.typeWriter = function(customOptions) {
elm = this[0];
var text = elm.innerHTML;
var idx = 0;
elm.innerHTML = "";
$.extend(options, defaults, customOptions);
if(options.cursor){
showCursor();
}
var interval = setInterval(function() {
elm.innerHTML = elm.innerHTML + text.slice(idx, idx + 1);
idx += 1;
if (idx == text.length) {
clearInterval(interval);
options.callback();
}
}, options.typeSpeedInms)
return this;
}
}(jQuery));