-
Notifications
You must be signed in to change notification settings - Fork 2
/
ityped.js
executable file
·132 lines (119 loc) · 4.53 KB
/
ityped.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
127
128
129
130
131
132
/**
* @name ityped
* @description Dead simple Animated Typing with no dependencies
* @author Luis Vinícius
* @email [email protected]
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.ityped = {})));
}(this, (function (exports) { 'use strict';
/**
* @name setProps
* @description Set the ityped properties configuration
* @param {Object} config The configuration properties
* @return {Promise}
*/
var setProps = function setProps(_ref) {
var _ref$strings = _ref.strings,
strings = _ref$strings === undefined ? ['Put your strings here...', 'and Enjoy!'] : _ref$strings,
_ref$typeSpeed = _ref.typeSpeed,
typeSpeed = _ref$typeSpeed === undefined ? 100 : _ref$typeSpeed,
_ref$backSpeed = _ref.backSpeed,
backSpeed = _ref$backSpeed === undefined ? 50 : _ref$backSpeed,
_ref$backDelay = _ref.backDelay,
backDelay = _ref$backDelay === undefined ? 500 : _ref$backDelay,
_ref$startDelay = _ref.startDelay,
startDelay = _ref$startDelay === undefined ? 500 : _ref$startDelay,
_ref$cursorChar = _ref.cursorChar,
cursorChar = _ref$cursorChar === undefined ? '|' : _ref$cursorChar,
_ref$placeholder = _ref.placeholder,
placeholder = _ref$placeholder === undefined ? false : _ref$placeholder,
_ref$showCursor = _ref.showCursor,
showCursor = _ref$showCursor === undefined ? true : _ref$showCursor,
_ref$disableBackTypin = _ref.disableBackTyping,
disableBackTyping = _ref$disableBackTypin === undefined ? false : _ref$disableBackTypin,
_ref$onFinished = _ref.onFinished,
onFinished = _ref$onFinished === undefined ? function () {} : _ref$onFinished,
_ref$loop = _ref.loop,
loop = _ref$loop === undefined ? true : _ref$loop;
return {
strings: strings,
typeSpeed: typeSpeed,
backSpeed: backSpeed,
cursorChar: cursorChar,
backDelay: backDelay,
placeholder: placeholder,
startDelay: startDelay,
showCursor: showCursor,
loop: loop,
disableBackTyping: disableBackTyping,
onFinished: onFinished
};
};
var init = function init(element, properties) {
var i = 0,
l = void 0,
STRINGS_TO_ITERATE = void 0;
var typewrite = function typewrite(strings, props) {
if (i === l) if (props.loop) i = 0;
setTimeout(function () {
typeString(strings[i], props);
}, props.startDelay);
};
var typeString = function typeString(str, props) {
var index = 0,
strLen = str.length;
var intervalID = setInterval(function () {
props.placeholder ? element.placeholder += str[index] : element.textContent += str[index];
if (++index === strLen) return onStringTyped(intervalID, props);
}, props.typeSpeed);
};
var onStringTyped = function onStringTyped(id, props) {
clearInterval(id);
if (props.disableBackTyping && i === l - 1) {
return props.onFinished();
}
if (!props.loop && i === l - 1) {
return props.onFinished();
}
setTimeout(function () {
return eraseString(props);
}, props.backDelay);
};
var eraseString = function eraseString(props) {
var str = props.placeholder ? element.placeholder : element.textContent,
strLen = str.length;
var intervalID = setInterval(function () {
props.placeholder ? element.placeholder = element.placeholder.substr(0, --strLen) : element.textContent = str.substr(0, --strLen);
if (strLen === 0) return onStringErased(intervalID, props);
}, props.backSpeed);
};
var onStringErased = function onStringErased(id, props) {
clearInterval(id);
++i;
typewrite(STRINGS_TO_ITERATE, props);
};
var setCursor = function setCursor(element, props) {
var cursorSpan = document.createElement('span');
cursorSpan.classList.add('ityped-cursor');
cursorSpan.textContent = '|';
cursorSpan.textContent = props.cursorChar;
element.insertAdjacentElement('afterend', cursorSpan);
};
var startTyping = function startTyping(prop) {
var props = setProps(prop || {});
var strings = props.strings;
STRINGS_TO_ITERATE = strings;
l = strings.length;
if (typeof element === "string") element = document.querySelector(element);
if (props.showCursor) setCursor(element, props);
typewrite(strings, props);
};
return startTyping(properties);
};
exports.init = init;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=index.js.map