【发布时间】:2020-07-15 11:24:01
【问题描述】:
【问题讨论】:
-
欢迎来到 Stackoverflow。您还需要发布到目前为止您尝试过的内容。
标签: javascript html css web animation
【问题讨论】:
标签: javascript html css web animation
这里是您正在寻找的内容的粗略版本:
const element = document.querySelector("#element");
const startTime = Date.now();
const duration = 2000;
const letters = element.dataset.text.split("");
const steps = letters.length;
const map = (n, x1, y1, x2, y2) => Math.min(Math.max(((n - x1) * (y2 - x2)) / (y1 - x1) + x2, x2), y2);
const random = (set) => set[Math.floor(Math.random() * set.length)];
let frame;
(function animate() {
frame = requestAnimationFrame(animate);
const step = Math.round(map(Date.now() - startTime, 0, duration, 0, steps));
element.innerText = letters
.map((s, i) => (step - 1 >= i ? letters[i] : random("0123456789")))
.join("");
if (step >= steps) {
cancelAnimationFrame(frame);
}
})();
<div id="element" data-text="After Effects"></div>
【讨论】: