Carousel component js code

  • 2021-07-09 06:55:02
  • OfStack

In this paper, we share the JavaScript carousel component code for your reference. The specific contents are as follows


// Carousel chart component 
function Rolling(o) {
 this.index = ++o.index || 1; // The current scrolling position, when index Greater than the number of carousels listLength Or equal to 0, Is in a non-scrollable state 
 this.num = o.num || 1; // Default scrolling 1 List 
 this.obj = o.obj || null; // Objects to carousel ul
 this.perObj = o.perObj || null; // Page Up Button Object 
 this.nextObj = o.nextObj || null; // Page Down Button Object 
 this.focusPoint = o.focusPoint || null; // Focus object, default to null . Do not open the focus object. If you want to open the focus object, you can open it automatically 
 this.focusClass = o.focusClass || 'mien-active'; // Current focus position class name 
 this.replaceBtn = o.replaceBtn || false;// Whether it is in carousel to the first 1 Page or last 1 Replace the page button picture when page. The default value is true, And replace the button picture with re+ Picture name. Such as: per.png Replace with re-per.ping
 console.log(o.replaceBtn)
 this.listLength = Math.ceil(o.obj.find('li').length / this.num); // Number of times that can be carousel 
 this.listObj = o.obj.children('li');
 this.listWidth =this.listObj.width() + parseInt(this.listObj.css('margin-left')) + parseInt(this.listObj.css('margin-right')); // List width 
 this.init(); // Initialization 
};
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();
  });
 }
 // Create Focus , And add a class to the focus at the current position 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; // Previous focus position 
  var index = $(this).index() + 1; // The focus of the current click 
  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); // Initialize the position of full-screen picture display 
}
Rolling.prototype.rollPrev = function() {
 --this.index;
 // When you click to the 1 Page on 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'));
 }
 // When clicked to the end, 1 Page on 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);
}

Related articles: