如何使用 document.write 更改文本并删除过去的文本

我想为网站制作一个加载动画,我认为这些 Unicode 字符看起来不错:☰ ☱ ☲ ☴ 但我不知道该怎么做。



德玛西亚99
浏览 125回答 2
2回答

呼啦一阵风

你可以用这样的方法来做到这一点:var animationContainer = document.querySelector('#animation');var animationThing = 0;setInterval(function() {&nbsp; switch (animationThing) {&nbsp; &nbsp; case 0:&nbsp; &nbsp; &nbsp; &nbsp; animationContainer.innerHTML = '☰';&nbsp; &nbsp; &nbsp; &nbsp; animationThing++;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; animationContainer.innerHTML = '☱';&nbsp; &nbsp; &nbsp; &nbsp; animationThing++;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; animationContainer.innerHTML = '☲';&nbsp; &nbsp; &nbsp; &nbsp; animationThing++;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; animationContainer.innerHTML = '☴';&nbsp; &nbsp; &nbsp; &nbsp; animationThing = 0;&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp;}}, 200);<span id="animation"></span>

扬帆大鱼

有多种方法可以设置元素的内容。setTimeout这只是使用或 来setInterval在暂停后继续更改内容的情况。例如:const chars = ['☰','☱', '☲', '☴'];const el = document.getElementById('spinner');let index = 0;function changeChar() {&nbsp; el.innerText = chars[index];&nbsp; index = (index + 1) % chars.length;&nbsp; setTimeout(changeChar, 200);}changeChar()<div id="spinner" />
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript