本文实例为大家分享了JavaScript轮播图组件代码,供大家参考,具体内容如下
var thisObj = this;
this.initLeft();
this.replaceFun();
if(this.focusPoint !== null) {
this.createFocusPoint();
}
this.perObj.unbind('click').on('click',function() {
thisObj.rollPrev();
});
this.nextObj.unbind('click').on('click',function() {
thisObj.rollNext();
});
}
//创建焦点,并给当前位置的焦点添加类mien-active
Rolling.prototype.createFocusPoint = function() {
var str = '',thisObj = this;
for(var i = 0; i < this.listLength; i++) {
if(i == this.index - 1) {
str += '<li class="' + this.focusClass + '">';
} else {
str += '
}
}
this.focusPoint.append(str);
this.focusPoint.children().click(function() {
var oldIndex = $('.' + thisObj.focusClass).index() + 1; //之前的焦点位置
var index = $(this).index() + 1; //当前点击的焦点
var sum = index - oldIndex;
var perObject = thisObj.perObj.find('img');
var nextObject = thisObj.nextObj.find('img');
if (index == 1 && !thisObj.replaceBtn){
perObject.attr('src',perObject.attr('data-src'));
nextObject.attr('src',nextObject.attr('data-src'));
}else if (index == thisObj.listLength && !thisObj.replaceBtn){
perObject.attr('src',perObject.attr('re-src'));
nextObject.attr('src',nextObject.attr('re-src'));
}else if (!thisObj.replaceBtn){
perObject.attr('src',nextObject.attr('data-src'));
}
thisObj.index += sum;
if(sum > 0) {
thisObj.obj.animate({
marginLeft: '-=' + Math.abs(sum) thisObj.num thisObj.listWidth + 'px'
});
}
if(sum < 0) {
thisObj.obj.animate({
marginLeft: '+=' + Math.abs(sum) thisObj.num thisObj.listWidth + 'px'
});
}
$(this).addClass(thisObj.focusClass).siblings().removeClass(thisObj.focusClass);
});
}
Rolling.prototype.initLeft = function() {
if(this.index == 1) {
return;
}
this.obj.css('margin-left',-(this.index - 1) this.listWidth); //初始化全屏图片显示的位置
}
Rolling.prototype.rollPrev = function() {
--this.index;
//当点击到第一页就return
if (this.index <= 1 && !this.replaceBtn){
this.perObj.find('img').attr('src',this.perObj.find('img').attr('data-src'));
}
if(!this.thisIndex()) {
++this.index;
return;
}
if (!this.replaceBtn){
this.nextObj.find('img').attr('src',this.nextObj.find('img').attr('data-src'));
}
this.obj.animate({
marginLeft: '+=' + this.num this.listWidth + 'px'
});
if(this.focusPoint !== null) {
$('.' + this.focusClass).removeClass(this.focusClass).prev().addClass(this.focusClass);
}
}
Rolling.prototype.rollNext = function() {
++this.index;
if (this.index == this.listLength && !this.replaceBtn){
this.nextObj.find('img').attr('src',this.nextObj.find('img').attr('re-src'));
}
//当点击到最后一页就return
if(!this.thisIndex()) {
--this.index;
return;
}
if (!this.replaceBtn){
this.perObj.find('img').attr('src',this.perObj.find('img').attr('re-src'));
}
this.obj.animate({
marginLeft: '-=' + this.num * this.listWidth + 'px'
});
if(this.focusPoint !== null) {
$('.' + this.focusClass).removeClass(this.focusClass).next().addClass(this.focusClass);
}
}
Rolling.prototype.replaceFun = function(){
var perObject = this.perObj.find('img'),nextObject = this.nextObj.find('img');
var perSrc = perObject.attr('src'),nextSrc = nextObject.attr('src');
perObject.attr({'data-src':perSrc,'re-src':this.splitSrc(perSrc)});
nextObject.attr({'data-src':nextSrc,'re-src':this.splitSrc(nextSrc)});
}
Rolling.prototype.splitSrc = function(str){
var list = str.split('/');
var data = list[list.length-1];
var sub = data.substr(0,data.indexOf('.'));
return str.replace(sub,'re-' + sub);
}
Rolling.prototype.thisIndex = function() {
if(this.index > this.listLength | this.index <= 0) {
return false;
}
return true;
}
function createRolling(o) {
return new Rolling(o);
}