如何使用 document.write 更改文本并删除过去的文本
How to change text with document.write and delete past text
我想做一个网站的加载动画,我觉得这些Unicode字符会很好看:☰☱☲☴但我不知道怎么做。
设置元素内容的方法有很多种。这只是使用 setTimeout
或 setInterval
在暂停后继续更改内容的情况。
例如:
const chars = ['☰','☱', '☲', '☴'];
const el = document.getElementById('spinner');
let index = 0;
function changeChar() {
el.innerText = chars[index];
index = (index + 1) % chars.length;
setTimeout(changeChar, 200);
}
changeChar()
<div id="spinner" />
你可以这样做:
var animationContainer = document.querySelector('#animation');
var animationThing = 0;
setInterval(function() {
switch (animationThing) {
case 0:
animationContainer.innerHTML = '☰';
animationThing++;
break;
case 1:
animationContainer.innerHTML = '☱';
animationThing++;
break;
case 2:
animationContainer.innerHTML = '☲';
animationThing++;
break;
case 3:
animationContainer.innerHTML = '☴';
animationThing = 0;
break;
}
}, 200);
<span id="animation"></span>
我想做一个网站的加载动画,我觉得这些Unicode字符会很好看:☰☱☲☴但我不知道怎么做。
设置元素内容的方法有很多种。这只是使用 setTimeout
或 setInterval
在暂停后继续更改内容的情况。
例如:
const chars = ['☰','☱', '☲', '☴'];
const el = document.getElementById('spinner');
let index = 0;
function changeChar() {
el.innerText = chars[index];
index = (index + 1) % chars.length;
setTimeout(changeChar, 200);
}
changeChar()
<div id="spinner" />
你可以这样做:
var animationContainer = document.querySelector('#animation');
var animationThing = 0;
setInterval(function() {
switch (animationThing) {
case 0:
animationContainer.innerHTML = '☰';
animationThing++;
break;
case 1:
animationContainer.innerHTML = '☱';
animationThing++;
break;
case 2:
animationContainer.innerHTML = '☲';
animationThing++;
break;
case 3:
animationContainer.innerHTML = '☴';
animationThing = 0;
break;
}
}, 200);
<span id="animation"></span>