[英]Rewriting word effect in Javascript
我正在循環切換文本以獲得視覺效果。 這是它的樣子https://naejc.github.io/Script/GodsLies
這是代碼
const symbols = ['God', 'Words'];
let count = 0;
const element = document.getElementById("WORD1");
const iteration = () => {
element.innerHTML = symbols[parseInt(count / 2, 10) % symbols.length];
if (count % 2 !== 0) {
element.classList.add("LANGUAGE");
} else {
element.classList.remove("LANGUAGE");
}
count++;
if (count === symbols.length * 2) {
count = 0;
}
};
let inthandle = setInterval(iteration, 270);
iteration();
有人可以告訴我如何在多個實例中使用此腳本嗎? 如果再次使用它沒有效果,試圖創建另一個切換文本的元素。
你如何嘗試創建一個“新”元素? 請注意,每個id
每個頁面只能使用一次。 它必須是獨一無二的。 否則 js 代碼將使用它找到的第一個id
。
https://www.w3schools.com/html/html_id.asp
您可能需要重寫代碼以使用類或類似的東西:
var initWords = function (element) {
const symbols = ['God', 'Words'];
let count = 0;
const iteration = () => {
element.innerHTML = symbols[parseInt(count / 2, 10) % symbols.length];
if (count % 2 !== 0) {
element.classList.add("LANGUAGE");
} else {
element.classList.remove("LANGUAGE");
}
count++;
if (count === symbols.length * 2) {
count = 0;
}
};
let inthandle = setInterval(iteration, 270);
iteration();
}
initWords(document.getElementById("WORD1"));
initWords(document.getElementById("WORD2"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.