/* * ***juery焦点图切换插件*** * 作者: xukai.json * mit license. * 版本: v2.0 * 文件名: super_slider.js * 日期: 2015-08-31 * 使用说明: 利用函数传参调用 $(".box").superslider({//选择器必须是插件最外层盒子class或id,包含上下按钮和轮播内容 prevbtn: ".prevbtn",//上一页按钮 nextbtn: ".nextbtn",//下一页按钮 listcont: ".content",//滚动列表外层 delaytime: 1000,//自动轮播时间间隔,默认1秒 speed: 1000,//滚动速度,默认1秒/次 amount: 1,//单次滚动数量,默认1个 shownum: 4,//默认显示个数,默认4个 autoplay: false///是否自动自动轮播,默认否,若要自动轮播请设置成true scrollwhere: "prev",//自动滚动方向,默然是上,下的参数为 next }); */ (function ($){ $.fn.superslider = function (options){ var _this = $(this);//模块最外层盒子 //默认参数 var defaults = { prevbtn: ".prevbtn",//上一页按钮 nextbtn: ".nextbtn",//下一页按钮 listcont: ".content",//滚动列表外层 delaytime: 1000,//自动轮播时间间隔,默认1秒 speed: 1000,//滚动速度,默认1秒/次 amount: 1,//单次滚动数量,默认1个 shownum: 4,//默认显示个数,默认4个 autoplay: false,///是否自动自动轮播,默认否,若要自动轮播请设置成true scrollwhere: "prev"//自动滚动方向,默然是上,下的参数为 next }; var opts = $.extend(defaults, options); //缓存后续常用全局变量 var gb = { cont: _this.find(opts.listcont),//轮播内容区域 prevbtn: _this.find(opts.prevbtn),//上一页按钮 nextbtn: _this.find(opts.nextbtn),//下一页按钮 time: opts.delaytime,//延迟时间 speed: opts.speed,//自动轮播速度 onecontwidth: _this.find(opts.listcont).children().eq(0).outerwidth(true),//单个内容宽度 n: opts.amount,//单次滚动数量 len: _this.find(opts.listcont).children().length,//轮播子元素数量 contwidth: _this.find(opts.listcont).children().length*_this.find(opts.listcont).children().eq(0).outerwidth(true),//元素总宽 shownum: opts.shownum//显示个数 }; //动画对象 var slider = { scrolllnext: function (){ if(!gb.cont.is(":animated")){ gb.cont.children().slice(0,gb.n).clone().appendto(gb.cont); gb.cont.width(gb.cont.children().length*gb.onecontwidth+'px');//设置轮播内容的宽度 gb.cont.animate({ "margin-left": "-"+gb.onecontwidth*gb.n+"px" }, gb.speed, function() { $(this).children().slice(0,gb.n).remove(); $(this).css('margin-left', '0px'); }); }else{ //cont.stop(true,true); } }, scrollprev: function (){ if(!gb.cont.is(":animated")){ gb.cont.children().slice(gb.len-gb.n,gb.len).clone().prependto(gb.cont); gb.cont.css({ "margin-left": "-"+gb.onecontwidth*gb.n+"px", "width": gb.cont.children().length*gb.onecontwidth+"px" }); gb.cont.animate({ "margin-left": "0px" }, gb.speed, function() { $(this).children().slice((gb.cont.children().length - gb.n),gb.cont.children().length).remove(); }); } }, init: function (){ //一点点样式的初始化,为了增加插件的易用性 gb.cont.parent().css({ "overflow": "hidden", "width": gb.shownum*gb.onecontwidth });//为父级设置超出隐藏 gb.cont.width(gb.contwidth+'px');//设置轮播内容的宽度 //判断如果多出显示个数则加滚屏效果 if(gb.cont.children().length <= gb.shownum){ gb.prevbtn.hide(); gb.nextbtn.hide(); } else{ gb.prevbtn.show(); gb.nextbtn.show(); gb.prevbtn.off().on('click',this.scrollprev); gb.nextbtn.off().on('click',this.scrolllnext); } if(opts.autoplay === true){ if(opts.scrollwhere === "prev"){ var fn = this.scrollprev; } else{ var fn = this.scrolllnext; } var t = opts.delaytime + opts.speed; var timer = setinterval(fn, t); $(opts.listcont+","+opts.prevbtn+","+opts.nextbtn).hover(function() { clearinterval(timer); }, function() { timer = setinterval(fn, t); }); } } } slider.init(); } })(jquery);