Skip to content

Commit 9b77dd5

Browse files
authored
feat: version 1.0.0 commit
1 parent 457d587 commit 9b77dd5

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Typer
2+
3+
Typer is a tiny lightweight JavaScript plugin for adding nice, customizable Typer effect using custom HTML5 attributes. This plugin is written in native JavaScript ES6 and does not requires any dependencies!
4+
5+
if you have an issue/idea, please feel free to fork the repo or send me a push request and i'll be happy to push it.
6+
7+
## Usage
8+
9+
1. Load the minified version `typer.min.js` by placing the script tag before the closing Body tag for better page speed performance.
10+
```html
11+
<script src="js/typer.min.js"></script>
12+
```
13+
2. Create an empty `span` tag with class name `typewrite`. You're able to pass the options using the custom HTML5 attributes:
14+
15+
- **data-loop:** Stop the typing loop. Value shoud be `yes` or `no`
16+
- **data-speed:** Typing speed in millisecond. Default value is `100`
17+
- **data-delay:** Time delay after word is complete in millisecond. Default value is `1000`
18+
- **data-words:** Words to add separated by comma i.e. `["...","...","..."]`
19+
20+
```html
21+
<h3>Hey, I'm
22+
<span class="typewrite" data-loop="yes" data-speed="100" data-delay="1000" data-words='["word 1", "word 2", "word 3"]'></span>
23+
</h3>
24+
```
25+
## [Demo](https://birajrai.github.io/typer/)

typer.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @package birajrai/typer
3+
* @description Lightweight JavaScript plugin for adding nice, customizable typer effect using custom HTML5 attributes.
4+
* @author Awran5 <github.com/birajrai>
5+
* @version 1.0.0
6+
* @license under MIT https://github.com/birajrai/typer/blob/main/LICENSE
7+
* @see <github.com/birajrai/typer>
8+
*/
9+
10+
11+
class typeWriting {
12+
13+
constructor(element) {
14+
this.element = element; // Selector
15+
this.words = JSON.parse(element.getAttribute('data-words')); // Input words
16+
this.speed = parseInt(element.getAttribute('data-speed'), 10) || 100; // fallback 100 ms
17+
this.delay = parseInt(element.getAttribute('data-delay'), 10) || 1000; // fallback 1000 ms
18+
this.loop = element.getAttribute('data-loop');
19+
this.char = ''; // word letters
20+
this.counter = 0; // loop counter
21+
this.isDeleting = false; // check when deleting letters
22+
this.type(); // Typing method
23+
}
24+
25+
type() {
26+
// Set the words index.
27+
const index = this.loop === 'yes' ? this.counter % this.words.length : this.counter;
28+
// Get the full word
29+
const fullWord = this.words[index];
30+
// Typing speed
31+
let typeSpeed = this.speed;
32+
33+
if (this.isDeleting) {
34+
// Divide speed by 2
35+
typeSpeed /= 2;
36+
// Add chars
37+
this.char = fullWord.substring(0, this.char.length - 1);
38+
} else {
39+
// Delete chars
40+
this.char = fullWord.substring(0, this.char.length + 1);
41+
}
42+
// Display on DOM
43+
this.element.innerHTML = `<span class="write">${this.char}</span><span class="blinking-cursor">|</span>`;
44+
// When word is completed
45+
if (!this.isDeleting && this.char === fullWord) {
46+
// break the loop before deletion.
47+
if (this.loop === "no" && this.counter >= this.words.length - 1) {
48+
return;
49+
}
50+
// Set char delete to true
51+
this.isDeleting = true;
52+
// Set time delay before new word
53+
typeSpeed = this.delay;
54+
} else if (this.isDeleting && this.char === '') {
55+
this.isDeleting = false;
56+
// Move to next word
57+
this.counter++;
58+
}
59+
// Set time out
60+
setTimeout(() => this.type(), typeSpeed);
61+
62+
}
63+
64+
}
65+
66+
// Call the class on DOMContentLoaded
67+
document.addEventListener('DOMContentLoaded', init)
68+
// Select all elements and trigger the class
69+
function init() {
70+
document.querySelectorAll('.typewrite').forEach(e => new typeWriting(e));
71+
}

typer.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)