以下使用稍微不同的方法,使您可以轻松添加 2 个以上的图像:
HTML
<!-- Define a single img element -->
<img src="http://musicglue-profile-pages.s3.amazonaws.com/wp-content/uploads/2013/03/NiceandSlezzy.png" alt="The Tin Pigeons" title="The Tin Pigeons" id="display">
JavaScript / jQuery
$(document).ready(function () {
// create an array that contains a entry for each img src. For more images, just add additional array entries.
var images = ['http://musicglue-profile-pages.s3.amazonaws.com/wp-content/uploads/2013/03/NiceandSlezzy.png',
'http://musicglue-profile-pages.s3.amazonaws.com/wp-content/uploads/2013/03/La-Route-Du-Rock1.jpg'];
var curImage=images.length-1;
var $image = $('#display');
function swap() {
$image.hide().attr('src', images[curImage]);
if (curImage==images.length-1) {
curImage=-1
}
curImage++;
$image.fadeIn(500)
}
setInterval(swap, 2000);
});