はい、これはJavaScript setInterval
で実行できます。
//Global Scope variables, usable in all functions...
var randomStuff = ["Foo", "Bar", "Even multiple Words"];
var $target;
var loadContentIndex = 0;
$(function() {
$target = $('#target'); //Set the target div...
loadContent(); //Initiate it once on page load...
window.setInterval(loadContent, 2000); //And set it to work every 2000ms (or 2s).
});
function loadContent() {
$target.fadeOut(function() { //Once fade out is complete...
$target.text(randomStuff[loadContentIndex]); //Change the text
$target.fadeIn(); //Fade back in.
});
loadContentIndex++; //Increase the array counter.
if (randomStuff.length == loadContentIndex) { //If reached the end of the array...
loadContentIndex = 0; //Reset the counter :)
}
}
注:私はそれがそんなに簡単だという理由だけで、アニメーションを処理するためにjQueryライブラリを使用しています。
http://tvtropes.org/pmwiki/pmwiki.php/Main/OnTheNext – sdleihssirhc
うわー、私のためのコードをしてくれてありがとう! – Matt