-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole-writer.min.js
11 lines (11 loc) · 1.38 KB
/
console-writer.min.js
1
2
3
4
5
6
7
8
9
10
11
/*!
* console-writer - Creates console writing animation.
*
* [console-writer.js]
*
* Author: Liav Barsheshet, LBDevelopments <[email protected], [email protected]>
* Website: https://www.liavbarsheshet.com
* Copyright(c) 2020-2022 Liav Barsheshet <LBDevelopments>
* MIT Licensed
*/
function consoleWriter(element,options={},callback=(()=>{})){if("string"!=typeof element&&!(element instanceof Element))return;if("string"==typeof element&&(element=document.querySelector(element)),!element)return;options&&"object"==typeof options||(options={}),options.speed=options.speed>0?options.speed:500,options.delay=options.delay>=0?options.delay:0;const text=element.innerText;element.innerHTML="";const under_score=document.createElement("span");under_score.style.animation="console-writer-blink-animation 0.8s infinite",under_score.innerHTML="_";const command_start=document.createElement("span");command_start.innerHTML="> ",element.appendChild(command_start),element.appendChild(under_score);let started=!1,finished=!1;const start=()=>{started||(started=!0,setTimeout(()=>{for(let i=0;i<text.length;++i){const char=text[i],tmp=document.createElement("span");tmp.innerHTML=char,setTimeout(()=>{element.insertBefore(tmp,under_score),i===text.length-1&&callback()},options.speed*i)}},options.delay))},finish=()=>{started&&element.removeChild(under_score)};return{start:start,finish:finish}}