|
67 | 67 | // amount of time to wait before backspacing
|
68 | 68 | this.backDelay = this.options.backDelay;
|
69 | 69 |
|
| 70 | + // Fade out instead of backspace |
| 71 | + this.fadeOut = this.options.fadeOut; |
| 72 | + this.fadeOutClass = this.options.fadeOutClass; |
| 73 | + this.fadeOutDelay = this.options.fadeOutDelay; |
| 74 | + |
70 | 75 | // div containing strings
|
71 | 76 | if($ && this.options.stringsElement instanceof $) {
|
72 | 77 | this.stringsElement = this.options.stringsElement[0]
|
|
154 | 159 | return;
|
155 | 160 | }
|
156 | 161 |
|
| 162 | + if (this.fadeOut && this.el.classList.contains(this.fadeOutClass)) { |
| 163 | + this.el.classList.remove(this.fadeOutClass); |
| 164 | + this.cursor.classList.remove(this.fadeOutClass); |
| 165 | + } |
| 166 | + |
157 | 167 | // varying values for setTimeout during typing
|
158 | 168 | // can't be global since number changes each time loop is executed
|
159 | 169 | var humanize = Math.round(Math.random() * (100 - 30)) + this.typeSpeed;
|
|
266 | 276 | },
|
267 | 277 |
|
268 | 278 | backspace: function(curString, curStrPos) {
|
| 279 | + var self = this; |
269 | 280 | // exit when stopped
|
270 | 281 | if (this.stop === true) {
|
271 | 282 | return;
|
272 | 283 | }
|
273 | 284 |
|
| 285 | + if (this.fadeOut){ |
| 286 | + this.initFadeOut(); |
| 287 | + return; |
| 288 | + } |
| 289 | + |
274 | 290 | // varying values for setTimeout during typing
|
275 | 291 | // can't be global since number changes each time loop is executed
|
276 | 292 | var humanize = Math.round(Math.random() * (100 - 30)) + this.backSpeed;
|
277 |
| - var self = this; |
278 | 293 |
|
279 | 294 | self.timeout = setTimeout(function() {
|
280 | 295 |
|
|
308 | 323 | // ----- continue important stuff ----- //
|
309 | 324 | // replace text with base text + typed characters
|
310 | 325 | var nextString = curString.substr(0, curStrPos);
|
311 |
| - if (self.attr) { |
312 |
| - self.el.setAttribute(self.attr, nextString); |
313 |
| - } else { |
314 |
| - if (self.isInput) { |
315 |
| - self.el.value = nextString; |
316 |
| - } else if (self.contentType === 'html') { |
317 |
| - self.el.innerHTML = nextString; |
318 |
| - } else { |
319 |
| - self.el.textContent = nextString; |
320 |
| - } |
321 |
| - } |
| 326 | + self.replaceText(nextString); |
322 | 327 |
|
323 | 328 | // if the number (id of character in current string) is
|
324 | 329 | // less than the stop number, keep going
|
|
348 | 353 | }, humanize);
|
349 | 354 |
|
350 | 355 | },
|
351 |
| - /** |
352 |
| - * Shuffles the numbers in the given array. |
353 |
| - * @param {Array} array |
354 |
| - * @returns {Array} |
355 |
| - */ |
| 356 | + |
| 357 | + // Adds a CSS class to fade out current string |
| 358 | + initFadeOut: function(){ |
| 359 | + self = this; |
| 360 | + this.el.className += ' ' + this.fadeOutClass; |
| 361 | + this.cursor.className += ' ' + this.fadeOutClass; |
| 362 | + return setTimeout(function() { |
| 363 | + self.arrayPos++; |
| 364 | + self.replaceText('') |
| 365 | + self.typewrite(self.strings[self.sequence[self.arrayPos]], 0); |
| 366 | + }, self.fadeOutDelay); |
| 367 | + }, |
| 368 | + |
| 369 | + // Replaces current text in the HTML element |
| 370 | + replaceText: function(str) { |
| 371 | + if (this.attr) { |
| 372 | + this.el.setAttribute(this.attr, str); |
| 373 | + } else { |
| 374 | + if (this.isInput) { |
| 375 | + this.el.value = str; |
| 376 | + } else if (this.contentType === 'html') { |
| 377 | + this.el.innerHTML = str; |
| 378 | + } else { |
| 379 | + this.el.textContent = str; |
| 380 | + } |
| 381 | + } |
| 382 | + }, |
| 383 | + |
| 384 | + // Shuffles the numbers in the given array. |
356 | 385 | shuffleArray: function(array) {
|
357 | 386 | var tmp, current, top = array.length;
|
358 | 387 | if(top) while(--top) {
|
|
439 | 468 | shuffle: false,
|
440 | 469 | // time before backspacing
|
441 | 470 | backDelay: 500,
|
| 471 | + // Fade out instead of backspace |
| 472 | + fadeOut: false, |
| 473 | + fadeOutClass: 'typed-fade-out', |
| 474 | + fadeOutDelay: 500, // milliseconds |
442 | 475 | // loop
|
443 | 476 | loop: false,
|
444 | 477 | // false = infinite
|
|
0 commit comments