本文实例为大家分享了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);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。