编写一个JS文件和一个网页,要求网页每5秒钟更换一张图片

写出所有代码 所有文件能给我 谢谢哪位兄弟帮忙
2025-04-30 12:48:23
推荐回答(1个)
回答1:

===========================demo.html===========================



demo








=======================ImageSwiter.js========================
var ImageSwitcher = function(targetImg, images) {
this.targetImg = targetImg;
this.images = images;
if (!targetImg) {
this.targetImg = targetImg = document.createElement("img");
document.body.appendChild(targetImg);
}
};
ImageSwitcher.prototype = {
targetImg: null,
images: null,
currentImageIndex: 0,
timer: null,
start: function(interval) {
var self = this;
this.switchImage(0);
this.timer = setInterval(function(){
self.switchImage(++self.currentImageIndex);
}, interval);
},
stop: function() {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
},
switchImage: function(imageIndex) {
this.targetImg.src = this.images[imageIndex];
}
};