几个月前,我构建了一个简单的图像转换脚本,效果很好。我的脚本不只是交换图像,而是首先将包含 div 的背景设置为第一个图像。这样,当第一张图像被隐藏时,没有闪烁,而第二张图像淡入。您可以在AdairHomes.com上查看过渡,这是大图像旋转。
HTML 包装器
<div id="galleryContent" style="background:url(images/simple-banner-image-bath.jpg) top left;">
<img id="bannerImage" src="<!--Place the first image source here-->" alt="<!--Place your first ALT tag here-->" width="960" height="562" />
</div>
jQuery
<script type="text/javascript">
var banners = new Array();
// Array of images which you want to cycle
banners = [{"Source":"images/simple-banner-image-construction2.jpg","Alt":"Construction - Framing"},
{"Source":"images/simple-banner-image-bath.jpg","Alt":"Signature Custom Bathroom"},
{"Source":"images/simple-banner-image-2659.jpg","Alt":"Model Signature 2659"},
{"Source":"images/simple-banner-image-kitchen.jpg","Alt":"Signature Custom Kitchen"},
{"Source":"images/simple-banner-image-2432.jpg","Alt":"Model Signature 2432"},
{"Source":"images/simple-banner-image-1405.jpg","Alt":"Model Signature 1405"},
{"Source":"images/simple-banner-image-2830.jpg","Alt":"Model Signature 2830"}];
var count = banners.length - 1; // subtracting 1 accounts for the array starting at [0]
var counting = 0;
going = setInterval(function(){
$("#bannerImage").fadeOut(200,function(){
$(this).attr({'src':banners[counting]['Source'],'alt':banners[counting]['Alt']}).fadeIn(200);
});
// Select Next Picture
if(counting != count){counting++;}else{counting = 0;}
// Set new background image
$("#galleryContent").css({'background':'url('+banners[counting]['Source']+') top left'});
},6000);
var loadCount = 1;
// Preload images after the first image is displayed
slowLoad = setInterval(function(){
if(loadCount < banners.length){
$("body").append('<img src="'+banners[loadCount]['Source']+'" style="display:none;" />');
loadCount++;
}else{
clearInterval(slowLoad);
}
},500);
</script>
您会注意到我选择将我正在旋转的所有图像保留在一个数组中。这是因为我使用 PHP 管理所有包含的图像。
使用此代码,您基本上只需要设置#galleryContent
div 的样式以使其看起来像您希望的样子,并将所有带有 alt 标签的图像添加到横幅数组中。您还需要将要首先显示的任何标题放入#bannerImage
.
如果您需要更多代码或帮助,请告诉我。我想你可以从我上面的链接中得到很好的感觉。