【发布时间】:2022-01-04 02:19:01
【问题描述】:
我正在尝试制作打字动画,但它不起作用,我不知道为什么。我知道我可以在 CSS 中做到这一点,但我想在 JS 中尝试一下。问题可能出在函数本身我不是 JS 中最好的。
<p class="typing-animation">this will be animated</p>
<p class="typing-animation">this aswell</p>
<script>
const sleep = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
const toType = document.getElementsByClassName("typing-animation");
document.getElementsByClassName("typing-animation").textContent = "";
(async (toType) => {
for (let each of toType) {
text = each.textContent;
each.textContent = "";
let i = 0;
for (let every of text) {
every.textContent += text[i];
await sleep(200);
i++;
}
}
})(toType);
</script>
【问题讨论】:
标签: javascript animation