答案 0 :(得分:2)
使用<input placeholder=""
或setTimeout
为动画循环设置setInterval
属性。
一个更简单的版本只是使用setTimeout
而不是动画循环来阻止一堆未来的更新,因为我觉得它更简单 - 尽管这种方法无法扩展。
var finalPlaceholderText = "foo bar baz";
var input = document.getElementById( 'idOfTextBox' );
var placeholderIndex = 0;
for( var i = 0; i < finalPlaceholderText.length; i++ ) {
setTimeout(
function( length ) {
input.placeholder = finalPlaceholderText.substring( 0, i );
}
, i * 500, i );
}
请注意,您需要将length
(i
)作为参数传递到setTimeout
回调中,因为JavaScript的闭包将使用 last 值for
循环结束后,而不是每次调用setTimeout
时使用的值。
使用setTimeout
- 循环,它看起来像这样(注意缺少for
循环):
var finalPlaceholderText = "foo bar baz";
var input = document.getElementById( 'idOfTextBox' );
function incrementPlaceholderText( i ) {
input.placeholder = finalPlaceholderText.substring( 0, i );
if( i < finalPlaceholderText.length ) {
setTimeout( incrementPlaceholderText, 500, i + 1 );
}
}
incrementPlaceholderText( 0 );
或更一般地说(以支持多个输入元素和不同占位符文本的方式):
function incrementPlaceholderText( input, finalText, i ) {
input.placeholder = finalText.substring( 0, i );
if( i < finalText.length ) {
setTimeout( incrementPlaceholderText, 500, input, finalText, i + 1 );
}
}
incrementPlaceholderText( document.getElementById( 'idOfTextBox1' ), "foo bar baz", 0 );
incrementPlaceholderText( document.getElementById( 'idOfTextBox2' ), "foo bar baz qux", 0 );
答案 1 :(得分:1)
希望这段代码有用。添加了评论以澄清
//create a variable. This will be used to create substring
var initialChar = 0;
//get the element and placeholder
let getElement = document.getElementById('inputElem');
let getPlaceHolderText = getElement.getAttribute('placeholder');
//create IIFE and this will be called as long as placeholder is not
//completly created
(function setPlaceholder() {
//settime out function to input one text at a time
let clearNow = setTimeout(function() {
// increase the count
initialChar++;
//create a substring and set this value as placeholder
let getChar = getPlaceHolderText.substring(0, initialChar);
getElement.setAttribute('placeholder', getChar + '|')
// when the variable value and length of string
// is equal it mean all the placeholder text has been created
// if not equal then add next character to placeholder
if (initialChar !== getPlaceHolderText.length) {
// calling the IIFE
setPlaceholder()
} else {
// equal so remove the pipe(pipe to create cursor effect)
getElement.setAttribute('placeholder', getElement.getAttribute('placeholder').slice(0, -1));
clearTimeout(clearNow);
}
}, Math.ceil(Math.random() * 150)) // any random number
}())
<input type="text" placeholder="Add you text here" id="inputElem">
答案 2 :(得分:1)
您可以使用setInterval
功能:
var finalPlaceholderText = "foo bar bazaazz";
var input = document.getElementById('idOfTextBox');
var len = finalPlaceholderText.length;
var timerID;
var counter = 0;
if (counter <= len) {
timerID = setInterval(function() {
counter = counter + 1;
typewriter(counter)
}, 100);
}
function typewriter(i) {
input.placeholder = finalPlaceholderText.substring(0, i);
if (i === len) {
counter = 0;
//comment out below if you want it to stop
//clearInterval(timerID)
}
}
&#13;
<input id="idOfTextBox" placeholder="" />
&#13;
答案 3 :(得分:0)
在检查了消息来源后发现已经为此建立了一个很棒的库。