Typewriter effect html+css+js

Effect (source code at the end):

Insert picture description here

achieve:

1. Define the label, the text will be written in the span tag, and the text will first be stored in the js array:

 <h1>
        <span class="text" ></span>
    </h1>

2. Define the basic style of text:

.text{
    
    
            font-family: 'fangsong';
            display: inline-block;
          position: relative;
          font-size: 40px;
          height: 60px;
          line-height: 60px;
          color: rgb(245, 245, 245);
        }

display: inline-block; inline block element
position: relative; relative positioning

3. Make a small cursor at the end of the text:

.text::after{
    
    
            content: '';
            position: absolute;
            right: -10px;
            top: 5px;
            height: 50px;
            width: 3px;     
            background-color: #fff;
            animation: san 0.5s steps(1) infinite;
        }
        @keyframes san{
    
    
            0%,100%{
    
    
                background-color: #fff;
            }
            50%{
    
    
               background-color: transparent;
            }

        }

position: absolute;
right: -10px;
top: 5px; absolute positioning
animation: san 0.5s steps(1) infinite; animation;
transparent transparent color

4. js part, to achieve the effect:

<script>
           <!-- 获取.text标签 -->
        const text = document.querySelector('.text');
         <!-- 定义数组,里面放上文本 -->
        const txt  =["北极光之夜。","夜越黑,星星越亮。","答案在风中飘荡。"]; 
        <!-- 定义当前要显示的字符串的第几个字符 -->
        var index=0;
        <!-- 定义文本数组的下标 -->
        var xiaBiao= 0;
        <!-- 定义huan,拿来判断是要实现打字还是删除字效果,初始为真 -->
        var huan = true;
     <!-- 定义一个定时器,200毫秒执行一次 -->
        setInterval(function(){
    
    
           <!-- huan为真 -->
            if(huan){
    
                 
                <!--.text标签添加字符,用.slice方法 --> 
                text.innerHTML = txt[xiaBiao].slice(0,++index);
            }
            else{
    
    
              <!--.text标签删除字符,用.slice方法 --> 
                text.innerHTML = txt[xiaBiao].slice(0,index--);
            }
            
            <!-- 判断当前index是否为当前字符串长度了+3了 ,+3是为了打完后多等会,多走3个啥也不做的轮回-->
            if(index==txt[xiaBiao].length+3)
            {
    
    
            <!-- huan变为假,开始执行删除文字效果 -->
                huan = false;
            }
            <!-- 如果删完了 -->
            else if(index<0)
            {
    
       
            <!-- index=0,huan为真,从头开始打字 -->
                index = 0;
                huan = true;
                <!-- 数组下标加1 -->
                xiaBiao++;
                if(xiaBiao>=txt.length)
                {
    
       
                <!-- 如果数组下标超过了,又回到0 -->
                    xiaBiao=0; 
                }
            }

        },200)

    </script

JavaScript slice() method

Complete source code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
     
     
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body{
     
     
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: rgb(43, 52, 82);
        }
        .text{
     
     
            font-family: 'fangsong';
            display: inline-block;
          position: relative;
          font-size: 40px;
          height: 60px;
          line-height: 60px;
          color: rgb(245, 245, 245);
        }
        .text::after{
     
     
            content: '';
            position: absolute;
            right: -10px;
            top: 5px;
            height: 50px;
            width: 3px;     
            background-color: #fff;
            animation: san 0.5s steps(1) infinite;
        }
        @keyframes san{
     
     
            0%,100%{
     
     
                background-color: #fff;
            }
            50%{
     
     
               background-color: transparent;
            }

        }
    </style>
</head>
<body>
    <h1>
        <span class="text" ></span>
    </h1>
    <script>
        const text = document.querySelector('.text');
        const txt  =["北极光之夜。","夜越黑,星星越亮。","答案在风中飘荡。"]; 
        
        var index=0;
        var xiaBiao= 0;
        var huan = true;
     
        setInterval(function(){
     
     
           
            if(huan){
     
           
                text.innerHTML = txt[xiaBiao].slice(0,++index);
                console.log(index);
            }
            else{
     
     
                text.innerHTML = txt[xiaBiao].slice(0,index--);
                console.log(index);
            }

            if(index==txt[xiaBiao].length+3)
            {
     
     
                huan = false;
            }
            else if(index<0)
            {
     
     
                index = 0;
                huan = true;
                xiaBiao++;
                if(xiaBiao>=txt.length)
                {
     
     
                    xiaBiao=0; 
                }
            }

        },200)

    </script>
</body>
</html>

to sum up:

Do you know any new games that are fun? I want to play some new games.

Other articles:
Colorful streamer text html+css
bubble floating background special effect html+css
simple clock special effect html+css+js
cyberpunk style button html+css
imitating NetEase cloud official website carousel image html+css+js
water wave loading animation html+ css
navigation bar scrolling gradient effect html+css+js
book page turning html+css
3D stereo album html+css
neon drawing board effect html+css+js
remember some CSS attribute summary (1)
Sass summary notes
... etc.

Guess you like

Origin blog.csdn.net/luo1831251387/article/details/114632844