(function($){ $.fn.ckslide = function(opts){ opts = $.extend({}, $.fn.ckslide.opts, opts); this.each(function(){ var slidewrap = $(this).find('.ck-slide-wrapper'); var slide = slidewrap.find('li'); var count = slide.length; var that = this; var index = 0; var time = null; $(this).data('opts', opts); // next $(this).find('.ck-next').on('click', function(){ if(opts['isanimate'] == true){ return; } var old = index; if(index >= count - 1){ index = 0; }else{ index++; } change.call(that, index, old); }); // prev $(this).find('.ck-prev').on('click', function(){ if(opts['isanimate'] == true){ return; } var old = index; if(index <= 0){ index = count - 1; }else{ index--; } change.call(that, index, old); }); $(this).find('.ck-slidebox li').each(function(cindex){ $(this).on('click.slidebox', function(){ change.call(that, cindex, index); index = cindex; }); }); // focus clean auto play $(this).on('mouseover', function(){ if(opts.autoplay){ clearinterval(time); } $(this).find('.ctrl-slide').css({opacity:0.6}); }); // leave $(this).on('mouseleave', function(){ if(opts.autoplay){ startatuoplay(); } $(this).find('.ctrl-slide').css({opacity:0.15}); }); startatuoplay(); // auto play function startatuoplay(){ if(opts.autoplay){ time = setinterval(function(){ var old = index; if(index >= count - 1){ index = 0; }else{ index++; } change.call(that, index, old); }, 5000); } } // 修正box var box = $(this).find('.ck-slidebox'); box.css({ 'margin-left':-(box.width() / 2) }) // dir switch(opts.dir){ case "x": opts['width'] = $(this).width(); slidewrap.css({ 'width':count * opts['width'] }); slide.css({ 'float':'left', 'position':'relative' }); slidewrap.wrap('
'); slide.show(); break; } }); }; function change(show, hide){ var opts = $(this).data('opts'); if(opts.dir == 'x'){ var x = show * opts['width']; $(this).find('.ck-slide-wrapper').stop().animate({'margin-left':-x}, function(){opts['isanimate'] = false;}); opts['isanimate'] = true }else{ $(this).find('.ck-slide-wrapper li').eq(hide).stop().animate({opacity:0}, function(){$(this).css({'visibility':'hidden'})}); $(this).find('.ck-slide-wrapper li').eq(show).show().css({opacity:0, 'visibility':'visible'}).stop().animate({opacity:1}); } $(this).find('.ck-slidebox li').removeclass('current'); $(this).find('.ck-slidebox li').eq(show).addclass('current'); } $.fn.ckslide.opts = { autoplay: false, dir: null, isanimate: false }; })(jquery);