我有以下代码,我已经“拼凑”在一起。基本上它应该采用一些JSON数据(其示例也在下面),预加载任何适用的图像并修改幻灯片以使用传回的数据。
它正在工作,但图像和文字不匹配,我认为它与currImg++%preloadArrayName.length
有关但是我不确定这是什么意思......
感谢任何帮助!
function updateHomepageSlideshow(data)
{
var preloadArr = new Array();
var preloadArrName = new Array();
var preloadArrOffer = new Array();
var i;
/* preload images */
for(i=0; i < data.length; i++){
preloadArr[i] = new Image();
preloadArr[i].src = 'http://media.domain.com/restaurants/large/' + data[i] ['restaurantCode'] + '.jpg';
preloadArrName[i] = data[i]['restaurantName'];
preloadArrOffer[i] = data[i]['offerName'];
}
var currImg = 1;
var intID = setInterval(changeImg(data), 6000);
/* image rotator */
function changeImg(data){
$('#amazingOffersAt').hide();
$('#homepageRestaurantNameId').html(preloadArrName[currImg++%preloadArrName.length]);
$('#homepageRotatingImage').css('background-image','url(' + preloadArr[currImg++%preloadArr.length].src +')');
$('#homepageRestaurantOfferId').html(preloadArrOffer[currImg++%preloadArrOffer.length]);
}
}
JSON
[{
"restaurantName": "Caf\u00e9 des Amis",
"restaurantCode": "cafe-des-amis",
"address": "11 - 14 Hanover Place",
"town": "Covent Garden",
"county": "London",
"postcode": "WC2E 9JP",
"lat": "51.5133900",
"lng": "-0.1231300",
"largeImage": "1",
"offerTypeId": "9",
"offerName": "3 course set menu for \u00a315"
}, {
"restaurantName": "Palm Court Brasserie",
"restaurantCode": "palm-court-brasserie",
"address": "39 King Street",
"town": "Covent Garden",
"county": "London",
"postcode": "WC2E 8JS",
"lat": "51.5119700",
"lng": "-0.1243000",
"largeImage": "1",
"offerTypeId": "12",
"offerName": "3 courses and a kir royale \u00a322.50"
}, {
"restaurantName": "Clos Maggiore",
"restaurantCode": "clos-maggiore",
"address": "33 King Street",
"town": "Covent Garden",
"county": "London",
"postcode": "WC2E 8JD",
"lat": "51.5116900",
"lng": "-0.1247700",
"largeImage": "1",
"offerTypeId": "12",
"offerName": "2 courses: \u00a315.50"
}, {
"restaurantName": "Navajo Joe",
"restaurantCode": "navajo-joe",
"address": "34 King Street",
"town": "Covent Garden",
"county": "London",
"postcode": "WC2E 8JD",
"lat": "51.5116900",
"lng": "-0.1247700",
"largeImage": "1",
"offerTypeId": "1",
"offerName": "50% Off Your Food "
}, {
"restaurantName": "Le Deuxieme",
"restaurantCode": "le-deuxieme",
"address": "65a Long Acre, Covent Garden",
"town": "London",
"county": "West End London",
"postcode": "WC2E 9JD",
"lat": "51.5139100",
"lng": "-0.1227800",
"largeImage": "1",
"offerTypeId": "12",
"offerName": "Sunday offer: 3 courses & half bottle of wine \u00a320"
}]
答案 0 :(得分:2)
你应该使用
var currImg = 0; // let's start at first image
var intID = setInterval(changeImg, 6000); // you don't need the data and you were passing the result of the function, not the function
/* image rotator */
function changeImg(data){
$('#amazingOffersAt').hide();
var index = currImg++%preloadArrName.length;
$('#homepageRestaurantNameId').html(preloadArrName[index]);
$('#homepageRotatingImage').css('background-image','url(' + preloadArr[index].src +')');
$('#homepageRestaurantOfferId').html(preloadArrOffer[index]);
}
您的代码在每一行都没有使用与currImg++
增量currImg
相同的索引。
更准确地说,currImg++%preloadArrayName.length
为(currImg++)%preloadArrayName.length
(请参阅precedence):currImg
的模数需要确保在[0, preloadArrayName.length[
中获取值(即数组中的有效索引)并递增currImg
。
请注意,只有3个阵列具有相同的大小时,您的代码才有效。我没有使用3个数组,而只使用了一个数组但是包含了对象(就像你在json中所拥有的那样)。
答案 1 :(得分:0)
您确定在changeImg()中使用增量索引的方法吗?进行模数运算有什么意义?也许我错过了什么。
如果你想简单地遍历图像,为什么不这样做呢:
var currImg = 0;
function changeImg(data){
if(currImg == preloadArrName.length - 1){
currImg = 0; // reset index to the first element
}else{
currImg++ // increment the index to the next element
}
$('#amazingOffersAt').hide();
$('#homepageRestaurantNameId').html(preloadArrName[index]);
$('#homepageRotatingImage').css('background-image','url(' + preloadArr[index].src +')');
$('#homepageRestaurantOfferId').html(preloadArrOffer[index]);
}