Skip to content

Commit fca9e17

Browse files
committed
added fade out feature
1 parent 548c6ad commit fca9e17

File tree

4 files changed

+71
-18
lines changed

4 files changed

+71
-18
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ Want the animated blinking cursor? Add this CSS.
7878
}
7979
~~~
8080

81+
CSS when using the `fadeOut` option
82+
83+
~~~ scss
84+
.typed-fade-out{
85+
opacity: 0;
86+
animation: 0;
87+
transition: opacity .25s;
88+
}
89+
~~~
90+
8191
Wonderful sites using Typed.js
8292
---
8393
https://slack.com/
@@ -192,6 +202,10 @@ Typed.new(".element", {
192202
shuffle: false,
193203
// time before backspacing
194204
backDelay: 500,
205+
// Fade out instead of backspace (must use CSS class)
206+
fadeOut: false,
207+
fadeOutClass: 'typed-fade-out',
208+
fadeOutSpeed: 500, // milliseconds
195209
// loop
196210
loop: false,
197211
// null = infinite

dist/typed.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.htm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@
7070
50% { opacity:0; }
7171
100% { opacity:1; }
7272
}
73+
74+
.typed-fade-out{
75+
opacity: 0;
76+
animation: 0;
77+
transition: opacity .25s;
78+
}
7379
</style>
7480
</head>
7581
<body>

js/typed.js

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
// amount of time to wait before backspacing
6868
this.backDelay = this.options.backDelay;
6969

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+
7075
// div containing strings
7176
if($ && this.options.stringsElement instanceof $) {
7277
this.stringsElement = this.options.stringsElement[0]
@@ -154,6 +159,11 @@
154159
return;
155160
}
156161

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+
157167
// varying values for setTimeout during typing
158168
// can't be global since number changes each time loop is executed
159169
var humanize = Math.round(Math.random() * (100 - 30)) + this.typeSpeed;
@@ -266,15 +276,20 @@
266276
},
267277

268278
backspace: function(curString, curStrPos) {
279+
var self = this;
269280
// exit when stopped
270281
if (this.stop === true) {
271282
return;
272283
}
273284

285+
if (this.fadeOut){
286+
this.initFadeOut();
287+
return;
288+
}
289+
274290
// varying values for setTimeout during typing
275291
// can't be global since number changes each time loop is executed
276292
var humanize = Math.round(Math.random() * (100 - 30)) + this.backSpeed;
277-
var self = this;
278293

279294
self.timeout = setTimeout(function() {
280295

@@ -308,17 +323,7 @@
308323
// ----- continue important stuff ----- //
309324
// replace text with base text + typed characters
310325
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);
322327

323328
// if the number (id of character in current string) is
324329
// less than the stop number, keep going
@@ -348,11 +353,35 @@
348353
}, humanize);
349354

350355
},
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.
356385
shuffleArray: function(array) {
357386
var tmp, current, top = array.length;
358387
if(top) while(--top) {
@@ -439,6 +468,10 @@
439468
shuffle: false,
440469
// time before backspacing
441470
backDelay: 500,
471+
// Fade out instead of backspace
472+
fadeOut: false,
473+
fadeOutClass: 'typed-fade-out',
474+
fadeOutDelay: 500, // milliseconds
442475
// loop
443476
loop: false,
444477
// false = infinite

0 commit comments

Comments
 (0)