我有一個打字效果的代碼(下面是JS,CSS和html),但是我無法循環這個效果。我希望它完成後e。 G。十秒鐘後清除並重新打字。效果應該無限重複。我試着做一個循環來清除鍵入後的文本,將其懸停不起作用。任何線索/想法?謝謝。循環使用JS的打字效果
CSS:
#typewriter {
font-size: 18px;
margin: 0;
background: none;
border: none;
color: black;
font-family: Exo-Regular;}
pre::after{
content: "|";
animation: blink 500ms linear infinite alternate;
color: rgb(255,20,147);}
HTML:
<pre id="typewriter">
<span class="var-highlightST">my typewriting effect</span>
</pre>
JS:
function setupTypewriter(t) {
var HTML = t.innerHTML;
t.innerHTML = "";
var cursorPosition = 0,
tag = "",
writingTag = false,
tagOpen = false,
typeSpeed = 50,
tempTypeSpeed = 0;
var type = function() {
if (writingTag === true) {
tag += HTML[cursorPosition];
}
if (HTML[cursorPosition] === "<") {
tempTypeSpeed = 0;
if (tagOpen) {
tagOpen = false;
writingTag = true;
} else {
tag = "";
tagOpen = true;
writingTag = true;
tag += HTML[cursorPosition];
}
}
if (!writingTag && tagOpen) {
tag.innerHTML += HTML[cursorPosition];
}
if (!writingTag && !tagOpen) {
if (HTML[cursorPosition] === " ") {
tempTypeSpeed = 0;
}
else {
tempTypeSpeed = (Math.random() * typeSpeed) + 50;
}
t.innerHTML += HTML[cursorPosition];
}
if (writingTag === true && HTML[cursorPosition] === ">") {
tempTypeSpeed = (Math.random() * typeSpeed) + 50;
writingTag = false;
if (tagOpen) {
var newSpan = document.createElement("span");
t.appendChild(newSpan);
newSpan.innerHTML = tag;
tag = newSpan.firstChild;
}
}
cursorPosition += 1;
if (cursorPosition < HTML.length - 1) {
setTimeout(type, tempTypeSpeed);
}
};
return {
type: type
};}
var typer = document.getElementById('typewriter');
typewriter = setupTypewriter(typer);
typewriter.type();
var i = 0;
setInterval(typewriter.innerHTML = "", 100);
感謝您的幫助:)它完美:) – Mikisz