/** * slider插件可悬停控制 */ ; $(function ($, window, document, undefined) { slider = function (container, options) { /* options = { auto: true, time: 3000, event: 'hover' | 'click', mode: 'slide | fade', controller: $(), activecontrollercls: 'classname', exchangeend: $.noop } */ "use strict"; //stirct mode not support by ie9- if (!container) return; var options = options || {}, currentindex = 0, cls = options.activecontrollercls, delay = options.delay, isauto = options.auto, controller = options.controller, event = options.event, interval, slideswrapper = container.children().first(), slides = slideswrapper.children(), length = slides.length, childwidth = container.width(), totalwidth = childwidth * slides.length; function init() { var controlitem = controller.children(); mode(); event == 'hover' ? controlitem.mouseover(function () { stop(); var index = $(this).index(); play(index, options.mode); }).mouseout(function () { isauto && autoplay(); }) : controlitem.click(function () { stop(); var index = $(this).index(); play(index, options.mode); isauto && autoplay(); }); isauto && autoplay(); } //animate mode function mode() { var wrapper = container.children().first(); options.mode == 'slide' ? wrapper.width(totalwidth) : wrapper.children().css({ 'position': 'absolute', 'left': 0, 'top': 0 }) .first().siblings().hide(); } //auto play function autoplay() { interval = setinterval(function () { triggerplay(currentindex); }, options.time); } //trigger play function triggerplay(cindex) { var index; (cindex == length - 1) ? index = 0 : index = cindex + 1; play(index, options.mode); } //play function play(index, mode) { slideswrapper.stop(true, true); slides.stop(true, true); mode == 'slide' ? (function () { if (index > currentindex) { slideswrapper.animate({ left: '-=' + math.abs(index - currentindex) * childwidth + 'px' }, delay); } else if (index < currentindex) { slideswrapper.animate({ left: '+=' + math.abs(index - currentindex) * childwidth + 'px' }, delay); } else { return; } })() : (function () { if (slideswrapper.children(':visible').index() == index) return; slideswrapper.children().fadeout(delay).eq(index).fadein(delay); })(); try { controller.children('.' + cls).removeclass(cls); controller.children().eq(index).addclass(cls); } catch (e) { } currentindex = index; options.exchangeend && typeof options.exchangeend == 'function' && options.exchangeend.call(this, currentindex); } //stop function stop() { clearinterval(interval); } //prev frame function prev() { stop(); currentindex == 0 ? triggerplay(length - 2) : triggerplay(currentindex - 2); isauto && autoplay(); } //next frame function next() { stop(); currentindex == length - 1 ? triggerplay(-1) : triggerplay(currentindex); isauto && autoplay(); } //init init(); //expose the slider api return { prev: function () { prev(); }, next: function () { next(); } } }; }(jquery, window, document));