/* version: 1.0.0 author: aykjweb */ (function($){ $.fn.extend({ /* by:gc */ //bigimg time:2020-12 bigimg:function(opts){ //定义默认的配置 var defaults ={ autoplay:true, //自动播放 speed:1000, //自动播放速度 bigimgwidth:600, //大图宽度 bigimgheight:400, //大图高度 containerclass:".bigimgconbox", //大图class slideboxclass:".bigimgslidebox", //小图切换class slidebdclass:".bigimgslidebd", //小图切换子级class slideclass:".bigimgslide", //大图滑块class slideclass1:".bigmyslide", //小图滑块class prevbtn:".prev", //大图上一个按钮 nextbtn:".next", //大图下一个按钮 slideprevbtn:".slideprev", //小图上一个按钮 slidenextbtn:".slidenext", //小图上一个按钮 slidenum:5, //小图滑块显示个数 slidebetween:10, //小图滑块间距 bilateral:50 //小图两侧间距 }; var i = 0,timer,num = 2; //将传入的参数覆盖默认参数中的默认项,最终合并到一个新的参数对象上 var program = $.extend({},defaults,opts); var _this=$(this); var _thischild=_this.children().children(program.slideclass); var _thissibling = _this.siblings(program.slideboxclass); var _thissibchild = _thissibling.children().children().children(program.slideclass1); var _thissibchildnum = _thissibchild.length; var _thissibchildbd = _thissibling.children(program.bigimgslidebd); var _thissibchildbdw = program.bigimgwidth - 2*program.bilateral; //滚动层父级宽度 var _thissibchildcon = _thissibling.children().children(); var _thissibchildw = (_thissibchildbdw / program.slidenum) - 2*program.slidebetween; //图片外层宽度 var _thissibchildconw = _thissibchildw * _thissibchildnum + _thissibchildnum * 2*program.slidebetween; //滚动层宽度 _thissibchild.append(""); //小图页码 //css样式设置 $(_thissibchild).css({ "float":"left", "display":"inline", "width":_thissibchildw, "overflow":"hidden", "position":"relative", "margin":"0" + "" + program.slidebetween +"px" }); $(_thissibchild).find(".bigimgpage").css({ "display": "block", "line-height": "20px", "padding-left": "5px", "padding-right": "5px", "background": "rgba(0,0,0,0.5)", "color": "#fff", "position": "absolute", "right": "6px", "bottom": "6px", "font-size": "12px" }); $(_thissibchildcon).css({ "width":_thissibchildconw, "overflow":"hidden" }); $(_thissibchildbd).css({ "width":_thissibchildbdw, "overflow":"hidden", "margin":"auto" }); _this.css({ "overflow":"hidden", "position":"relative", "width":program.bigimgwidth, "height":program.bigimgheight, "margin":"auto" }); $(_thissibling).css({ "overflow":"hidden", "position":"relative", "width":program.bigimgwidth, "margin":"auto" }); $(program.prevbtn).css({ "position":"absolute", "left":"0px", "top":"50%", "margin-top":"-25px", "width":"30px", "height":"50px", "cursor": "pointer" }); $(program.nextbtn).css({ "position":"absolute", "right":"0px", "top":"50%", "margin-top":"-25px", "width":"30px", "height":"50px", "cursor": "pointer" }); $(program.slideprevbtn).css({ "position":"absolute", "left":"0px", "top":"50%", "margin-top":"-25px", "width":"30px", "height":"50px", "cursor": "pointer" }); $(program.slidenextbtn).css({ "position":"absolute", "right":"0px", "top":"50%", "margin-top":"-25px", "width":"30px", "height":"50px", "cursor": "pointer" }); var sum = _thischild.length;//大图图片个数 _thischild.hide(); _thischild.eq(i).show(); _thissibchild.eq(i).addclass("active"); //自动播放 function ilunbo() { if(program.autoplay == true){ timer = setinterval(function() { i++; if (i == sum) { i = 0; } startlunbo(); }, program.speed) }else{ return false; } } //鼠标移入暂停播放 $(_this).hover(function(){ clearinterval(timer) },function(){ ilunbo() }) $(_thissibling).hover(function(){ clearinterval(timer) },function(){ ilunbo() }) ilunbo() //图片轮播 function startlunbo() { if (i == sum) { i = 0; } $(_thischild).eq(i).show().siblings().hide(); $(_thissibchild).eq(i).addclass("active").siblings().removeclass("active"); if(_thissibchildnum > program.slidenum){ if(i < _thissibchildnum - num){ $(_thissibchildcon).animate({ "margin-left":( - ($(_thissibchild).outerwidth(true)) * (i - num < 0 ? 0 : (i - num))) }); } else if(i >= _thissibchildnum - num){ $(_thissibchildcon).animate({ "margin-left":( - ($(_thissibchild).outerwidth(true)) * (_thissibchildnum - program.slidenum)) }); } } //回调 _this.find(_thischild).trigger('beforetab',[_this,i,program]); } $(_thissibchild).each(function(){ var thisindex = $(this).index(); $(this).find(".bigimgpage").text((thisindex+1) +"/"+ sum) }); //扩展方法 var mybigimg=$.fn.bigimg; mybigimg.myswitch=function(){ $(program.prevbtn).click(function() { clearinterval(timer); if (i == 0) { i = sum; } i--; startlunbo(); }); $(program.slideprevbtn).click(function() { clearinterval(timer); if (i == 0) { i = sum; } i--; startlunbo(); }); $(program.nextbtn).click(function() { clearinterval(timer); if (i == sum - 1) { i = -1; } i++; startlunbo(); }); $(program.slidenextbtn).click(function() { clearinterval(timer); if (i == sum - 1) { i = -1; } i++; startlunbo(); }); $(_thissibchild).click(function() { clearinterval(timer); i = $(this).index(); startlunbo(); }); } } }); })(jquery);