简体   繁体   English

闲置时间过后如何添加随机输入?

[英]How to add random input after inactivity time?

I'm making a webpage and I want to make "type" effect on form. 我正在制作一个网页,并且希望在表单上产生“文字”效果。 After user waits for a while, it starts typing some text as an example, if possible - with cursor. 用户稍等片刻后,如果可能的话,它将开始输入一些文本作为示例-使用光标。 I don't really know where to start though. 我真的不知道从哪里开始。 I'm using jQuery. 我正在使用jQuery。

Thanks in advance! 提前致谢!

Animate the Placeholder 动画占位符

You might use the placeholder attribute to display example input. 您可以使用占位符属性来显示示例输入。 Static text is usually enough for most users. 对于大多数用户而言,静态文本通常就足够了。 Yet, you could add an animation loop to simulate typing. 但是,您可以添加一个动画循环来模拟打字。 Using the placeholder also allows you to display text without altering the form input value. 使用占位符还可以在不更改表单输入值的情况下显示文本。

The example below uses a loop to simulate typing and stops with the user enters information. 下面的示例使用循环来模拟键入,并在用户输入信息时停止。 No jQuery required. 不需要jQuery。

Run the snippet to demo 运行代码片段进行演示

 var timerId, placeholder = fullname.placeholder, n =0; id = setInterval(function() { if (fullname.value) { // stop as user has typed something clearInterval(id); fullname.placeholder = placeholder; } else { // show next character fullname.placeholder = placeholder.substr(0,n); n = (n+1) % (placeholder.length+1); } }, 400); 
 input {font-family: 'Special Elite', cursive; font-size:36px; border:1px solid steelblue;color:black; font-weight:bold;} 
 <link href='https://fonts.googleapis.com/css?family=Special+Elite' rel='stylesheet' type='text/css'> <input id="fullname" placeholder="Berlin, Germany"> 

You can register a timeout function at form load using settimeout function. 您可以使用settimeout函数在表单加载时注册一个超时函数。

If user presses a key you can cancel it. 如果用户按下某个键,则可以将其取消。

The timeout function callback could set the placeholder of your input using jQuery. 超时函数回调可以使用jQuery设置输入的占位符。

Consider the inserted snippet to have an idea on how to start a proper implementation. 考虑插入的代码段,以了解如何启动适当的实现。

 (function() { "use strict"; var exampleText = "my example text"; var inactivityDelay = 2000; var typeDelay = 200; var interval; var appendExample = function() { var index = 0; interval = setInterval(function() { if (index >= exampleText.length) { clearInterval(interval); return; } var myinput = $("#myinput"); myinput.attr("placeholder", myinput.attr("placeholder") + exampleText.charAt(index++)); }, typeDelay); }; var timeout = setTimeout(appendExample, inactivityDelay); $("#myinput").on("change paste keyup focus", function() { clearTimeout(timeout); clearInterval(interval); $("#myinput").attr("placeholder", ""); }); }()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input type="text" id="myinput" maxlength="100" placeholder="" /> </form> 

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

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