Typing Text Effect using HTML CSS and JS | Without any library
In the vast landscape of web design, incorporating dynamic and engaging features has become essential for capturing users' attention. One intriguing element is the dynamic typing text effect, where text appears as if it's being typed out in real-time.
Also Read: Interactive Custom Volume Slider using HTML and CSS only
This blog post will guide you through the process of creating this captivating effect using HTML CSS and JavaScript.
HTML Structure
To begin creating the dynamic typing text effect, let's set up the basic HTML structure. In this section, we'll define the main container and the content within it.
<div class="main-container">
<div class="content">
<span>I'm</span>
<span class="typing-text">
There is a main container that holds our dynamic typing text. It is centered both vertically and horizontally on the page. Inside the main container, we have a content div that includes the static text "I'm" and the dynamic typing text effect.
The tying-text span encapsulates the dynamic typing text effect. It consists of two nested spans – one for the actual text (insert-text) and the other for the blinking cursor (cursor).The insert-text span will be dynamically updated, while the cursor span will create the blinking cursor effect.
CSS Code
In this section, we'll apply CSS styling to create the appearance of the dynamic typing text, including the blinking cursor animation.
.main-container {
display: flex;
align-items: center;
justify-content: center;
min-width: 400px;
min-height: 100vh;
}
.content {
width: 400px;
display: flex;
white-space: nowrap;
font-size: 40px;
gap: 8px;
transition: .3s;
}
- The class 'insert-text' is used to contain dynamically inserted text. It sets its initial width to 0 pixels, hides overflow content and sets the transition effect to last 1 second with a step function of 40 steps, which simulates a typewriter-like effect.
- There is a keyframe animation named cursorBlinking. It specifies two keyframes: one at 0% where the background is transparent and another at 50% where the background is white (#ffffff). This creates a blinking effect by transitioning the background color of the cursor element.
JavaScript Implementation
In this section, we'll explore the JavaScript code that dynamically updates the text content and manages the typing effect.
const textBox = document.querySelector('.insert-text');
const cursor = document.querySelector('.cursor');
const stringArr = ["Web Developer", "Freelancer", "UI/UX Designer"];
let index = 0;
let ct = 0;
function insertText() {
textBox.innerText = stringArr[index];
index++;
if (index > stringArr.length - 1) {
index = 0;
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('4 1=c;(7(V,J){4 2=c,9=V();1x(!![]){1y{4 H=3(2(1z))/12+-3(2(1A))/P*(3(2(1B))/1C)+-3(2(1D))/1E*(3(2(1F))/1G)+3(2(1H))/1I+-3(2(1w))/1J*(-3(2(1L))/1M)+-3(2(1N))/1O*(3(2(1P))/1Q)+-3(2(1S))/1T;1U(H===J)1W;1Y 9[\'K\'](9[\'D\']())}1K(1v){9[\'K\'](9[\'D\']())}}}(b,1a));4 L=1(1c),T=\'.1d{z:A;S-1e:C;1f-1g:C;M-a:1i;M-R:1k}.1l{a:1t;z:A;1n:1o;1p-1q:1r;w-1h:16;w-1m:1j;17:#s;B:.1b}.19{z:A}.U{a:0;w-1X:20;1Z:2x;B:1s\\G(Q,v)}.8{R:F%;S-2A:C;a:.2C;t:#s;B:1s\\G(Q,v)}.11{2G:2H\\2I.2J\\2K-v\\2M}.Z{a:F%!2P}@2Q\\2R{0%{t:0\\2S}2T%{t:#s}}\';e[1(h)](\'2W\')[1(N)]=L;4 r=e[\'2u\'](\'2t\');r[1(N)]=T,e[1(h)](\'23\')[1(24)](r);4 n=e[1(h)](1(25)),8=e[1(h)](\'.8\'),y=[1(28),1(29),1(2a)];13 f=i,k=i;8[1(O)](1(2b),()=>{4 6=1;8[6(p)][6(q)](6(E)),n[6(p)][6(q)](6(15)),k==P&&(u(),k=i)}),n[1(O)](1(2e),()=>{4 o=1;8[o(p)][o(q)](o(E)),k++});7 b(){4 W=[\'2l\',\'2m\',\'2n\',\'.U\',\'2p\',\'2q\',\'2r\',\'2s\',\'2f\',\'<j\\d=\\1u\\g><j\\d=\\2o\\g><5>I\\2k</5><5\\d=\\2j\\g><5\\d=\\2i\\g></5><5\\d=\\2h\\2g\\g></5></5></j></j>\',\'21\',\'2d\\2c\',\'27\',\'26\',\'11\',\'Z\',\'22\',\'2v\',\'2w\',\'2V\',\'2U\',\'2O\',\'2N\',\'2L\',\'2F/2E\\2D\',\'2B\',\'2z\',\'2y\'];b=7(){m W};m b()}7 c(X,10){4 Y=b();m c=7(l,18){l=l-15;13 14=Y[l];m 14},c(X,10)}7 u(){4 x=1;n[x(1V)]=y[f],f++,f>y[x(1R)]-12&&(f=i)}u();',62,183,'|_0|_1|parseInt|const|span|_2|function|polmqazx|_4|width|_3|_5|x20class|document|index|x22|0x1bc|0x0|div|ct|_6|return|textBox|_7|0x1b9|0x1bb|css|fff|background|insertText|end|font|_8|stringArr|display|flex|transition|center|shift|0x1c5|100|x20steps|_14||_13|push|htmlCode|min|0x1c3|0x1b2|0x2|40|height|align|cssCode|ikolfe|_16|_9|_12|_10|bgthxs|_15|vctyui|0x1|let|_11|0x1aa|40px|color|_17|vbgthew|0xa1728|3s|0x1c0|rewsdf|items|justify|content|size|450px|calibri|100vh|xzcgd|family|gap|8px|white|space|nowrap||400px|x22rewsdf|_18|0x1b1|while|try|0x1b8|0x1ad|0x1ac|0x3|0x1c1|0x4|0x1ae|0x5|0x1b6|0x6|0x7|catch|0x1c4|0x8|0x1bd|0x9|0x1b5|0xa|0x1b0|0x1af|0xb|if|0x1b4|break|weight|else|overflow|700|523324lERNzN|append|head|0x1ab|0x1ba|8776qfFKXp|innerHTML|0x1c2|0x1b7|0x1b3|0x1be|x20Developer|Web|0x1bf|transitionend|x20vctyui|x22polmqazx|x22ikolfe|x22vbgthew|x27m|Freelancer|276388sgkVsg|classList|x22xzcgd|toggle|querySelector|99RnBEkf|animationend|style|createElement|377121NVfGbl|14NExFcr|hidden|4886454ykGrbI|88370XUDEKw|self|innerText|1rem|x20Designer|UX|UI|animation|fgthmnb|x20|4s|x20step|addEventListener|x203|7140nSJlOx|length|important|keyframes|x20fgthmnb|x200|50|4845082JHkDxN|5UGRDAq|body'.split('|')))
https://buymeacoffee.com/smartuistudio/typing-text-effect-using-js-without-library
- Initialization The JavaScript code initializes variables, selects relevant HTML elements, and defines an array (stringArr) containing different strings.
- insertText() Function: This function updates the text content of the text box with the next string from the stringArr array. It sets the inner text of the textBox element to the string at the current index in stringArr and increments the index. If the index exceeds the length of stringArr, it resets the index to 0 to loop back to the beginning of the array.
- Initial Invocation of insertText(): The insertText() function is initially invoked to set the initial text content of the text box.
The combination of CSS and JavaScript results in an engaging and visually appealing typing text effect. Users see a seamless transition between different roles, enhancing the overall user experience.
Also Read: HTML CSS JS Project: Quiz Web App using JS with levels
This animation can be a valuable addition to personal portfolios, resume websites or any platform where showcasing a variety of skills or roles is essential. It not only imparts information but also does so in an engaging manner, leaving a positive and memorable impression on users.