-
Notifications
You must be signed in to change notification settings - Fork 2
/
typewriting.min.js
1 lines (1 loc) · 1011 Bytes
/
typewriting.min.js
1
class typeWriting{constructor(a){this.element=a,this.words=JSON.parse(a.getAttribute('data-words')),this.speed=parseInt(a.getAttribute('data-speed'),10)||100,this.delay=parseInt(a.getAttribute('data-delay'),10)||1e3,this.loop=a.getAttribute('data-loop'),this.char='',this.counter=0,this.isDeleting=!1,this.type()}type(){const a='yes'===this.loop?this.counter%this.words.length:this.counter,b=this.words[a];let c=this.speed;if(this.isDeleting?(c/=2,this.char=b.substring(0,this.char.length-1)):this.char=b.substring(0,this.char.length+1),this.element.innerHTML=`<span class="write">${this.char}</span><span class="blinking-cursor">|</span>`,!this.isDeleting&&this.char===b){if('no'===this.loop&&this.counter>=this.words.length-1)return;this.isDeleting=!0,c=this.delay}else this.isDeleting&&''===this.char&&(this.isDeleting=!1,this.counter++);setTimeout(()=>this.type(),c)}}document.addEventListener('DOMContentLoaded',init);function init(){document.querySelectorAll('.typewrite').forEach(a=>new typeWriting(a))}