我将 Arnoud Sietsema 的回答更进一步。轮播中的图像可以从加载 gif 开始。然后...
显示轮播
function showGallery(startIndex) {
//Load first, second and last image
var imageElement = $('#fullscreengallery .item:nth-child(' + (startIndex + 1) + ') img');
imageElement.attr('src', imageElement.attr('data-lazy-load-src'));
imageElement.attr('data-loaded', '1');
imageElement = $('#fullscreengallery .item:nth-child(' + (startIndex + 2) + ') img');
imageElement.attr('src', imageElement.attr('data-lazy-load-src'));
imageElement.attr('data-loaded', '1');
imageElement = $('#myCarousel .item:last img');
imageElement.attr('src', imageElement.attr('data-src'));
imageElement.attr('data-loaded', '1');
//Init carousel
$('#fullscreengallery').carousel(startIndex);
//Show carousel
$('#fullscreengallery').removeClass('hidden');
};
做延迟加载
$('#fullscreengallery').on('slid.bs.carousel', function() {
//Lazy load
var nextImage = $('.active.item', this).next('.item').find('img');
//If no next, select first
if(nextImage.length === 0){
nextImage = $('#fullscreengallery .item:nth-child(1) img');
}
//Set the source if it hasn't been set already
if (nextImage.attr('data-loaded') == '0') {
var $downloadingImage = $("<img>");
$downloadingImage.load(function(){
nextImage.attr("src", $(this).attr("src"));
nextImage.attr('data-loaded', '1');
});
$downloadingImage.attr("src", nextImage.attr('data-src'));
}
var prevImage = $('.active.item', this).prev('.item').find('img');
//If none, select last
if(prevImage.length === 0){
prevImage = $('#myCarousel .item:last img');
}
//Set the source if it hasn't been set already
if (prevImage.attr('data-loaded') == '0') {
var $downloadingImage = $("<img>");
$downloadingImage.load(function(){
prevImage.attr("src", $(this).attr("src"));
prevImage.attr('data-loaded', '1');
});
$downloadingImage.attr("src", prevImage.attr('data-src'));
}
});
不同之处在于,当尝试加载下一张幻灯片时,它首先“在引擎盖下”加载,并且只有在加载时才插入轮播。因此,加载图像会一直显示,直到图像完全加载。
此外,旋转木马现在可以双向移动。