對於JavaScripters,我將是一個簡單的方法。我有很長的研究,但我找不到正確的答案。我想要一個菜單(基本上只是錨點而不是列表元素)像具有特定時間延遲的滑塊一樣高亮顯示。jQuery loop - setTimeout,addClass,removeClass
另外,如果你知道如何擺脫所有這些無用的ID(「#menu a」)和$(this),這將是很好的。由於我不能做很多JavaScript(儘管我更喜歡簡單),下面是我在jQuery中的糟糕代碼,它可以工作,但它不是循環的。
$("#anchor1").addClass("highlight");
function loopMenu() {
window.clearTimeout();
setTimeout(function(){$("#anchor1").removeClass("highlight");}, 4000);
setTimeout(function(){$("#anchor2").addClass("highlight");}, 4000);
setTimeout(function(){$("#anchor2").removeClass("highlight");}, 8000);
setTimeout(function(){$("#anchor3").addClass("highlight");}, 8000);
setTimeout(function(){$("#anchor3").removeClass("highlight");}, 12000);
setTimeout(function(){$("#anchor4").addClass("highlight");}, 12000);
setTimeout(function(){$("#anchor4").removeClass("highlight");}, 16000);
setTimeout(function(){$("#anchor1").addClass("highlight");}, 12000);
}
loopMenu();
我想要什麼:一個腳本,從當前元素和addClass消除類以每4秒下一個錨式元素,然後跳轉到第一個元素,永遠重複。
Here is a solved question與此有一些關係,雖然我不能讓它工作。
延遲或隊列怎麼樣 – mplungjan 2013-05-14 14:59:31
你需要在4秒後發生所有其他迭代後再次執行loopMenu。 – 2013-05-14 15:00:21