简体   繁体   English

使用javascript / Jquery编写具有键入效果的文本

[英]Write the text with typing effect using javascript/Jquery

I will receive some content from server side.What I trying is to make the typing effect at the time of display this content. 我将从服务器端收到一些内容。我试图在显示此内容时使输入效果。

 $("#dislay").click(function() { //this is the dummy content i will recieve from server var contentFromServer = "Smile spoke total few great had never their too. Amongst moments do in arrived at my replied. Fat weddings servants but man believed prospect. Companions understood is as especially pianoforte connection introduced. Nay newspaper can sportsman are admitting gentleman belonging his. Is oppose no he summer lovers twenty in. Not his difficulty boisterous surrounded bed. Seems folly if in given scale. Sex contented dependent conveying advantage can use. Do play they miss give so up. Words to up style of since world. We leaf to snug on no need. Way own uncommonly travelling now acceptance bed compliment solicitude. Dissimilar admiration so terminated no in contrasted it. Advantages entreaties mr he apartments do. Limits far yet turned highly repair parish talked six. Draw fond rank form nor the day eat. In post mean shot ye. There out her child sir his lived. Design at uneasy me season of branch on praise esteem. Abilities discourse believing consisted remaining to no. Mistaken no me denoting dashwood as screened. Whence or esteem easily he on. Dissuade husbands at of no if disposal."; var typerText = ""; var contentLength = contentFromServer.length; var count = 0; var typingSpeed = 100000 / contentLength; var typer = setInterval(function() { if (count > contentFromServer.length) { clearInterval(typer); } typerText += contentFromServer.charAt(count); document.getElementById("dislayArea").innerHTML = "" + typerText + ""; count++; }, typingSpeed); //reset the interval on click of button $("#dislay").click(function() { clearInterval(typer); }); }); 
 div { border: 1px solid gray; padding: 8px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id="dislay" type="button">Display Content</button> <div id="dislayArea"></div> 

The question is I do not know if I'm using the correct way or not. 问题是我不知道我是否使用正确的方法。 That is, not sure if it would be better to use the for loop, or use setInterval(what I am using). 也就是说,不确定使用for循环还是使用setInterval(我使用的是什么)会更好。 Or there is any better approach to do this. 还是有更好的方法可以做到这一点。

Using setInterval() is definitely better than loop statement , as using loop will block your JS execution and you would not be able to do something during the same time. 使用setInterval()绝对比使用loop statement更好,因为使用loop会阻塞您的JS执行,并且您将无法同时执行某些操作。 To avoid this you may use variable speed based on string length (as you have done) but IMO this will not give good visual experience. 为避免这种情况,您可以根据字符串长度使用可变速度(如您所做),但是IMO不能提供良好的视觉体验。


I will also suggest to take a look at typed.js library. 我也建议看一看typed.js库。 (There can be other libraries that achieve the same task, but I have experience with this library and it works great!) Using the library provides more flexible control over the task with various options and again why to reinvent the wheel ? (可能还有其他库可以完成相同的任务,但是我有使用该库的经验,并且效果很好!)使用该库可以通过各种选项对任务进行更灵活的控制,以及为什么还要重新发明轮子?

Here is an example snippet of typed.js : 这是typed.js的示例片段:

 var typed = null; $("#dislay").click(function() { if(typed != null) typed.destroy(); var contentFromServer = "Smile spoke total few great had never their too. Amongst moments do in arrived at my replied. Fat weddings servants but man believed prospect. Companions understood is as especially pianoforte connection introduced. Nay newspaper can sportsman are admitting gentleman belonging his. Is oppose no he summer lovers twenty in. Not his difficulty boisterous surrounded bed. Seems folly if in given scale. Sex contented dependent conveying advantage can use. Do play they miss give so up. Words to up style of since world. We leaf to snug on no need. Way own uncommonly travelling now acceptance bed compliment solicitude. Dissimilar admiration so terminated no in contrasted it. Advantages entreaties mr he apartments do. Limits far yet turned highly repair parish talked six. Draw fond rank form nor the day eat. In post mean shot ye. There out her child sir his lived. Design at uneasy me season of branch on praise esteem. Abilities discourse believing consisted remaining to no. Mistaken no me denoting dashwood as screened. Whence or esteem easily he on. Dissuade husbands at of no if disposal."; var typedOptions = { strings: [contentFromServer], typeSpeed: 60, showCursor: false }; typed = new Typed("#displayArea", typedOptions); }); 
 div { border: 1px solid gray; padding: 8px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/typed.js/2.0.8/typed.js"></script> <button id="dislay" type="button">Display Content</button> <div id="displayArea"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM