javascript - 用单个跨度作为字母替换文本的 div 并遍历它
问题描述
我试图用span
每个字母的单个元素替换文本的 div,这很简单,但我试图让这些跨度字体大小逐渐增加,我不知道在 setTimeout 函数中要扔什么。
<div id ="foo"> Welcome to Here </div>
function test() {
let word = document.getElementById('foo');
let arr = word.split("")
word.innerHTML = "";
arr.forEach((x,index) => {
let newSpan = document.createElement('span')
newSpan.innerHTML = x;
word.appendChild(newSpan);
setTimeout(() => {
????
}, 100 + index*100)
})
}
解决方案
他是 settimeout 的又一次尝试
function test() {
var fontSize = 12;
let word = document.getElementById('foo');
let arr = word.innerHTML.split("")
word.innerHTML = "";
arr.forEach((x,index) => {
var fs = (fontSize + index) +"px";
let newSpan = document.createElement('span')
newSpan.innerHTML = x;
word.appendChild(newSpan);
setTimeout(function(){
newSpan.style.fontSize = fs
}, 2000 + (index * 100));
})
}
test();
span{
display : inline-block;
font-size:12;
}
<div id ="foo"> Welcome to Here </div>
推荐阅读
- forms - Xamarin Forms:GetAsync 在 SearchBar 事件处理程序中不起作用
- tsql - TSQL中不同手动输入的全名之间的模糊匹配算法?
- python - 我处理创建 python 字典的代码有什么问题?
- java - @SQLInsert 动态数据库架构
- mysql - MySQL 错误:操作数应在简单更新查询中包含 1 列
- spring-boot - Spring Boot 指标导出到 InfluxDB - 如何减少导出数量?
- javascript - 在调整窗口大小时,保持子元素根据其容器水平对齐,并根据其他子元素保持左对齐
- c# - 如何在“真实”方法中使用参数调用模拟方法?
- powershell - StaffHub Powershell Cmdlet - 团队 ID 必须具有 Team_guid 模式
- windows - 批处理脚本在 Windows 10 Pro 上运行,但不在 Surface Pro 4 上运行