Use jQuery to write a ChatGPT typing effect
<div class="textbox"><span id="content"></span><span id="cursor"></span></div>
.textbox { width:750px; margin:100px auto; padding:20px 30px; font-size:16px; color:# 333; white-space: pre-wrap; line-height:30px; border:2px solid #e8e8e8; box-sizing: border-box; background:# f8f8f8; } #cursor { width:0.2em; height:17px; margin:7px 0 0 0.1em; background-color:# 000; display: inline-block; vertical-align: top; animation:blink 0.8s infinite; } @keyframes blink { from, to { opacity:1; } 50% { opacity:0; } }
$(function() { Var text="On the night of October 12, the sixth year of Yuanfeng, when he was ready to undress and enter the house in the moonlight, he set off happily. Those who thought of nothing and were happy went to Chengtian Temple to find Zhang Huaimin. Huaimin also did not sleep, and walked with each other in the atrium. The courtyard was like clear water, algae in the water, not cross, and covered with bamboo and cypress shadows. What night was without moon? Where was there no bamboo and cypress? But few idle people were like the ears of the two of us. \N -- "Notes on Night Tour of Chengtian Temple" by Su Shi of Song Dynasty;//The text to be printed var delay = 50; //Typing speed, in milliseconds printText(text, delay); }); function printText(text, delay){ var chars = text.split(""); var i = 0; var intervalId = setInterval(function(){ if(i < chars.length){ var newText = $('#content').text() + chars[i]; $('#content').text(newText); i++; }else{ clearInterval(intervalId); $('#cursor').remove(); //Remove the cursor at the end of the text at the end of the content display. If you leave the cursor, delete or use//to comment on this line } }, delay); }