Simulate a typewriter effect in vanilla JavaScript.
- Flexible API allowing granular control
- Option to repeat the sequence indefinitely
- Allows stopping and resuming the sequence on-the-fly
- 524 bytes gzipped
<div class="typewriter"></div>
const malarkey = require('malarkey')
const element = document.querySelector('.typewriter')
function callback (text) {
element.textContent = text
}
const options = {
typeSpeed: 50,
deleteSpeed: 50,
pauseDuration: 2000,
repeat: true
}
malarkey(callback, options)
.type('Say hello')
.pause()
.delete()
.type('Wave goodbye')
.pause()
.delete()
const malarkey = require('malarkey')
Initialise the typewriter effect with the given optional options
settings.
-
callback
is a function that is invoked for everytype
anddelete
event in the sequence. The function signature is(text)
, wheretext
is the string of characters that have been typed so far. -
options
is an object literal:Key Description Default typeSpeed
Duration in milliseconds to type
a single character50
deleteSpeed
Duration in milliseconds to delete
a single character50
pauseDuration
Duration in milliseconds to pause
2000
repeat
Whether to repeat the entire sequence indefinitely false
Type the given string
, one character at a time.
- Set
options.speed
to override the defaulttypeSpeed
.
Delete the specified number of characters, one character at a time.
characterCount
is a positive integer. If not specified, all characters previously typed will be deleted, one character at a time.- Set
options.speed
to override the defaultdeleteSpeed
.
Do nothing for some duration.
- Set
options.duration
to override the defaultpauseDuration
.
Immediately clear all characters that were typed.
Call the given fn
function.
- The function signature of
fn
is(callback, text)
. Invokecallback
to signal thatfn
has finished execution and that we can move on to the next event in the sequence.
Stops the sequence. Calls the given fn
function when the sequence has been stopped.
- The function signature of
fn
is(text)
.
Resume the sequence. Has no effect if the sequence is already running.
Returns true
if the sequence is currently stopped, else returns false
.
Install via yarn:
$ yarn add malarkey
Or npm:
$ npm install --save malarkey