/** * jquery.gallery.js * http://www.codrops.com * * copyright 2011, pedro botelho / codrops * free to use under the mit license. * * date: mon jan 30 2012 */ (function( $, undefined ) { /* * gallery object. */ $.gallery = function( options, element ) { this.$el = $( element ); this._init( options ); }; $.gallery.defaults = { current : 0, // index of current item autoplay : false,// slideshow on / off interval : 2000 // time between transitions }; $.gallery.prototype = { _init : function( options ) { this.options = $.extend( true, {}, $.gallery.defaults, options ); // support for 3d / 2d transforms and transitions this.support3d = modernizr.csstransforms3d; this.support2d = modernizr.csstransforms; this.supporttrans = modernizr.csstransitions; this.$wrapper = this.$el.find('.dg-wrapper'); this.$items = this.$wrapper.children(); this.itemscount = this.$items.length; this.$nav = this.$el.find('nav'); this.$navprev = this.$nav.find('.dg-prev'); this.$navnext = this.$nav.find('.dg-next'); // minimum of 3 items if( this.itemscount < 3 ) { this.$nav.remove(); return false; } this.current = this.options.current; this.isanim = false; this.$items.css({ 'opacity' : 0, 'visibility': 'hidden' }); this._validate(); this._layout(); // load the events this._loadevents(); // slideshow if( this.options.autoplay ) { this._startslideshow(); } }, _validate : function() { if( this.options.current < 0 || this.options.current > this.itemscount - 1 ) { this.current = 0; } }, _layout : function() { // current, left and right items this._setitems(); // current item is not changed // left and right one are rotated and translated var leftcss, rightcss, currentcss; if( this.support3d && this.supporttrans ) { leftcss = { '-webkit-transform' : 'translatex(-830px) translatez(-380px) rotatey(-50deg)', '-moz-transform' : 'translatex(-830px) translatez(-380px) rotatey(-50deg)', '-o-transform' : 'translatex(-830px) translatez(-380px) rotatey(-50deg)', '-ms-transform' : 'translatex(-830px) translatez(-380px) rotatey(-50deg)', 'transform' : 'translatex(-830px) translatez(-380px) rotatey(-50deg)', //'opacity' : 0.5 }; rightcss = { '-webkit-transform' : 'translatex(830px) translatez(-380px) rotatey(50deg)', '-moz-transform' : 'translatex(830px) translatez(-380px) rotatey(50deg)', '-o-transform' : 'translatex(830px) translatez(-380px) rotatey(50deg)', '-ms-transform' : 'translatex(830px) translatez(-380px) rotatey(50deg)', 'transform' : 'translatex(830px) translatez(-380px) rotatey(50deg)', //'opacity' : 0.5 }; leftcss.opacity = 0.4; leftcss.visibility = 'visible'; rightcss.opacity = 0.4; rightcss.visibility = 'visible'; } else if( this.support2d && this.supporttrans ) { leftcss = { '-webkit-transform' : 'translate(-350px) scale(0.8)', '-moz-transform' : 'translate(-350px) scale(0.8)', '-o-transform' : 'translate(-350px) scale(0.8)', '-ms-transform' : 'translate(-350px) scale(0.8)', 'transform' : 'translate(-350px) scale(0.8)' }; rightcss = { '-webkit-transform' : 'translate(350px) scale(0.8)', '-moz-transform' : 'translate(350px) scale(0.8)', '-o-transform' : 'translate(350px) scale(0.8)', '-ms-transform' : 'translate(350px) scale(0.8)', 'transform' : 'translate(350px) scale(0.8)' }; currentcss = { 'z-index' : 999 }; leftcss.opacity = 1; leftcss.visibility = 'visible'; rightcss.opacity = 1; rightcss.visibility = 'visible'; } this.$leftitm.css( leftcss || {} ); this.$rightitm.css( rightcss || {} ); this.$currentitm.css( currentcss || {} ).css({ 'opacity' : 1, 'visibility': 'visible' }).addclass('dg-center'); }, _setitems : function() { this.$items.removeclass('dg-center'); this.$currentitm = this.$items.eq( this.current ); this.$leftitm = ( this.current === 0 ) ? this.$items.eq( this.itemscount - 1 ) : this.$items.eq( this.current - 1 ); this.$rightitm = ( this.current === this.itemscount - 1 ) ? this.$items.eq( 0 ) : this.$items.eq( this.current + 1 ); if( !this.support3d && this.support2d && this.supporttrans ) { this.$items.css( 'z-index', 1 ); this.$currentitm.css( 'z-index', 999 ); } // next & previous items if( this.itemscount > 3 ) { // next item this.$nextitm = ( this.$rightitm.index() === this.itemscount - 1 ) ? this.$items.eq( 0 ) : this.$rightitm.next(); this.$nextitm.css( this._getcoordinates('outright') ); // previous item this.$previtm = ( this.$leftitm.index() === 0 ) ? this.$items.eq( this.itemscount - 1 ) : this.$leftitm.prev(); this.$previtm.css( this._getcoordinates('outleft') ); } }, _loadevents : function() { var _self = this; this.$navprev.on( 'click.gallery', function( event ) { if( _self.options.autoplay ) { cleartimeout( _self.slideshow ); _self.options.autoplay = false; } _self._navigate('prev'); return false; }); this.$navnext.on( 'click.gallery', function( event ) { if( _self.options.autoplay ) { cleartimeout( _self.slideshow ); _self.options.autoplay = false; } _self._navigate('next'); return false; }); this.$wrapper.on( 'webkittransitionend.gallery transitionend.gallery otransitionend.gallery', function( event ) { _self.$currentitm.addclass('dg-center'); _self.$items.removeclass('dg-transition'); _self.isanim = false; }); }, _getcoordinates : function( position ) { if( this.support3d && this.supporttrans ) { switch( position ) { case 'outleft': return { '-webkit-transform' : 'translatex(-830px) translatez(-385px) rotatey(-50deg)', '-moz-transform' : 'translatex(-830px) translatez(-385px) rotatey(-50deg)', '-o-transform' : 'translatex(-830px) translatez(-385px) rotatey(-50deg)', '-ms-transform' : 'translatex(-830px) translatez(-385px) rotatey(-50deg)', 'transform' : 'translatex(-830px) translatez(-385px) rotatey(-50deg)', 'opacity' : 0, 'visibility' : 'hidden' }; break; case 'outright': return { '-webkit-transform' : 'translatex(830px) translatez(-385px) rotatey(50deg)', '-moz-transform' : 'translatex(830px) translatez(-385px) rotatey(50deg)', '-o-transform' : 'translatex(830px) translatez(-385px) rotatey(50deg)', '-ms-transform' : 'translatex(830px) translatez(-385px) rotatey(50deg)', 'transform' : 'translatex(830px) translatez(-385px) rotatey(50deg)', 'opacity' : 0, 'visibility' : 'hidden' }; break; case 'left': return { '-webkit-transform' : 'translatex(-830px) translatez(-385px) rotatey(-50deg)', '-moz-transform' : 'translatex(-830px) translatez(-385px) rotatey(-50deg)', '-o-transform' : 'translatex(-830px) translatez(-385px) rotatey(-50deg)', '-ms-transform' : 'translatex(-830px) translatez(-385px) rotatey(-50deg)', 'transform' : 'translatex(-830px) translatez(-385px) rotatey(-50deg)', 'opacity' : 0.4, 'visibility' : 'visible' }; break; case 'right': return { '-webkit-transform' : 'translatex(830px) translatez(-385px) rotatey(50deg)', '-moz-transform' : 'translatex(830px) translatez(-385px) rotatey(50deg)', '-o-transform' : 'translatex(830px) translatez(-385px) rotatey(50deg)', '-ms-transform' : 'translatex(830px) translatez(-385px) rotatey(50deg)', 'transform' : 'translatex(830px) translatez(-385px) rotatey(50deg)', 'opacity' : 0.4, 'visibility' : 'visible' }; break; case 'center': return { '-webkit-transform' : 'translatex(0px) translatez(0px) rotatey(0deg)', '-moz-transform' : 'translatex(0px) translatez(0px) rotatey(0deg)', '-o-transform' : 'translatex(0px) translatez(0px) rotatey(0deg)', '-ms-transform' : 'translatex(0px) translatez(0px) rotatey(0deg)', 'transform' : 'translatex(0px) translatez(0px) rotatey(0deg)', 'opacity' : 1, 'visibility' : 'visible' }; break; }; } else if( this.support2d && this.supporttrans ) { switch( position ) { case 'outleft': return { '-webkit-transform' : 'translate(-450px) scale(0.7)', '-moz-transform' : 'translate(-450px) scale(0.7)', '-o-transform' : 'translate(-450px) scale(0.7)', '-ms-transform' : 'translate(-450px) scale(0.7)', 'transform' : 'translate(-450px) scale(0.7)', 'opacity' : 0, 'visibility' : 'hidden' }; break; case 'outright': return { '-webkit-transform' : 'translate(450px) scale(0.7)', '-moz-transform' : 'translate(450px) scale(0.7)', '-o-transform' : 'translate(450px) scale(0.7)', '-ms-transform' : 'translate(450px) scale(0.7)', 'transform' : 'translate(450px) scale(0.7)', 'opacity' : 0, 'visibility' : 'hidden' }; break; case 'left': return { '-webkit-transform' : 'translate(-350px) scale(0.8)', '-moz-transform' : 'translate(-350px) scale(0.8)', '-o-transform' : 'translate(-350px) scale(0.8)', '-ms-transform' : 'translate(-350px) scale(0.8)', 'transform' : 'translate(-350px) scale(0.8)', 'opacity' : 1, 'visibility' : 'visible' }; break; case 'right': return { '-webkit-transform' : 'translate(350px) scale(0.8)', '-moz-transform' : 'translate(350px) scale(0.8)', '-o-transform' : 'translate(350px) scale(0.8)', '-ms-transform' : 'translate(350px) scale(0.8)', 'transform' : 'translate(350px) scale(0.8)', 'opacity' : 1, 'visibility' : 'visible' }; break; case 'center': return { '-webkit-transform' : 'translate(0px) scale(1)', '-moz-transform' : 'translate(0px) scale(1)', '-o-transform' : 'translate(0px) scale(1)', '-ms-transform' : 'translate(0px) scale(1)', 'transform' : 'translate(0px) scale(1)', 'opacity' : 1, 'visibility' : 'visible' }; break; }; } else { switch( position ) { case 'outleft' : case 'outright' : case 'left' : case 'right' : return { 'opacity' : 0, 'visibility' : 'hidden' }; break; case 'center' : return { 'opacity' : 1, 'visibility' : 'visible' }; break; }; } }, _navigate : function( dir ) { if( this.supporttrans && this.isanim ) return false; this.isanim = true; switch( dir ) { case 'next' : this.current = this.$rightitm.index(); // current item moves left this.$currentitm.addclass('dg-transition').css( this._getcoordinates('left') ); // right item moves to the center this.$rightitm.addclass('dg-transition').css( this._getcoordinates('center') ); // next item moves to the right if( this.$nextitm ) { // left item moves out this.$leftitm.addclass('dg-transition').css( this._getcoordinates('outleft') ); this.$nextitm.addclass('dg-transition').css( this._getcoordinates('right') ); } else { // left item moves right this.$leftitm.addclass('dg-transition').css( this._getcoordinates('right') ); } break; case 'prev' : this.current = this.$leftitm.index(); // current item moves right this.$currentitm.addclass('dg-transition').css( this._getcoordinates('right') ); // left item moves to the center this.$leftitm.addclass('dg-transition').css( this._getcoordinates('center') ); // prev item moves to the left if( this.$previtm ) { // right item moves out this.$rightitm.addclass('dg-transition').css( this._getcoordinates('outright') ); this.$previtm.addclass('dg-transition').css( this._getcoordinates('left') ); } else { // right item moves left this.$rightitm.addclass('dg-transition').css( this._getcoordinates('left') ); } break; }; this._setitems(); if( !this.supporttrans ) this.$currentitm.addclass('dg-center'); }, _startslideshow : function() { var _self = this; this.slideshow = settimeout( function() { _self._navigate( 'next' ); if( _self.options.autoplay ) { _self._startslideshow(); } }, this.options.interval ); }, destroy : function() { this.$navprev.off('.gallery'); this.$navnext.off('.gallery'); this.$wrapper.off('.gallery'); } }; var logerror = function( message ) { if ( this.console ) { console.error( message ); } }; $.fn.gallery = function( options ) { if ( typeof options === 'string' ) { var args = array.prototype.slice.call( arguments, 1 ); this.each(function() { var instance = $.data( this, 'gallery' ); if ( !instance ) { logerror( "cannot call methods on gallery prior to initialization; " + "attempted to call method '" + options + "'" ); return; } if ( !$.isfunction( instance[options] ) || options.charat(0) === "_" ) { logerror( "no such method '" + options + "' for gallery instance" ); return; } instance[ options ].apply( instance, args ); }); } else { this.each(function() { var instance = $.data( this, 'gallery' ); if ( !instance ) { $.data( this, 'gallery', new $.gallery( options, this ) ); } }); } return this; }; })( jquery );