所以我有这段代码修改了刷新div的内容。
但我也希望它在一段时间内淡出和淡出一个新的随机文本。我不知道从哪里开始是我的原始代码:
HTML:
<div id="logo" class="container">
<h1>InsiderIndy</h1>
<p id="myQuote">Slogan</p>
</div>
JavaScript的:
<script type="text/javascript">
var myQuotes = new Array();
myQuotes[0] = "Quote1";
myQuotes[1] = "Quote2";
myQuotes[2] = "Quote3"
myQuotes[3] = "Quote4"
myQuotes[4] = "Quote5"
myQuotes[5] = "Quote6"
var myRandom = Math.floor(Math.random()*myQuotes.length);
$('#myQuote').html(myQuotes[myRandom]);
</script>
有什么想法吗?谢谢! :)
答案 0 :(得分:2)
具有可调节功能的完整工作示例,用于淡入淡出速度,旋转之间的暂停时间和开始位置。当它到达结束时也返回到开头:
编辑:我第一次看到你的问题太快了,并编写了一个顺序轮换的函数。我已经更新了我的答案,包括两个函数 - 一个用于顺序轮换startSeq()
,另一个用于随机轮换startRandom()
。
function startSeq(i,iSpeed, iPause) {
$('#myQuote').html(myQuotes[i]);
$('#myQuote').fadeIn(iSpeed,function() {
setTimeout(function() {
$('#myQuote').fadeOut(iSpeed,function() {
setTimeout(function() {
if (i++ == myQuotes.length) i =0;
startSeq(i,iSpeed,iPause);
},iPause);
});
},iPause);
});
}
function startRandom(iSpeed, iPause) {
var i = Math.floor(Math.random()*myQuotes.length);
$('#myQuote').html(myQuotes[i]);
$('#myQuote').fadeIn(iSpeed,function() {
setTimeout(function() {
$('#myQuote').fadeOut(iSpeed,function() {
setTimeout(function() {
startRandom(iSpeed,iPause);
},iPause);
});
},iPause);
});
}
//startSeq(0,1000,1000);
startRandom(1000,1000);
答案 1 :(得分:1)
看起来像jQuery,所以......
(function quote() {
var myRandom = Math.floor(Math.random()*myQuotes.length);
$('#myQuote').delay(5000)
.fadeOut(500, function() {
$(this).text(myQuotes[myRandom])
.fadeIn(500, quote);
});
})();
您可能需要一些时间来研究jQuery docs
答案 2 :(得分:1)
使用jQuery:
var refreshMillis = 10 * 1000; // 10 seconds.
setInterval(function() {
var myRandom = Math.floor(Math.random()*myQuotes.length)
, $myQuote = $('#myQuote');
$myQuote.fadeOut(function() {
$myQuote.html(myQuotes[myRandom]).fadeIn();
});
}, refreshMillis);