/bin/html/javascripts/jquery.cycle.lite.js
JavaScript | 232 lines | 186 code | 30 blank | 16 comment | 57 complexity | 867e99d3b99e34e1163fced0c75586c2 MD5 | raw file
1/*! 2 * jQuery Cycle Lite Plugin 3 * http://malsup.com/jquery/cycle/lite/ 4 * Copyright (c) 2008-2012 M. Alsup 5 * Version: 1.6 (02-MAY-2012) 6 * Dual licensed under the MIT and GPL licenses: 7 * http://www.opensource.org/licenses/mit-license.php 8 * http://www.gnu.org/licenses/gpl.html 9 * Requires: jQuery v1.3.2 or later 10 */ 11;(function($) { 12"use strict"; 13 14var ver = 'Lite-1.6'; 15 16$.fn.cycle = function(options) { 17 return this.each(function() { 18 options = options || {}; 19 20 if (this.cycleTimeout) clearTimeout(this.cycleTimeout); 21 22 this.cycleTimeout = 0; 23 this.cyclePause = 0; 24 25 var $cont = $(this); 26 var $slides = options.slideExpr ? $(options.slideExpr, this) : $cont.children(); 27 var els = $slides.get(); 28 if (els.length < 2) { 29 if (window.console) 30 console.log('terminating; too few slides: ' + els.length); 31 return; // don't bother 32 } 33 34 // support metadata plugin (v1.0 and v2.0) 35 var opts = $.extend({}, $.fn.cycle.defaults, options || {}, $.metadata ? $cont.metadata() : $.meta ? $cont.data() : {}); 36 var meta = $.isFunction($cont.data) ? $cont.data(opts.metaAttr) : null; 37 if (meta) 38 opts = $.extend(opts, meta); 39 40 opts.before = opts.before ? [opts.before] : []; 41 opts.after = opts.after ? [opts.after] : []; 42 opts.after.unshift(function(){ opts.busy=0; }); 43 44 // allow shorthand overrides of width, height and timeout 45 var cls = this.className; 46 opts.width = parseInt((cls.match(/w:(\d+)/)||[])[1], 10) || opts.width; 47 opts.height = parseInt((cls.match(/h:(\d+)/)||[])[1], 10) || opts.height; 48 opts.timeout = parseInt((cls.match(/t:(\d+)/)||[])[1], 10) || opts.timeout; 49 50 if ($cont.css('position') == 'static') 51 $cont.css('position', 'relative'); 52 if (opts.width) 53 $cont.width(opts.width); 54 if (opts.height && opts.height != 'auto') 55 $cont.height(opts.height); 56 57 var first = 0; 58 $slides.css({position: 'absolute', top:0}).each(function(i) { 59 $(this).css('z-index', els.length-i); 60 }); 61 62 $(els[first]).css('opacity',1).show(); // opacity bit needed to handle reinit case 63 if ($.browser.msie) els[first].style.removeAttribute('filter'); 64 65 if (opts.fit && opts.width) 66 $slides.width(opts.width); 67 if (opts.fit && opts.height && opts.height != 'auto') 68 $slides.height(opts.height); 69 if (opts.pause) 70 $cont.hover(function(){this.cyclePause=1;}, function(){this.cyclePause=0;}); 71 72 var txFn = $.fn.cycle.transitions[opts.fx]; 73 if (txFn) 74 txFn($cont, $slides, opts); 75 76 $slides.each(function() { 77 var $el = $(this); 78 this.cycleH = (opts.fit && opts.height) ? opts.height : $el.height(); 79 this.cycleW = (opts.fit && opts.width) ? opts.width : $el.width(); 80 }); 81 82 if (opts.cssFirst) 83 $($slides[first]).css(opts.cssFirst); 84 85 if (opts.timeout) { 86 // ensure that timeout and speed settings are sane 87 if (opts.speed.constructor == String) 88 opts.speed = {slow: 600, fast: 200}[opts.speed] || 400; 89 if (!opts.sync) 90 opts.speed = opts.speed / 2; 91 while((opts.timeout - opts.speed) < 250) 92 opts.timeout += opts.speed; 93 } 94 opts.speedIn = opts.speed; 95 opts.speedOut = opts.speed; 96 97 opts.slideCount = els.length; 98 opts.currSlide = first; 99 opts.nextSlide = 1; 100 101 // fire artificial events 102 var e0 = $slides[first]; 103 if (opts.before.length) 104 opts.before[0].apply(e0, [e0, e0, opts, true]); 105 if (opts.after.length > 1) 106 opts.after[1].apply(e0, [e0, e0, opts, true]); 107 108 if (opts.click && !opts.next) 109 opts.next = opts.click; 110 if (opts.next) 111 $(opts.next).unbind('click.cycle').bind('click.cycle', function(){return advance(els,opts,opts.rev?-1:1);}); 112 if (opts.prev) 113 $(opts.prev).unbind('click.cycle').bind('click.cycle', function(){return advance(els,opts,opts.rev?1:-1);}); 114 115 if (opts.timeout) 116 this.cycleTimeout = setTimeout(function() { 117 go(els,opts,0,!opts.rev); 118 }, opts.timeout + (opts.delay||0)); 119 }); 120}; 121 122function go(els, opts, manual, fwd) { 123 if (opts.busy) 124 return; 125 var p = els[0].parentNode, curr = els[opts.currSlide], next = els[opts.nextSlide]; 126 if (p.cycleTimeout === 0 && !manual) 127 return; 128 129 if (manual || !p.cyclePause) { 130 if (opts.before.length) 131 $.each(opts.before, function(i,o) { o.apply(next, [curr, next, opts, fwd]); }); 132 var after = function() { 133 if ($.browser.msie) 134 this.style.removeAttribute('filter'); 135 $.each(opts.after, function(i,o) { o.apply(next, [curr, next, opts, fwd]); }); 136 queueNext(opts); 137 }; 138 139 if (opts.nextSlide != opts.currSlide) { 140 opts.busy = 1; 141 $.fn.cycle.custom(curr, next, opts, after); 142 } 143 var roll = (opts.nextSlide + 1) == els.length; 144 opts.nextSlide = roll ? 0 : opts.nextSlide+1; 145 opts.currSlide = roll ? els.length-1 : opts.nextSlide-1; 146 } else { 147 queueNext(opts); 148 } 149 150 function queueNext(opts) { 151 if (opts.timeout) 152 p.cycleTimeout = setTimeout(function() { go(els,opts,0,!opts.rev); }, opts.timeout); 153 } 154} 155 156// advance slide forward or back 157function advance(els, opts, val) { 158 var p = els[0].parentNode, timeout = p.cycleTimeout; 159 if (timeout) { 160 clearTimeout(timeout); 161 p.cycleTimeout = 0; 162 } 163 opts.nextSlide = opts.currSlide + val; 164 if (opts.nextSlide < 0) { 165 opts.nextSlide = els.length - 1; 166 } 167 else if (opts.nextSlide >= els.length) { 168 opts.nextSlide = 0; 169 } 170 go(els, opts, 1, val>=0); 171 return false; 172} 173 174$.fn.cycle.custom = function(curr, next, opts, cb) { 175 var $l = $(curr), $n = $(next); 176 $n.css(opts.cssBefore); 177 var fn = function() {$n.animate(opts.animIn, opts.speedIn, opts.easeIn, cb);}; 178 $l.animate(opts.animOut, opts.speedOut, opts.easeOut, function() { 179 $l.css(opts.cssAfter); 180 if (!opts.sync) 181 fn(); 182 }); 183 if (opts.sync) 184 fn(); 185}; 186 187$.fn.cycle.transitions = { 188 fade: function($cont, $slides, opts) { 189 $slides.not(':eq(0)').hide(); 190 opts.cssBefore = { opacity: 0, display: 'block' }; 191 opts.cssAfter = { display: 'none' }; 192 opts.animOut = { opacity: 0 }; 193 opts.animIn = { opacity: 1 }; 194 }, 195 fadeout: function($cont, $slides, opts) { 196 opts.before.push(function(curr,next,opts,fwd) { 197 $(curr).css('zIndex',opts.slideCount + (fwd === true ? 1 : 0)); 198 $(next).css('zIndex',opts.slideCount + (fwd === true ? 0 : 1)); 199 }); 200 $slides.not(':eq(0)').hide(); 201 opts.cssBefore = { opacity: 1, display: 'block', zIndex: 1 }; 202 opts.cssAfter = { display: 'none', zIndex: 0 }; 203 opts.animOut = { opacity: 0 }; 204 opts.animIn = { opacity: 1 }; 205 } 206}; 207 208$.fn.cycle.ver = function() { return ver; }; 209 210// @see: http://malsup.com/jquery/cycle/lite/ 211$.fn.cycle.defaults = { 212 animIn: {}, 213 animOut: {}, 214 fx: 'fade', 215 after: null, 216 before: null, 217 cssBefore: {}, 218 cssAfter: {}, 219 delay: 0, 220 fit: 0, 221 height: 'auto', 222 metaAttr: 'cycle', 223 next: null, 224 pause: false, 225 prev: null, 226 speed: 1000, 227 slideExpr: null, 228 sync: true, 229 timeout: 4000 230}; 231 232})(jQuery);