我已经在jQuery carousel
工作了很长一段时间。一直以来,我一直在提问,以便进入下一步,你们都非常乐于助人。我可以在第一次jQuery冒险的隧道尽头看到灯光,这应该是最后一步。
$(document).ready(function() {
var stage = $(".gallery");
var stageoffset = stage.offset();
var stageleft = stageoffset.left + 600;
var firstimage = $("img:eq(1)");
var lastimage = $("img:last");
function gallery () {
firstimage.animate({"margin-left":"+=100px"}, {duration: 1000, easing: "linear", progress: checkBox});
};
var interv = setInterval(gallery, 1000);
function checkBox () {
if(!(typeof lastimage.offset() === 'undefined')) {
var imageoffset = lastimage.offset();
var imageleft = imageoffset.left;
if(imageleft > stageleft) {
lastimage.prependTo(firstimage);
firstimage = lastimage;
lastimage = lastimage.prev();
};
} else {
clearInterval(interv);
};
};
});
正如您所看到的,感谢您们,检查图像是否在舞台之外的功能完美无缺。当触发时,我尝试拍摄第一张图像并使用“PrependTo”拍摄该图像,然后将其重新插入线条的前面。
现在,由于实际的第一张图像是最后一张图像,我将“firstimage”变量设置为新的第一张图像。然后我使用“lastimage.prev()”设置新的最后一个图像。
但是,正如您在JSFiddle上看到的那样,动画在原始最后一个图像被添加到原始第一个图像之后停止。如何让动画继续流动?
答案 0 :(得分:0)
已添加注释以供您参考已实施的逻辑
检查更新的小提琴here
$(document).ready(function() {
var img = [];
// Create images array to be looped through
img[0] = '<img src="Hydrangeas.jpg" id ="image" class="image" alt=""/>';
img[1] = '<img src="Jellyfish.jpg" id="image-two" class="image" alt=""/>';
img[2] = '<img src="Koala.jpg" id="image-three" class="image" alt=""/>';
// Counter for looping through img array
var i = 0;
var stage = $(".gallery");
stage.append(img[i]);
var secondimage = null;
var firstimage = $("img:eq(0)");
firstimage.css('margin-left', -firstimage.width());
function gallery() {
// Since we need to show to images at a certain point of time and to move both the images i have used, firstimage and secondimage
if (firstimage !== null) {
// If firstimage is set animate that
firstimage.animate({"margin-left": "+=100px"}, {duration: 1000, easing: "linear", progress: checkBox});
}
if (secondimage !== null) {
// If secondimage is set animate that
secondimage.animate({"margin-left": "+=100px"}, {duration: 1000, easing: "linear", progress: checkBox});
}
}
var interv = setInterval(gallery, 1000);
function checkBox() {
// If first image is not null and second is null and first image is about to move out of div,
// set second image as another iteration of img variable, once it moves out it is set as null
// If second image is not null and first is null and second image is about to move out of div,
// set first image as another iteration of img variable, once it moves out it is set as null
// Finally, if all the iterations are set, then set i as -1 i.e. reset counter
if ((secondimage === null && firstimage !== null && parseInt(firstimage.css('margin-left')) > parseInt(stage.width() / 3))
|| (firstimage === null && secondimage !== null && parseInt(secondimage.css('margin-left')) > parseInt(stage.width() / 3)) || (i === -1)) {
i += 1;
if (i < img.length) {
stage.prepend(img[i]);
$("img:eq(0)").css('margin-left', -$("img:eq(0)").width());
if (secondimage === null) {
secondimage = $("img:eq(0)");
} else {
firstimage = $("img:eq(0)");
}
} else {
i = -1;
}
} else if (firstimage !== null && (parseInt(firstimage.css('margin-left')) > parseInt(stage.width()))) {
// remove first image
firstimage.remove();
firstimage = null;
} else if (secondimage !== null && (parseInt(secondimage.css('margin-left')) > parseInt(stage.width()))) {
// remove second image
secondimage.remove();
secondimage = null;
}
}
});