我有一个使用 jQuery 来更改图像的工作图像滑块(是的,我知道在 DIV 中使用背景图像不是最佳做法,但它是唯一适用于我页面其余部分的方法):
我的 HTML:
<a id="featuredLink" href="#"><div class="featuredImages" style="background-image:url('http://www.placehold.it/2000x500/00ff00.png')"><div class="slide"></div></div></a>
jQuery:
var urls = [ 'http://www.google.com','http://www.microsoft.com','http://www.yahoo.com' ];
var images = [ 'http://www.placehold.it/2000x500/ff0000.png','http://www.placehold.it/2000x500/00ff00.png','http://www.placehold.it/2000x500/0000ff.png' ];
var cur_image = 0;
function changeBackground() {
cur_image++;
if ( cur_image >= images.length )
cur_image = 0;
// change images
$( '.featuredImages' ).css({
backgroundImage: 'url(' + images[ cur_image ] + ')'
});
$( '.featuredImages .slide' ).fadeOut( 3000, function(){
$( this ).css({
backgroundImage: 'url(' + images[ cur_image ] + ')'
}).show();
} );
}
setInterval( changeBackground, 10000 );
和 CSS:
.featuredImages {
position:absolute;
margin: 0;
height: 500px;
width: 100%;
background: transparent url('') no-repeat center;
overflow: hidden;
}
.featuredImages .slide {
position: absolute;
margin: 0;
height: 500px;
width: 100%;
background: transparent url('') no-repeat center;
overflow: hidden;
}
我想在图像交换时更改该锚标记(“featuredLinks”)的href,以便它链接到图像相关的页面。
我想使用一个数组来存储链接(“urls”)和用于图像的相同循环计数器来跟踪要使用的链接。
任何指向正确方向的指针都会很棒!
提前致谢!