javascript - 如何使用 Javascript 中的 forEach() 一次将一个字母连续添加到跨标签
问题描述
我有三个跨度标签,我需要为每个标签添加一些文本,以使其看起来像是被输入到屏幕上。我正在使用 forEach() 循环遍历每个跨度标记并运行一个函数,该函数应该将每个字母一次一个地添加到每个跨度标记中。
但是,forEach 将每个字母同时添加到所有三个跨度标签中。一旦其他两个都添加了所有字母,我只会在最后一个跨度标签上获得所需的“打字”效果。
如何调整 forEach 以使其看起来像是在屏幕上输入文本?请注意,出于样式目的,文本必须位于单独的跨度标签中。
let delay = 500;
let currentChar = 1;
let idArr = ["firstSpan", "secondSpan", "thirdSpan"];
let textArr = ["heading ", ": ", "'My Book Title'"];
idArr.forEach((item, index) => {
let text = textArr[index];
function type() {
document.getElementById(item).innerHTML = text.substr(0, currentChar);
currentChar++;
if (currentChar > text.length) {
console.log('text length check running')
currentChar = 1
} else {
setTimeout(type, delay);
}
}
type();
})
.cursor {
color: black;
animation: 1s blink step-end infinite;
}
@keyframes blink {
from, to {
color: transparent;
}
50% {
color: black;
}
}
<span>
<span id="firstSpan"></span>
<span id="secondSpan"></span>
<span id="thirdSpan"></span>
</span>
<span class="cursor">|</span>
解决方案
我无法抗拒...
const delay = async(ms) => new Promise(r => setTimeout(r, ms))
, spanN = document.querySelectorAll('#sp3 span')
, txtArr = ['heading ', ': ', 'My Book Title' ]
;
async function* LettersSpanGen(aT)
{
let SpLt = aT.reduce((r,c,sp)=>[...r,...[...c].map(lt=>({sp,lt}))],[])
yield* SpLt
}
(async function()
{
for await (let {sp,lt} of LettersSpanGen( txtArr ))
{
spanN[sp].textContent += lt
await delay(500)
}
}
)();
#sp3 span:last-of-type {
--blinkColor : darkblue;
color : var(--blinkColor);
font-weight : bold;
animation : 1s blink step-end infinite;
}
@keyframes blink {
from, to { color: transparent; }
50% { color: var(--blinkColor); }
}
<div id="sp3">
<span></span><span></span><span></span><span>|</span>
</div>
任何问题 ?
推荐阅读
- mysql - 为排除某些数据创建 SQL
- reactjs - ts/react - useEffect 中的 fetch 被多次调用
- git - 加密/解密 Git 存储库的选项?
- anaconda - anaconda:无法使用 shell 脚本切换环境
- python - 使用来自 csv Python 的数据更新 Firebird 表
- java - 让 git 忽略连续的换行符
- kubernetes - kops 导出 kubeconfig,TTL 不会减少
- dataset - 如何在 SSRS 中添加 2 个数据集
- php - 如何使用 PHP 过滤 REST API 中的响应?
- chart.js - 如何在chartjs或ng2-chart的箱形图中使用时间?