Creating a JQuery Slideshow of a Background-Image
I have an element that serves as the banner on my website. This banner has HTML content on it, but uses a high-resolution picture as the background-image. Because of this, I'm loading the background-image last and fading it in when it has been downloaded. My code looks like this:
<table id="bannerTable" border="0" cellpadding="0" cellspacing="0" style="width:640px; height:480px;">
<tr><td style="padding-top:20px; padding-left:300px;">开发者_如何学C;
<div>[Some Text]</div>
</td></tr>
<tr><td style="vertical-align:bottom; padding-bottom:20px;">
<div>[Site Menu Goes Here]</div>
</td></tr>
</table>
<script type="text/javascript">
$(document).ready(function () {
$("#bannerTable").loadBGImage("/picture1.png");
});
$.fn.loadBGImage = function (url) {
var t = this;
$('<img />')
.attr('src', url)
.load(function () {
t.each(function () {
$(this).hide();
$(this).css('backgroundImage', 'url(' + url + ')');
$(this).fadeIn();
});
});
return this;
}
</script>
Even with this, my website feels static. Because of this, I want to loop through multiple high-resolution images at runtime. These high-resolution images will be used in the bannerTable as the background-image. I want them to fade-in and fade-out as they loop.
How do I do this with jQuery?
- DEMO: http://so.devilmaycode.it/creating-a-jquery-slideshow-of-a-background-image
look the demo source for the full working code
Basically use setInterval
to call loadBGImage, say every 60 seconds.
e.g. something like
var interval = 0;
var intervalTimer = setInterval(
function(){
if (++interval > 10) {interval = 1}
$("#bannerTable").loadBGImage("/picture" + interval + ".png");
}
,
60000
);
精彩评论