我的JS轮播图笔记(未完)
function Banner() {
this.bannerGroup = $("#banner-group");
this.leftArrow = $('.left-arrow');
this.rightArrow = $('.right-arrow');
this.listenBannerHover();
this.bannerUl = $('#banner-ul');
this.liList = this.bannerUl.children('li');
this.bannerCount = this.liList.length;
this.index = 0;
}
Banner.prototype.toggleArrow = function (isShow) {
var self = this;
if (isShow) {
self.leftArrow.show();
self.rightArrow.show();
} else {
self.leftArrow.hide();
self.rightArrow.hide();
}
};
Banner.prototype.listenArrowClick = function () {
self = this;
self.leftArrow.click(function () {
if (self.index === 0) {
self.index = self.bannerCount - 1;
} else {
self.index--;
}
self.animate();
});
self.rightArrow.click(function () {
if (self.index === self.bannerCount - 1) {
self.index = 0;
} else {
self.index++;
}
self.animate();
})
};
Banner.prototype.listenBannerHover = function () {
var self = this;
this.bannerGroup.hover(function () {
clearInterval(self.timer);
self.toggleArrow(true);
}, function () {
self.loop();
self.toggleArrow(false);
})
};
Banner.prototype.animate = function () {
this.bannerUl.animate({"left": -798 * this.index}, 500);
};
Banner.prototype.loop = function () {
var self = this;
this.timer = setInterval(function () {
if (self.index >= 3) {
self.index = 0;
} else {
self.index++;
}
self.animate();
}, 2000);
};
Banner.prototype.run = function () {
this.loop();
this.listenArrowClick();
};
$(function () {
var banner = new Banner();
banner.run();
});