/* * jquery nivo slider v3.2 * http://nivo.dev7studios.com * * copyright 2012, dev7studios * free to use and abuse under the mit license. * http://www.opensource.org/licenses/mit-license.php */ (function($) { var nivoslider = function(element, options){ // defaults are below var settings = $.extend({}, $.fn.nivoslider.defaults, options); // useful variables. play carefully. var vars = { currentslide: 0, currentimage: '', totalslides: 0, running: false, paused: false, stop: false, controlnavel: false }; // get this slider var slider = $(element); slider.data('nivo:vars', vars).addclass('nivoslider'); // find our slider children var kids = slider.children(); kids.each(function() { var child = $(this); var link = ''; if(!child.is('img')){ if(child.is('a')){ child.addclass('nivo-imagelink'); link = child; } child = child.find('img:first'); } // get img width & height var childwidth = (childwidth === 0) ? child.attr('width') : child.width(), childheight = (childheight === 0) ? child.attr('height') : child.height(); if(link !== ''){ link.css('display','none'); } child.css('display','none'); vars.totalslides++; }); // if randomstart if(settings.randomstart){ settings.startslide = math.floor(math.random() * vars.totalslides); } // set startslide if(settings.startslide > 0){ if(settings.startslide >= vars.totalslides) { settings.startslide = vars.totalslides - 1; } vars.currentslide = settings.startslide; } // get initial image if($(kids[vars.currentslide]).is('img')){ vars.currentimage = $(kids[vars.currentslide]); } else { vars.currentimage = $(kids[vars.currentslide]).find('img:first'); } // show initial link if($(kids[vars.currentslide]).is('a')){ $(kids[vars.currentslide]).css('display','block'); } // set first background var sliderimg = $('').addclass('nivo-main-image'); sliderimg.attr('src', vars.currentimage.attr('src')).show(); slider.append(sliderimg); // detect window resize $(window).resize(function() { slider.children('img').width(slider.width()); sliderimg.attr('src', vars.currentimage.attr('src')); sliderimg.stop().height('auto'); $('.nivo-slice').remove(); $('.nivo-box').remove(); }); //create caption slider.append($('
')); // process caption function var processcaption = function(settings){ var nivocaption = $('.nivo-caption', slider); if(vars.currentimage.attr('title') != '' && vars.currentimage.attr('title') != undefined){ var title = vars.currentimage.attr('title'); if(title.substr(0,1) == '#') title = $(title).html(); if(nivocaption.css('display') == 'block'){ settimeout(function(){ nivocaption.html(title); }, settings.animspeed); } else { nivocaption.html(title); nivocaption.stop().fadein(settings.animspeed); } } else { nivocaption.stop().fadeout(settings.animspeed); } } //process initial caption processcaption(settings); // in the words of super mario "let's a go!" var timer = 0; if(!settings.manualadvance && kids.length > 1){ timer = setinterval(function(){ nivorun(slider, kids, settings, false); }, settings.pausetime); } // add direction nav if(settings.directionnav){ slider.append(''); $(slider).on('click', 'a.nivo-prevnav', function(){ if(vars.running) { return false; } clearinterval(timer); timer = ''; vars.currentslide -= 2; nivorun(slider, kids, settings, 'prev'); }); $(slider).on('click', 'a.nivo-nextnav', function(){ if(vars.running) { return false; } clearinterval(timer); timer = ''; nivorun(slider, kids, settings, 'next'); }); } // add control nav if(settings.controlnav){ vars.controlnavel = $(''); slider.after(vars.controlnavel); for(var i = 0; i < kids.length; i++){ if(settings.controlnavthumbs){ vars.controlnavel.addclass('nivo-thumbs-enabled'); var child = kids.eq(i); if(!child.is('img')){ child = child.find('img:first'); } if(child.attr('data-thumb')) vars.controlnavel.append(''); } else { vars.controlnavel.append(''+ (i + 1) +''); } } //set initial active link $('a:eq('+ vars.currentslide +')', vars.controlnavel).addclass('active'); $('a', vars.controlnavel).bind('click', function(){ if(vars.running) return false; if($(this).hasclass('active')) return false; clearinterval(timer); timer = ''; sliderimg.attr('src', vars.currentimage.attr('src')); vars.currentslide = $(this).attr('rel') - 1; nivorun(slider, kids, settings, 'control'); }); } //for pauseonhover setting if(settings.pauseonhover){ slider.hover(function(){ vars.paused = true; clearinterval(timer); timer = ''; }, function(){ vars.paused = false; // restart the timer if(timer === '' && !settings.manualadvance){ timer = setinterval(function(){ nivorun(slider, kids, settings, false); }, settings.pausetime); } }); } // event when animation finishes slider.bind('nivo:animfinished', function(){ sliderimg.attr('src', vars.currentimage.attr('src')); vars.running = false; // hide child links $(kids).each(function(){ if($(this).is('a')){ $(this).css('display','none'); } }); // show current link if($(kids[vars.currentslide]).is('a')){ $(kids[vars.currentslide]).css('display','block'); } // restart the timer if(timer === '' && !vars.paused && !settings.manualadvance){ timer = setinterval(function(){ nivorun(slider, kids, settings, false); }, settings.pausetime); } // trigger the afterchange callback settings.afterchange.call(this); }); // add slices for slice animations var createslices = function(slider, settings, vars) { if($(vars.currentimage).parent().is('a')) $(vars.currentimage).parent().css('display','block'); $('img[src="'+ vars.currentimage.attr('src') +'"]', slider).not('.nivo-main-image,.nivo-control img').width(slider.width()).css('visibility', 'hidden').show(); var sliceheight = ($('img[src="'+ vars.currentimage.attr('src') +'"]', slider).not('.nivo-main-image,.nivo-control img').parent().is('a')) ? $('img[src="'+ vars.currentimage.attr('src') +'"]', slider).not('.nivo-main-image,.nivo-control img').parent().height() : $('img[src="'+ vars.currentimage.attr('src') +'"]', slider).not('.nivo-main-image,.nivo-control img').height(); for(var i = 0; i < settings.slices; i++){ var slicewidth = math.round(slider.width()/settings.slices); if(i === settings.slices-1){ slider.append( $('').css({ left:(slicewidth*i)+'px', width:(slider.width()-(slicewidth*i))+'px', height:sliceheight+'px', opacity:'0', overflow:'hidden' }) ); } else { slider.append( $('').css({ left:(slicewidth*i)+'px', width:slicewidth+'px', height:sliceheight+'px', opacity:'0', overflow:'hidden' }) ); } } $('.nivo-slice', slider).height(sliceheight); sliderimg.stop().animate({ height: $(vars.currentimage).height() }, settings.animspeed); }; // add boxes for box animations var createboxes = function(slider, settings, vars){ if($(vars.currentimage).parent().is('a')) $(vars.currentimage).parent().css('display','block'); $('img[src="'+ vars.currentimage.attr('src') +'"]', slider).not('.nivo-main-image,.nivo-control img').width(slider.width()).css('visibility', 'hidden').show(); var boxwidth = math.round(slider.width()/settings.boxcols), boxheight = math.round($('img[src="'+ vars.currentimage.attr('src') +'"]', slider).not('.nivo-main-image,.nivo-control img').height() / settings.boxrows); for(var rows = 0; rows < settings.boxrows; rows++){ for(var cols = 0; cols < settings.boxcols; cols++){ if(cols === settings.boxcols-1){ slider.append( $('').css({ opacity:0, left:(boxwidth*cols)+'px', top:(boxheight*rows)+'px', width:(slider.width()-(boxwidth*cols))+'px' }) ); $('.nivo-box[name="'+ cols +'"]', slider).height($('.nivo-box[name="'+ cols +'"] img', slider).height()+'px'); } else { slider.append( $('').css({ opacity:0, left:(boxwidth*cols)+'px', top:(boxheight*rows)+'px', width:boxwidth+'px' }) ); $('.nivo-box[name="'+ cols +'"]', slider).height($('.nivo-box[name="'+ cols +'"] img', slider).height()+'px'); } } } sliderimg.stop().animate({ height: $(vars.currentimage).height() }, settings.animspeed); }; // private run method var nivorun = function(slider, kids, settings, nudge){ // get our vars var vars = slider.data('nivo:vars'); // trigger the lastslide callback if(vars && (vars.currentslide === vars.totalslides - 1)){ settings.lastslide.call(this); } // stop if((!vars || vars.stop) && !nudge) { return false; } // trigger the beforechange callback settings.beforechange.call(this); // set current background before change if(!nudge){ sliderimg.attr('src', vars.currentimage.attr('src')); } else { if(nudge === 'prev'){ sliderimg.attr('src', vars.currentimage.attr('src')); } if(nudge === 'next'){ sliderimg.attr('src', vars.currentimage.attr('src')); } } vars.currentslide++; // trigger the slideshowend callback if(vars.currentslide === vars.totalslides){ vars.currentslide = 0; settings.slideshowend.call(this); } if(vars.currentslide < 0) { vars.currentslide = (vars.totalslides - 1); } // set vars.currentimage if($(kids[vars.currentslide]).is('img')){ vars.currentimage = $(kids[vars.currentslide]); } else { vars.currentimage = $(kids[vars.currentslide]).find('img:first'); } // set active links if(settings.controlnav){ $('a', vars.controlnavel).removeclass('active'); $('a:eq('+ vars.currentslide +')', vars.controlnavel).addclass('active'); } // process caption processcaption(settings); // remove any slices from last transition $('.nivo-slice', slider).remove(); // remove any boxes from last transition $('.nivo-box', slider).remove(); var currenteffect = settings.effect, anims = ''; // generate random effect if(settings.effect === 'random'){ anims = new array('slicedownright','slicedownleft','sliceupright','sliceupleft','sliceupdown','sliceupdownleft','fold','fade', 'boxrandom','boxrain','boxrainreverse','boxraingrow','boxraingrowreverse'); currenteffect = anims[math.floor(math.random()*(anims.length + 1))]; if(currenteffect === undefined) { currenteffect = 'fade'; } } // run random effect from specified set (eg: effect:'fold,fade') if(settings.effect.indexof(',') !== -1){ anims = settings.effect.split(','); currenteffect = anims[math.floor(math.random()*(anims.length))]; if(currenteffect === undefined) { currenteffect = 'fade'; } } // custom transition as defined by "data-transition" attribute if(vars.currentimage.attr('data-transition')){ currenteffect = vars.currentimage.attr('data-transition'); } // run effects vars.running = true; var timebuff = 0, i = 0, slices = '', firstslice = '', totalboxes = '', boxes = ''; if(currenteffect === 'slicedown' || currenteffect === 'slicedownright' || currenteffect === 'slicedownleft'){ createslices(slider, settings, vars); timebuff = 0; i = 0; slices = $('.nivo-slice', slider); if(currenteffect === 'slicedownleft') { slices = $('.nivo-slice', slider)._reverse(); } slices.each(function(){ var slice = $(this); slice.css({ 'top': '0px' }); if(i === settings.slices-1){ settimeout(function(){ slice.animate({opacity:'1.0' }, settings.animspeed, '', function(){ slider.trigger('nivo:animfinished'); }); }, (100 + timebuff)); } else { settimeout(function(){ slice.animate({opacity:'1.0' }, settings.animspeed); }, (100 + timebuff)); } timebuff += 50; i++; }); } else if(currenteffect === 'sliceup' || currenteffect === 'sliceupright' || currenteffect === 'sliceupleft'){ createslices(slider, settings, vars); timebuff = 0; i = 0; slices = $('.nivo-slice', slider); if(currenteffect === 'sliceupleft') { slices = $('.nivo-slice', slider)._reverse(); } slices.each(function(){ var slice = $(this); slice.css({ 'bottom': '0px' }); if(i === settings.slices-1){ settimeout(function(){ slice.animate({opacity:'1.0' }, settings.animspeed, '', function(){ slider.trigger('nivo:animfinished'); }); }, (100 + timebuff)); } else { settimeout(function(){ slice.animate({opacity:'1.0' }, settings.animspeed); }, (100 + timebuff)); } timebuff += 50; i++; }); } else if(currenteffect === 'sliceupdown' || currenteffect === 'sliceupdownright' || currenteffect === 'sliceupdownleft'){ createslices(slider, settings, vars); timebuff = 0; i = 0; var v = 0; slices = $('.nivo-slice', slider); if(currenteffect === 'sliceupdownleft') { slices = $('.nivo-slice', slider)._reverse(); } slices.each(function(){ var slice = $(this); if(i === 0){ slice.css('top','0px'); i++; } else { slice.css('bottom','0px'); i = 0; } if(v === settings.slices-1){ settimeout(function(){ slice.animate({opacity:'1.0' }, settings.animspeed, '', function(){ slider.trigger('nivo:animfinished'); }); }, (100 + timebuff)); } else { settimeout(function(){ slice.animate({opacity:'1.0' }, settings.animspeed); }, (100 + timebuff)); } timebuff += 50; v++; }); } else if(currenteffect === 'fold'){ createslices(slider, settings, vars); timebuff = 0; i = 0; $('.nivo-slice', slider).each(function(){ var slice = $(this); var origwidth = slice.width(); slice.css({ top:'0px', width:'0px' }); if(i === settings.slices-1){ settimeout(function(){ slice.animate({ width:origwidth, opacity:'1.0' }, settings.animspeed, '', function(){ slider.trigger('nivo:animfinished'); }); }, (100 + timebuff)); } else { settimeout(function(){ slice.animate({ width:origwidth, opacity:'1.0' }, settings.animspeed); }, (100 + timebuff)); } timebuff += 50; i++; }); } else if(currenteffect === 'fade'){ createslices(slider, settings, vars); firstslice = $('.nivo-slice:first', slider); firstslice.css({ 'width': slider.width() + 'px' }); firstslice.animate({ opacity:'1.0' }, (settings.animspeed*2), '', function(){ slider.trigger('nivo:animfinished'); }); } else if(currenteffect === 'slideinright'){ createslices(slider, settings, vars); firstslice = $('.nivo-slice:first', slider); firstslice.css({ 'width': '0px', 'opacity': '1' }); firstslice.animate({ width: slider.width() + 'px' }, (settings.animspeed*2), '', function(){ slider.trigger('nivo:animfinished'); }); } else if(currenteffect === 'slideinleft'){ createslices(slider, settings, vars); firstslice = $('.nivo-slice:first', slider); firstslice.css({ 'width': '0px', 'opacity': '1', 'left': '', 'right': '0px' }); firstslice.animate({ width: slider.width() + 'px' }, (settings.animspeed*2), '', function(){ // reset positioning firstslice.css({ 'left': '0px', 'right': '' }); slider.trigger('nivo:animfinished'); }); } else if(currenteffect === 'boxrandom'){ createboxes(slider, settings, vars); totalboxes = settings.boxcols * settings.boxrows; i = 0; timebuff = 0; boxes = shuffle($('.nivo-box', slider)); boxes.each(function(){ var box = $(this); if(i === totalboxes-1){ settimeout(function(){ box.animate({ opacity:'1' }, settings.animspeed, '', function(){ slider.trigger('nivo:animfinished'); }); }, (100 + timebuff)); } else { settimeout(function(){ box.animate({ opacity:'1' }, settings.animspeed); }, (100 + timebuff)); } timebuff += 20; i++; }); } else if(currenteffect === 'boxrain' || currenteffect === 'boxrainreverse' || currenteffect === 'boxraingrow' || currenteffect === 'boxraingrowreverse'){ createboxes(slider, settings, vars); totalboxes = settings.boxcols * settings.boxrows; i = 0; timebuff = 0; // split boxes into 2d array var rowindex = 0; var colindex = 0; var box2darr = []; box2darr[rowindex] = []; boxes = $('.nivo-box', slider); if(currenteffect === 'boxrainreverse' || currenteffect === 'boxraingrowreverse'){ boxes = $('.nivo-box', slider)._reverse(); } boxes.each(function(){ box2darr[rowindex][colindex] = $(this); colindex++; if(colindex === settings.boxcols){ rowindex++; colindex = 0; box2darr[rowindex] = []; } }); // run animation for(var cols = 0; cols < (settings.boxcols * 2); cols++){ var prevcol = cols; for(var rows = 0; rows < settings.boxrows; rows++){ if(prevcol >= 0 && prevcol < settings.boxcols){ /* due to some weird js bug with loop vars being used in settimeout, this is wrapped with an anonymous function call */ (function(row, col, time, i, totalboxes) { var box = $(box2darr[row][col]); var w = box.width(); var h = box.height(); if(currenteffect === 'boxraingrow' || currenteffect === 'boxraingrowreverse'){ box.width(0).height(0); } if(i === totalboxes-1){ settimeout(function(){ box.animate({ opacity:'1', width:w, height:h }, settings.animspeed/1.3, '', function(){ slider.trigger('nivo:animfinished'); }); }, (100 + time)); } else { settimeout(function(){ box.animate({ opacity:'1', width:w, height:h }, settings.animspeed/1.3); }, (100 + time)); } })(rows, prevcol, timebuff, i, totalboxes); i++; } prevcol--; } timebuff += 100; } } }; // shuffle an array var shuffle = function(arr){ for(var j, x, i = arr.length; i; j = parseint(math.random() * i, 10), x = arr[--i], arr[i] = arr[j], arr[j] = x); return arr; }; // for debugging var trace = function(msg){ if(this.console && typeof console.log !== 'undefined') { console.log(msg); } }; // start / stop this.stop = function(){ if(!$(element).data('nivo:vars').stop){ $(element).data('nivo:vars').stop = true; trace('stop slider'); } }; this.start = function(){ if($(element).data('nivo:vars').stop){ $(element).data('nivo:vars').stop = false; trace('start slider'); } }; // trigger the afterload callback settings.afterload.call(this); return this; }; $.fn.nivoslider = function(options) { return this.each(function(key, value){ var element = $(this); // return early if this element already has a plugin instance if (element.data('nivoslider')) { return element.data('nivoslider'); } // pass options to plugin constructor var nivoslider = new nivoslider(this, options); // store plugin object in this element's data element.data('nivoslider', nivoslider); }); }; //default settings $.fn.nivoslider.defaults = { effect: 'random', slices: 15, boxcols: 8, boxrows: 4, animspeed: 500, pausetime: 3000, startslide: 0, directionnav: true, controlnav: true, controlnavthumbs: false, pauseonhover: true, manualadvance: false, prevtext: 'prev', nexttext: 'next', randomstart: false, beforechange: function(){}, afterchange: function(){}, slideshowend: function(){}, lastslide: function(){}, afterload: function(){} }; $.fn._reverse = [].reverse; })(jquery);