[英]how to set interval in order to display images by turn
所以...我從數組中獲取圖像,設置時間間隔的問題,以便依次顯示圖像
$(function() {
var length = $('.fadein img').length; //get the number of images
var arrayimage = [];
for (i=0; i<length; i++){ // fill the array
arrayimage.push($(".fadein img:eq("+ i +")").attr("src"));
}
var lengtharray = arrayimage.length;
setTimeout(function run() {
for (i=0; i<lengtharray; i++){ //get images by turn
var cft = arrayimage[i];
var imageurl = cft;
$("html").css({
'background': '#000000 url("' +imageurl + '") no-repeat top left fixed',
'background-size' : 'cover',
'width':'100%',
'height':'100%',
'position':'avsolute'
});
setTimeout(cft, 5000);
alert("set");
}
},5000);
});
如何設置間隔? 如果需要的話是html代碼:)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="style.css" rel="stylesheet" type="text/css">
<script src="js/jquery/jquery2.0.3.js"></script>
<script src="js/jquery/slideshow.js"></script>
</head>
<body>
<div class="fadein">
<img src="images/backgrounds/stones.jpg"/>
<img src="images/backgrounds/pier.jpg"/>
<img src="images/backgrounds/sea-mist.jpg"/>
<img src="images/backgrounds/mojave.jpg"/>
<img src="images/backgrounds/flowing-rock.jpg"/>
</div>
</body>
</html>
聽起來好像要setInterval(function, milliseconds)
而不是setTimeout
。
你有
setTimeout(cft, 5000);
'cft'是一個字符串。 您應該將setTimeout傳遞給函數。 您必須根據可重復使用的函數來考慮setTimeout。 您可以在第一個外部setTimeout中執行該函數,然后在后續的setTimeouts中內部調用該函數。 如果您希望它立即開始,則第一個調用將直接指向您的函數,而不是setTimeout中。
您正在使用什么幻燈片插件? 您可能會在文檔中找到一些所需的答案。
這是我在想的一個例子:
$(function() {
//set up
$('.fadein img').hide().eq(0).addClass('nextup');
rotate = function(){
var imageurl = $('.fadein img.nextup').attr('src')
//next
$('.fadein img.nextup').removeClass('nextup').next('img').addClass('nextup');
if($('.fadein img.nextup').length == 0){
//back to first
$('.fadein img').hide().eq(0).addClass('nextup');
}
$("html").css({
'background': '#000000 url("' +imageurl + '") no-repeat top left fixed',
'background-size' : 'cover',
'width':'100%',
'height':'100%',
'position':'absolute'
});
setTimeout(rotate, 5000);
};
rotate();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.