Pure js to achieve typewriter effect

Pure js to achieve typewriter effect

This article example shares the specific code of js to achieve the typewriter effect for your reference. The specific content is as follows

Rendering

Application Scenario

It's not very useful. I just saw a similar effect on the Internet and wrote 40 or 50 lines of code that I couldn't understand, so I tried to see if it could be easily implemented.

html

<h2 id="text-box"></h2>
<!--One line is also code-->
CSS

        h2 {
   width: 800px;
   line-height: 40px;
   border-bottom: 1px solid;
   margin: 200px auto;
   font-size: 24px;
  }
  
  .animate {
   display: inline-block;
   padding: 0 5px;
   vertical-align: 3px;
   font-size: 20px;
   font-weight: normal;
  }
  
  .animate.on {
   animation: fade 1.5s infinite forwards;
  }
  
  @keyframes fade {
   from {
    opacity: 0;
   }
   to {
    opacity: 1;
   }
  }

js

let textBox = $('#text-box');
let index = 0;
let str = 'Welcome to my website!';
 
  let len ​​= str.length;
 
  function input() {
 
   textBox.html(str.substr(0, index) + '<span class="animate">|</span>');
 
   setTimeout(function() {
    index++;
 
    if(index === len + 1) {
     $('.animate').addClass('on');
     return;
    }
 
    input();
 
   }, Math.random() * 600)
 
   console.log(index);
  }
 
  input();

Implementation principle

The timer is combined with string interception to achieve a typewriter-like sense of frustration, and the index is accumulated through recursion. This is equivalent to intercepting one byte at the first second, intercepting two bytes at the second second, and so on... The timer takes a random number to better simulate the pause feeling of typing. Add an end loop condition to the recursive call and start the animation before the end to simulate the cursor jumping.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • A complete example of typewriter effect implemented by JS
  • JavaScript game development: "Romance of the Three Kingdoms: The Legend of Cao Cao" component development (Part 3) Typewriter-like text output in scenario dialogue
  • JavaScript textarea typewriter effect prompt code recommendation
  • js typewriter effect code

<<:  Detailed explanation of the simple use of MySQL query cache

>>:  Detailed explanation of Docker Secret management and use

Recommend

Example of how nginx implements dynamic and static separation

Table of contents Deploy nginx on server1 Deploy ...

How to use file writing to debug a Linux application

In Linux, everything is a file, so the Android sy...

Mysql | Detailed explanation of fuzzy query using wildcards (like,%,_)

Wildcard categories: %Percent wildcard: indicates...

Detailed tutorial on MySql installation and uninstallation

This article shares the tutorial of MySql install...

Summary of Operator Operations That Are Very Error-Prone in JavaScript

Table of contents Arithmetic operators Abnormal s...

Install zip and unzip command functions under Linux and CentOS (server)

Install zip decompression function under Linux Th...

HTML weight loss Streamline HTML tags to create web pages

HTML 4 HTML (not XHTML), MIME type is text/html, ...

Tic-Tac-toe game implemented in pure CSS3

Operation effect: html <div class="tic-ta...

JavaScript Timer Details

Table of contents 1. Brief Introduction 2. setInt...

How to install JDK 13 in Linux environment using compressed package

What is JDK? Well, if you don't know this que...

How to set the width attribute to the style of the span tag

If you directly set the width attribute to the sty...

Detailed explanation of JavaScript closure issues

Closures are one of the traditional features of p...

Introduction to HTML link anchor tags and their role in SEO

The <a> tag is mainly used to define links ...