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

A brief discussion on HTML ordered lists, unordered lists and definition lists

Ordered List XML/HTML CodeCopy content to clipboa...

Solution to the error when calling yum in docker container

When executing yum in dockerfile or in the contai...

Three methods of automatically completing commands in MySQL database

Note: The third method is only used in XSell and ...

Write a publish-subscribe model with JS

Table of contents 1. Scene introduction 2 Code Op...

jQuery realizes image highlighting

It is very common to highlight images on a page. ...

React event binding details

Table of contents Class component event binding F...

vue-cropper component realizes image cutting and uploading

This article shares the specific code of the vue-...

Detailed tutorial on installing Ubuntu 19.10 on Raspberry Pi 4

Because some dependencies of opencv could not be ...

Personal opinion: Talk about design

<br />Choose the most practical one to talk ...

How to import Tomcat source code into idea

Table of contents 1. Download the tomcat code 2. ...

img usemap attribute China map link

HTML img tag: defines an image to be introduced in...

Several ways to remove the dotted box that appears when clicking a link

Here are a few ways to remove it: Add the link dir...

Implementation of Docker CPU Limit

1. --cpu=<value> 1) Specify how much availa...