/** * created by administrator on 2018/1/5. */ ;(function($){ $.fn.timeline = function(opt) { //设置 默认值 var defaults = { w: 0, //每一个li的宽度 n:0, //点击移动的次数 btnprev:"#btnleft", //上一个按钮 btnnext: "#btnright", //下一个按钮 datebox:"#yearlist", //日期的ul盒子 datearr: "#yearlist li", //保存每一个日期的数组 conbox:"#cul", //保存切换内容的ul盒子 conarr:"#cul li" //保存每一个内容的数组 //init:function(){ // var _this=this; //} }; opt = $.extend({}, defaults, opt); return this.each(function() { opt.btnprev=$(opt.btnprev); opt.btnnext=$(opt.btnnext); opt.datebox=$(opt.datebox); opt.datearr=$(opt.datearr); opt.conbox=$(opt.conbox); opt.conarr=$(opt.conarr); var linum=opt.conarr.length; //初始化li的位置 opt.conarr.each(function(i){ $(this).css("left",opt.w*i+"px"); }) //绑定右边点击事件 opt.btnnext.on("click",function(){ if(opt.n=linum)&&(opt.n=0); updata(); } }) //绑定左边点击事件 opt.btnprev.on("click",function(){ opt.n--; (opt.n<0)&&(opt.n=linum-1); updata(); }) function updata(){ opt.conbox.animate({left:-(opt.w*opt.n)+'px'},300); opt.datebox.animate({left:-((200)*opt.n)+'px'},300); $(opt.datearr[opt.n]).addclass("year-active").siblings().removeclass("year-active"); } }); } })(jquery);