DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 輪播圖組件js代碼
輪播圖組件js代碼
編輯:關於JavaScript     

本文實例為大家分享了JavaScript輪播圖組件代碼,供大家參考,具體內容如下

//輪播圖組件
function Rolling(o) {
 this.index = ++o.index || 1; //當前滾動的位置,當index大於可輪播的次數listLength或者等於0,為不可滾動狀態
 this.num = o.num || 1; //默認滾動一個列表
 this.obj = o.obj || null; //要輪播的對象ul
 this.perObj = o.perObj || null; //向上翻頁的按鈕對象
 this.nextObj = o.nextObj || null; //向下翻頁的按鈕對象
 this.focusPoint = o.focusPoint || null; //焦點對象,默認為null。意思不開啟焦點對象,如要開啟可傳入焦點對象可自動開啟
 this.focusClass = o.focusClass || 'mien-active'; //當前焦點位置類名
 this.replaceBtn = o.replaceBtn || false;//是否在輪播到第一頁或最後一頁時替換翻頁按鈕圖片。默認值為true,並替換按鈕圖片為re+圖片名。如:per.png替換成re-per.ping
 console.log(o.replaceBtn)
 this.listLength = Math.ceil(o.obj.find('li').length / this.num); //可輪播的次數
 this.listObj = o.obj.children('li');
 this.listWidth =this.listObj.width() + parseInt(this.listObj.css('margin-left')) + parseInt(this.listObj.css('margin-right')); //列表寬度
 this.init(); //初始化
};
Rolling.prototype.init = function() {
   
  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 + '"></li>';
  } else {
   str += '<li></li>';
  }
 }
 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',perObject.attr('re-src'));
   nextObject.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);
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved