【发布时间】:2014-05-04 22:11:45
【问题描述】:
我需要帮助来改进我的背景图片幻灯片的脚本。
我设法用.fadeIn(600) JS 函数创建了一个淡入 效果。但是,我希望图像在彼此之间切换,而不是在白色背景之间切换。
换句话说,我需要平滑图像之间的过渡。
请注意,每张图片都有一个标题,但我可以接受。我不需要任何文本转换。
JSFiddle http://jsfiddle.net/newsha/B4TXj/
var timer;
$(document).ready(function () {
// background image on load
var numberImages = 4
var images = [];
for (var i = 1; i <= numberImages; i++) {
images.push(i);
}
var imgValue = 1
$('.backgroundImage').addClass('bg' + imgValue + '');
// Switch background image - forwards
$('.imgSwitchRight').click(function () {
$('.backgroundImage').each(function (e) {
$(this).removeClass('bg1 bg2 bg3 bg4').fadeOut(0);
});
clearTimeout(timer);
timer = setTimeout(function () {
$('.imgSwitchRight').click();
}, 8000);
if (imgValue < numberImages) {
imgValue++
} else {
imgValue = 1
}
$('.backgroundImage').addClass('bg' + imgValue + '').fadeIn(600);
});
clearTimeout(timer);
timer = setTimeout(function () {
$('.imgSwitchRight').click();
}, 8000);
// Switch background - backwards
$('.imgSwitchLeft').click(function () {
$('.backgroundImage').each(function (e) {
$(this).removeClass('bg1 bg2 bg3 bg4').fadeOut(0);
});
clearTimeout(timer);
timer = setTimeout(function () {
$('.imgSwitchRight').click();
}, 8000);
if (imgValue > 1) {
imgValue--
} else {
imgValue = numberImages
}
$('.backgroundImage').addClass('bg' + imgValue + '').fadeIn(600);
});
clearTimeout(timer);
timer = setTimeout(function () {
$('.imgSwitchRight').click();
}, 8000);
});
【问题讨论】:
-
我认为您的问题是在淡入新图像之前隐藏了所有其他图像(通过
.bgx类)。在淡入新的时保持当前图像可见,然后才隐藏它。 -
我推荐使用Backstretch as a slideshow。这是一个很棒的库,可以完成您要完成的工作
标签: javascript jquery css slideshow transition