gpt4 book ai didi

javascript - 动画占位符文本,使其键入多个短语

转载 作者:行者123 更新时间:2023-11-30 08:23:53 25 4
gpt4 key购买 nike

我正在使用来自 jsfiddle 的代码为我的搜索栏中的占位符文本设置动画。最终结果是它看起来像是在打字。现在它只输入一个短语,我想让它输入一个短语,然后用另一个短语替换,依此类推。我们将不胜感激。

// your custom placeholder goes here!
var ph = "Search Website e.g. \"Dancing Cats\"",
searchBar = $('#search'),
// placeholder loop counter
phCount = 0;

// function to return random number between
// with min/max range
function randDelay(min, max) {
return Math.floor(Math.random() * (max-min+1)+min);
}

// function to print placeholder text in a
// 'typing' effect
function printLetter(string, el) {
// split string into character seperated array
var arr = string.split(''),
input = el,
// store full placeholder
origString = string,
// get current placeholder value
curPlace = $(input).attr("placeholder"),
// append next letter to current placeholder
placeholder = curPlace + arr[phCount];

setTimeout(function(){
// print placeholder text
$(input).attr("placeholder", placeholder);
// increase loop count
phCount++;
// run loop until placeholder is fully printed
if (phCount < arr.length) {
printLetter(origString, input);
}
// use random speed to simulate
// 'human' typing
}, randDelay(50, 90));
}

// function to init animation
function placeholder() {
$(searchBar).attr("placeholder", "");
printLetter(ph, searchBar);
}

placeholder();
$('.submit').click(function(e){
phCount = 0;
e.preventDefault();
placeholder();
});

最佳答案

查看我的基于 ES6 Promise 的解决方案。希望这会有所帮助。

// Add something to given element placeholder
function addToPlaceholder(toAdd, el) {
el.attr('placeholder', el.attr('placeholder') + toAdd);
// Delay between symbols "typing"
return new Promise(resolve => setTimeout(resolve, 100));
}

// Cleare placeholder attribute in given element
function clearPlaceholder(el) {
el.attr("placeholder", "");
}

// Print one phrase
function printPhrase(phrase, el) {
return new Promise(resolve => {
// Clear placeholder before typing next phrase
clearPlaceholder(el);
let letters = phrase.split('');
// For each letter in phrase
letters.reduce(
(promise, letter, index) => promise.then(_ => {
// Resolve promise when all letters are typed
if (index === letters.length - 1) {
// Delay before start next phrase "typing"
setTimeout(resolve, 1000);
}
return addToPlaceholder(letter, el);
}),
Promise.resolve()
);
});
}

// Print given phrases to element
function printPhrases(phrases, el) {
// For each phrase
// wait for phrase to be typed
// before start typing next
phrases.reduce(
(promise, phrase) => promise.then(_ => printPhrase(phrase, el)),
Promise.resolve()
);
}

// Start typing
function run() {
let phrases = [
"Search Website e.g. \"Dancing Cats\"",
"Lorem ipsum dolor sit amet",
"Consectetur adipiscing elit",
"JS is so strange :)"
];

printPhrases(phrases, $('#search'));
}

run();
.searchbar {
width: 300px;
padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Bin</title>
</head>
<body>
<input type='text' id="search" class="searchbar" />
</body>
</html>

关于javascript - 动画占位符文本,使其键入多个短语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48815262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com