Lightweight custom typewriter
Demo is available here
Usage
Add it to your html:
or install as a dependency:
npm i lightweight-typewriteryarn add lightweight-typewriter
Import:
const TypeWriter = ;;
Create an element with the desired class name and create a class instance:
Done! Refresh the page and see the result :)
Customize
If you would pass text to the element, it will be displayed in front of the typewriter and margin-left: 6px
will be applied:
Front Text <!-- Above results in: --> Front TextTyping text <!-- To control margin-left use indent property -->
You can dynamically change all the properties with applyChanges()
method:
const typeWriter = new TypeWriter('myCoolTypeWriter', ).start(); // change speed and cursor after 1 second , 1000);
There are also available start()
and stop()
methods:
// create an instance const typeWriter = new TypeWriter('elemClass', ); // start typing when it's needed , 1000); // completely pause typing; can be resumed with start() typeWriter.stop() // stop typing and continue after 2 seconds typeWriter.stop(2000)
Options
option | type | required / default | example |
---|---|---|---|
elementClass |
string | yes | 'myCoolTypeWriter' |
text |
array | yes | ['out', 'in'] |
speed |
number (ms) | no / 1000 |
2000 |
pause |
number (ms) | no / 1500 |
2000 |
indent |
string | no / '0px' or '6px' |
'4px' |
cursorStyle |
object | no / { color: '#000', width: '2px' } | { color: 'dodgerblue', width: '5px' } |
elementClass
is the class name of target element
text
is an array of typing strings
speed
specifies the time needed to write a word
pause
specifies the delay before the word starts deleting
cursorStyle
is an optional object for styling typing cursor