1/* 2 * Enhanced Easy Slider 3 * 4 * Built off of the Easy Slider - jQuery plugin 5 * written by Alen Grakalic 6 * http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider 7 * 8 * enhancements by Joseph Pecoraro 9 * email: [email protected] 10 * http://blog.bogojoker.com 11 * 12 * The Easy Slider Copyrights: 13 * Copyright (c) 2009 Alen Grakalic (http://cssglobe.com) 14 * Dual licensed under the MIT (MIT-LICENSE.txt) 15 * and GPL (GPL-LICENSE.txt) licenses. 16 * 17 * Built for jQuery library 18 * http://jquery.com 19 * 20 */ 21 22/* 23 * Quick Documentation of New Features 24 * 25 * * Non-Autogenerated Pagination - set the autogeneratePagination 26 * option to false in the easySlide options and be sure to provide 27 * id's in the prevId and nextId options. Those provided ids will 28 * unobtrusively get the easySlide actions applied to them. 29 * 30 * * Looping - just set the loop option to true in the easySlide 31 * options and you will be able to navigate forward/backward around 32 * the ends smoothly. 33 * 34 * * Autoplay - just set the autoplayDuration to a non-zero value 35 * in the easySlide options and the content will slide on its own. 36 * The value you provide will only be the value between animations, 37 * so you don't have to compensate for the speed of transitions, 38 * that is handled automatically. Autoplaying works fine with 39 * looping. 40 * 41 * If you have pagination buttons, when the user clicks a button 42 * the autoplay will suspend for a little while. The restartDuration 43 * value specifies the amount of time since the last user 44 * interruption to wait before restarting the autoplay. 45 * 46 * * Hovering - just set hover to true and pagination will trigger 47 * when hovering instead of clicking. The slides will continue to 48 * transition as long as you hover over the pagination element. 49 * Hovering works fine with looping. 50 * 51 * If you want a pause between slides you can set the hoverPause 52 * value to an integer greater than 0. That specifies the time 53 * to wait between transitions. 54 * 55 * * Linear or Swing easing - jQuery offers two different styles 56 * of animations. Just set the easing option to 'linear' or 57 * 'swing' (default)and it will use that easing style. 58 * 59 * * Pauseable - Pause autoplay by hovering over an image. Likewise 60 * you can extend this to pause when hovering the buttons as well. 61 * 62 * * Fade Orientation - Not Vertical or Horizontal Slideing, but Fading! 63 * Just provide orientation: 'fade' for fading to happen. 64 * 65 * * Callbacks - You are allowed to provide your own functions for 66 * events triggered by the slider. You can provide a function 67 * for 'beforeTransition' and 'afterTransition'. Their signature 68 * is as follows: 69 * 70 * function callback( slideNumber, jQuery_li, jQuery_ul ) 71 * ^ ^ ^ 72 * | | | 73 * int of the slide number ----- | | 74 * jQuery wrapper for the now showing <li> -- | 75 * jQuery wrapper for the entire <ul> of all slides --- 76 * 77 * For example, to get the explicit DOM <li> Node you could do 78 * 79 * function after(num, $li, $ul) { 80 * dom_li = $li[0]; 81 * console.log(dom_li); 82 * } 83 * 84 */ 85 86(function($) { 87 88 $.fn.easySlider = function(options){ 89 90 // default configuration properties 91 var defaults = { 92 prevId: 'prevBtn', // id of the element to apply prev rules to 93 nextId: 'nextBtn', // id of the element to apply next rules to 94 prevText: 'Previous', // text for autogenerated prev link 95 nextText: 'Next', // text for autogenerated next link 96 autogeneratePagination: false, // will automatically generate pagination links 97 orientation: '', // 'vertical', 'fade', or anything else will assume horizontal
98 speed: 800, // duration of a transition 99 autoplayDuration: 0, // auto-play if non-zero, this is the time between transitions 100 restartDuration: 2500, // time to restart autoplay after a user interupts and autoplay 101 loop: false, // loop around the content 102 hoverPause: 0, // when hovering, the time between transitions 103 hover: false, // on hover instead of onclick 104 easing: null, // jQuery animation settings 'linear' or 'swing' (default) 105 pauseable: false, // pause autoplay when hovering the image 106 pauseButtons: false, // hovering over the cuttons will pause as well 107 beforeTransition: null, // callback before transitioning 108 afterTransition: null // callback after transitioning 109 }; 110 111 var options = $.extend(defaults, options); 112 113 return this.each(function() { 114 115 // Setup Measurements and Options 116 var obj = $(this), 117 totalSlides = $("li", obj).length, 118 slideWidth = obj.width(), 119 slideHeight = obj.height(), 120 lastSlide = totalSlides-1, 121 current = 0; 122 123 // States 124 var vertical = (options.orientation == 'vertical'), 125 fade = (options.orientation == 'fade'), 126 horizontal = (!vertical && !fade), 127 autogen = options.autogeneratePagination, 128 pauseable = options.pauseable, 129 loop = options.loop, 130 speed = options.speed, 131 easing = options.easing, 132 hover = options.hover, 133 hoverPause = options.hoverPause, 134 buttonsPause = options.pauseButtons, 135 restartDuration = options.restartDuration, 136 autoplayDuration = Math.max(options.autoplayDuration,25)+speed, 137 autoplay = (options.autoplayDuration > 0), 138 beforeTransition = options.beforeTransition, 139 afterTransition = options.afterTransition; 140 interval = null, 141 restart = null; 142 143 // Autogenerate Pagination 144 if (autogen) { 145 $(obj).after( '<span id="'+options.prevId+'"><a href=\"javascript:void(0);\">'+options.prevText+'</a></span> '+ 146 '<span id="'+options.nextId+'"><a href=\"javascript:void(0);\">'+options.nextText+'</a></span>'); 147 } 148 149 // Important Elements 150 var $ul = $("ul", obj), 151 $next = $("#"+options.nextId), 152 $prev = $("#"+options.prevId); 153 154 // Loop - Duplicate the First Slide onto the end 155 if (loop) { 156 $ul.append( $("li:first", obj).clone() ); 157 totalSlides += 1; 158 lastSlide += 1; 159 } 160 161 // Horizontal - Make them float left and set the width of the ul (for all slides) 162 if (horizontal) { 163 $("li", obj).css('float','left'); 164 $ul.css('width', totalSlides * slideWidth); 165 } 166 167 // Fade - Hide all but the first slide so they can fadeIn from nothing 168 if (fade) { 169 $ul.find("li:not(:first)").hide(); 170 } 171 172 // Reusable Closure to pause the autoplay timer 173 var pauseFunc = function() { 174 clearInterval(interval); 175 clearTimeout(restart); 176 } 177 178 // Reusable Closure to restart the autoplay timers using the restartDuration 179 var restartFunc = function() { 180 restart = setTimeout(function() { 181 interval = setInterval(auto, autoplayDuration); 182 }, restartDuration); 183 }; 184 185 // Pauseable (should be autoplay only, but would have no effect otherwise) 186 // On Hover: cancel the autoplay timers 187 // On Leave: restart the autoplay timers 188 if ( pauseable && autoplay ) { 189 $(obj).hover(pauseFunc, restartFunc); 190 if ( buttonsPause ) { 191 $next.hover(pauseFunc, restartFunc); 192 $prev.hover(pauseFunc, restartFunc); 193 } 194 } 195 196 // Add Actions to Next and Prev Buttons 197 // If Not Hover Mode made it click(), if it is Hover Mode, make it hover() 198 // When in autoplay mode make next/prev reset the autoplay timers 199 if ( !hover ) { 200 201 $next.click(function(){ 202 if (autoplay) { 203 pauseFunc(); 204 if ( !buttonsPause ) { 205 restartFunc(); 206 } 207 }; 208 animate("next"); 209 }); 210 211 $prev.click(function(){ 212 if (autoplay) { 213 pauseFunc(); 214 if ( !buttonsPause ) { 215 restartFunc(); 216 } 217 }; 218 animate("prev"); 219 }); 220 221 } else { 222 223 var hoverNext = false, 224 hoverPrev = false, 225 isAnimating = false; 226 227 $next.hover( 228 function() { 229 var tfunc = arguments.callee; 230 hoverNext = true; 231 if ( !isAnimating ) { 232 isAnimating = true; 233 animate("next", function() { 234 isAnimating = false; 235 if ( hoverNext ) { tfunc(); } 236 }); 237 } 238 }, 239 function() { 240 hoverNext = false; 241 } 242 ); 243 244 $prev.hover( 245 function() { 246 var tfunc = arguments.callee; 247 hoverPrev = true; 248 if ( !isAnimating ) { 249 isAnimating = true; 250 animate("prev", function() { 251 isAnimating = false; 252 if ( hoverPrev ) { tfunc(); } 253 }); 254 } 255 }, 256 function() { 257 hoverPrev = false; 258 } 259 ); 260 261 } 262 263 264 // 265 // Enclosed Slide Animation 266 // 267 // This handles all of the sliding/faiding animation 268 // @param dir the direction "next" or "prev" 269 // @param cb the callback function 270 // 271 function animate(dir, cb) { 272 273 // Before Transition, Guarantee 274 if ( beforeTransition ) { 275 beforeTransition( current, $ul.find('li:eq('+current+')'), $ul ); 276 } 277 278 279 // After Transition, Guarantee 280 // Setup the next have 281 // NOTE: That current will be the new value when this is called 282 // due to it being a closure! 283 var newcb = function() { 284 if ( cb != null ) { 285 ( hoverPause == 0 ) ? cb() : setTimeout(cb, hoverPause); 286 } 287 if ( afterTransition ) { 288 afterTransition( current, $ul.find('li:eq('+current+')'), $ul ); 289 } 290 } 291 292 293 // cur, nex, and update "current" 294 // cur is the old current slide, the slide we are currently on 295 // nex is the new current slide, the slide we are transitioning too 296 // current is a global value that gets updated right here 297 var cur = current; 298 if ( loop ) { 299 if ( dir == 'next' ) { 300 current = (current==lastSlide) ? 1 : current+1; 301 } else { 302 current = (current==0) ? lastSlide-1 : current-1; 303 } 304 } else { 305 if (dir == "next") { 306 current = (current>=lastSlide) ? lastSlide : current+1; 307 } else { 308 current = (current<=0) ? 0 : current-1; 309 } 310 } 311 var nex = current; 312 313 314 // Special Case for looping 315 // If at the end and going forward, instantaneous jump to the first slide 316 // If at the start and going backward, instantaneous jump to the last slide 317 // NOTE: The instantaneous jump is an animate() so jQuery handles it in tune 318 // with the rest of the animations, and is -1 (not 0) time so its instant. 319 if ( loop ) { 320 if ( (dir == "next") && (cur == lastSlide) ) { 321 (vertical) ? 322 $ul.animate({marginTop:0}, -1) : 323 $ul.animate({marginLeft:0}, -1);
324 } else if ( (dir == "prev") && (cur == 0) ) { 325 (vertical) ? 326 $ul.animate({marginTop:(totalSlides-1)*slideHeight*-1}, -1) : 327 $ul.animate({marginLeft:(totalSlides-1)*slideWidth*-1}, -1); 328 } 329 } 330 331 332 // Dispatch jQuery Animation for the transition 333 // Slide horizontally, vertically, or fade 334 if ( horizontal ) { 335 $ul.animate( { marginLeft: (nex*slideWidth*-1) }, speed, easing, newcb ); 336 } else if ( vertical ) { 337 $ul.animate( { marginTop: (nex*slideHeight*-1) }, speed, easing, newcb ); 338 } else { 339 var curli = 'li:eq(' + cur + ')'; 340 var nexli = 'li:eq(' + nex + ')'; 341 $ul.find(curli).fadeOut("slow", function() { 342 $ul.find(nexli).fadeIn("slow", newcb); 343 }); 344 } 345 346 347 // Correctly show the Next/Prev links 348 // When looping they always show, so we can ignore that 349 // If at the end, hide the next link 350 // If at the start, hide the previous link 351 // Any other case we can show both links 352 if ( !loop ) { 353 if ( nex <= 0 ) { 354 $prev.fadeOut(); 355 } else if ( nex >= lastSlide ) { 356 $next.fadeOut(); 357 } else { 358 $next.fadeIn(); 359 $prev.fadeIn(); 360 } 361 } 362 363 }; 364 365 366 // 367 // Autoplay 368 // 369 // Note that autoplayDuration already has the transitionDuration 370 // added to it (to prevent doing the addition over and over). 371 // However that means that the first invokation would take 372 // autoplayDuration+transitionDuration. We remove that extra 373 // wait by first doing a single setTimeout with the correct 374 // time, followed by the correct setInterval. 375 // 376 if ( autoplay ) { 377 var auto = function() { 378 animate('next'); 379 if ( !loop && current>=lastSlide ) { 380 clearInterval(interval); 381 } 382 } 383 restart = setTimeout(function() { 384 auto(); 385 interval = setInterval(auto, autoplayDuration); 386 }, autoplayDuration-speed); 387 } 388 389 390 // Initially Hide both buttons 391 $next.hide(); 392 $prev.hide(); 393 394 // Finally, Display the Next Link if more then one slide 395 if (totalSlides>1) { 396 $next.fadeIn(); 397 if(loop) { $prev.fadeIn(); } 398 } 399 400 }); 401 402 }; 403 404})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.