欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

轮播图组件js代码

时间:2020-10-06 21:53:50|栏目: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);
}

上一篇:js实现非常棒的弹出div

栏    目:JavaScript代码

下一篇:解决微信小程序调用moveToLocation失效问题【超简单】

本文标题:轮播图组件js代码

本文地址:http://www.codeinn.net/misctech/7538.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有