Guest User

Untitled

a guest
Mar 26th, 2013
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * jquery.flipshow.js v1.0.0
  3.  * http://www.codrops.com
  4.  *
  5.  * Licensed under the MIT license.
  6.  * http://www.opensource.org/licenses/mit-license.php
  7.  *
  8.  * Copyright 2013, Codrops
  9.  * http://www.codrops.com
  10.  */
  11. ;( function( $, window, undefined ) {
  12.  
  13.     'use strict';
  14.  
  15.     // ======================= imagesLoaded Plugin ===============================
  16.     // https://github.com/desandro/imagesloaded
  17.  
  18.     // $('#my-container').imagesLoaded(myFunction)
  19.     // execute a callback when all images have loaded.
  20.     // needed because .load() doesn't work on cached images
  21.  
  22.     // callback function gets image collection as argument
  23.     //  this is the container
  24.  
  25.     // original: mit license. paul irish. 2010.
  26.     // contributors: Oren Solomianik, David DeSandro, Yiannis Chatzikonstantinou
  27.  
  28.     // blank image data-uri bypasses webkit log warning (thx doug jones)
  29.     // blank image data-uri bypasses webkit log warning (thx doug jones)
  30.     var BLANK = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';
  31.  
  32.     $.fn.imagesLoaded = function( callback ) {
  33.         var $this = this,
  34.             deferred = $.isFunction($.Deferred) ? $.Deferred() : 0,
  35.             hasNotify = $.isFunction(deferred.notify),
  36.             $images = $this.find('img').add( $this.filter('img') ),
  37.             loaded = [],
  38.             proper = [],
  39.             broken = [];
  40.  
  41.         // Register deferred callbacks
  42.         if ($.isPlainObject(callback)) {
  43.             $.each(callback, function (key, value) {
  44.                 if (key === 'callback') {
  45.                     callback = value;
  46.                 } else if (deferred) {
  47.                     deferred[key](value);
  48.                 }
  49.             });
  50.         }
  51.  
  52.         function doneLoading() {
  53.             var $proper = $(proper),
  54.                 $broken = $(broken);
  55.  
  56.             if ( deferred ) {
  57.                 if ( broken.length ) {
  58.                     deferred.reject( $images, $proper, $broken );
  59.                 } else {
  60.                     deferred.resolve( $images );
  61.                 }
  62.             }
  63.  
  64.             if ( $.isFunction( callback ) ) {
  65.                 callback.call( $this, $images, $proper, $broken );
  66.             }
  67.         }
  68.  
  69.         function imgLoadedHandler( event ) {
  70.             imgLoaded( event.target, event.type === 'error' );
  71.         }
  72.  
  73.         function imgLoaded( img, isBroken ) {
  74.             // don't proceed if BLANK image, or image is already loaded
  75.             if ( img.src === BLANK || $.inArray( img, loaded ) !== -1 ) {
  76.                 return;
  77.             }
  78.  
  79.             // store element in loaded images array
  80.             loaded.push( img );
  81.  
  82.             // keep track of broken and properly loaded images
  83.             if ( isBroken ) {
  84.                 broken.push( img );
  85.             } else {
  86.                 proper.push( img );
  87.             }
  88.  
  89.             // cache image and its state for future calls
  90.             $.data( img, 'imagesLoaded', { isBroken: isBroken, src: img.src } );
  91.  
  92.             // trigger deferred progress method if present
  93.             if ( hasNotify ) {
  94.                 deferred.notifyWith( $(img), [ isBroken, $images, $(proper), $(broken) ] );
  95.             }
  96.  
  97.             // call doneLoading and clean listeners if all images are loaded
  98.             if ( $images.length === loaded.length ) {
  99.                 setTimeout( doneLoading );
  100.                 $images.unbind( '.imagesLoaded', imgLoadedHandler );
  101.             }
  102.         }
  103.  
  104.         // if no images, trigger immediately
  105.         if ( !$images.length ) {
  106.             doneLoading();
  107.         } else {
  108.             $images.bind( 'load.imagesLoaded error.imagesLoaded', imgLoadedHandler )
  109.             .each( function( i, el ) {
  110.                 var src = el.src;
  111.  
  112.                 // find out if this image has been already checked for status
  113.                 // if it was, and src has not changed, call imgLoaded on it
  114.                 var cached = $.data( el, 'imagesLoaded' );
  115.                 if ( cached && cached.src === src ) {
  116.                     imgLoaded( el, cached.isBroken );
  117.                     return;
  118.                 }
  119.  
  120.                 // if complete is true and browser supports natural sizes, try
  121.                 // to check for image status manually
  122.                 if ( el.complete && el.naturalWidth !== undefined ) {
  123.                     imgLoaded( el, el.naturalWidth === 0 || el.naturalHeight === 0 );
  124.                     return;
  125.                 }
  126.  
  127.                 // cached images don't fire load sometimes, so we reset src, but only when
  128.                 // dealing with IE, or image is complete (loaded) and failed manual check
  129.                 // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
  130.                 if ( el.readyState || el.complete ) {
  131.                     el.src = BLANK;
  132.                     el.src = src;
  133.                 }
  134.             });
  135.         }
  136.  
  137.         return deferred ? deferred.promise( $this ) : $this;
  138.     };
  139.  
  140.     // global
  141.     var Modernizr = window.Modernizr;
  142.  
  143.     $.Flipshow = function( options, element ) {
  144.         this.$el = $( element );
  145.         this._init( options );
  146.     };
  147.  
  148.     // the options
  149.     $.Flipshow.defaults = {
  150.         // default transition speed (ms)
  151.         speed : 700,
  152.         // default transition easing
  153.         easing : 'cubic-bezier(.29,1.44,.86,1.06)'
  154.     };
  155.  
  156.     $.Flipshow.prototype = {
  157.         _init : function( options ) {
  158.            
  159.             // options
  160.             this.options = $.extend( true, {}, $.Flipshow.defaults, options );
  161.             // support for CSS Transitions & 3D transforms
  162.             this.support = Modernizr.csstransitions && Modernizr.csstransforms3d;
  163.             // transition end event name and transform name
  164.             var transProperties = {
  165.                 'WebkitTransition' : { transitionEndEvent : 'webkitTransitionEnd', transformName : '-webkit-transform' },
  166.                 'MozTransition' : { transitionEndEvent : 'transitionend', transformName : '-moz-transform' },
  167.                 'OTransition' : { transitionEndEvent : 'oTransitionEnd', transformName : '-o-transform' },
  168.                 'msTransition' : { transitionEndEvent : 'MSTransitionEnd', transformName : '-ms-transform' },
  169.                 'transition' : { transitionEndEvent : 'transitionend', transformName : 'transform' }
  170.             };
  171.             if( this.support ) {
  172.                 this.transEndEventName = transProperties[ Modernizr.prefixed( 'transition' ) ].transitionEndEvent + '.flipshow';
  173.                 this.transformName = transProperties[ Modernizr.prefixed( 'transition' ) ].transformName;
  174.             }
  175.             this.transitionProperties = this.transformName + ' ' + this.options.speed + 'ms ' + this.options.easing;
  176.  
  177.             // the list of items
  178.             this.$listItems = this.$el.children( 'ul.fc-slides' );
  179.             // the items
  180.             this.$items = this.$listItems.children( 'li' ).hide();
  181.             // total number of items
  182.             this.itemsCount = this.$items.length;
  183.             // current item´s index
  184.             this.current = 0;
  185.             this.$listItems.imagesLoaded( $.proxy( function() {
  186.                 // show first item
  187.                 this.$items.eq( this.current ).show();
  188.                 // add navigation and flipping structure
  189.                 if( this.itemsCount > 0 ) {
  190.                     this._addNav();
  191.                     if( this.support ) {
  192.                         this._layout();
  193.                     }
  194.                 }
  195.             }, this ) );
  196.  
  197.         },
  198.         _addNav : function() {
  199.  
  200.             var self = this,
  201.                 $navLeft = $( '<div class="fc-left"><span></span><span></span><span></span><i class="icon-arrow-left"><img src="images/left_arrow.png"/></i></div>' ),
  202.                 $navRight = $( '<div class="fc-right"><span></span><span></span><span></span><i class="icon-arrow-right"><img src="images/right_arrow.png"/></i></div>' );
  203.  
  204.             $( '<nav></nav>' ).append( $navLeft, $navRight ).appendTo( this.$el );
  205.  
  206.             $navLeft.find( 'span' ).on( 'click.flipshow touchstart.flipshow', function() {
  207.                 self._navigate( $( this ), 'left' );
  208.             } );
  209.  
  210.             $navRight.find( 'span' ).on( 'click.flipshow touchstart.flipshow', function() {
  211.                 self._navigate( $( this ), 'right' );
  212.             } );
  213.  
  214.         },
  215.         _layout : function( $current, $next ) {
  216.  
  217.             this.$flipFront = $( '<div class="fc-front"><div></div></div>' );
  218.             this.$frontContent = this.$flipFront.children( 'div:first' );
  219.             this.$flipBack = $( '<div class="fc-back"><div></div></div>' );
  220.             this.$backContent = this.$flipBack.children( 'div:first' );
  221.             this.$flipEl = $( '<div class="fc-flip"></div>' ).append( this.$flipFront, this.$flipBack ).hide().appendTo( this.$el );
  222.  
  223.         },
  224.         _navigate : function( $nav, dir ) {
  225.  
  226.             if( this.isAnimating && this.support ) {
  227.                 return false;
  228.             }
  229.             this.isAnimating = true;
  230.            
  231.             var $currentItem = this.$items.eq( this.current ).hide();
  232.  
  233.             if( dir === 'right' ) {
  234.                 this.current < this.itemsCount - 1 ? ++this.current : this.current = 0;
  235.             }
  236.             else if( dir === 'left' ) {
  237.                 this.current > 0 ? --this.current : this.current = this.itemsCount - 1;
  238.             }
  239.  
  240.             var $nextItem = this.$items.eq( this.current );
  241.  
  242.             if( this.support ) {
  243.                 this._flip( $currentItem, $nextItem, dir, $nav.index() );
  244.             }
  245.             else {
  246.                 $nextItem.show();              
  247.             }
  248.  
  249.         },
  250.         _flip : function( $currentItem, $nextItem, dir, angle ) {
  251.            
  252.             var transformProperties = '',
  253.                 // overlays
  254.                 $overlayLight = $( '<div class="fc-overlay-light"></div>' ),
  255.                 $overlayDark = $( '<div class="fc-overlay-dark"></div>' );
  256.  
  257.             this.$flipEl.css( 'transition', this.transitionProperties );
  258.            
  259.             this.$flipFront.find( 'div.fc-overlay-light, div.fc-overlay-dark' ).remove();
  260.             this.$flipBack.find( 'div.fc-overlay-light, div.fc-overlay-dark' ).remove();
  261.  
  262.             if( dir === 'right' ) {
  263.                 this.$flipFront.append( $overlayLight );
  264.                 this.$flipBack.append( $overlayDark );
  265.                 $overlayDark.css( 'opacity', 1 );
  266.             }
  267.             else if( dir === 'left' ) {
  268.                 this.$flipFront.append( $overlayDark );
  269.                 this.$flipBack.append( $overlayLight );
  270.                 $overlayLight.css( 'opacity', 1 );
  271.             }
  272.             var overlayStyle = { transition : 'opacity ' + ( this.options.speed / 1.3 ) + 'ms' };
  273.             $overlayLight.css( overlayStyle );
  274.             $overlayDark.css( overlayStyle );
  275.  
  276.             switch( angle ) {
  277.                 case 0 :
  278.                     transformProperties = dir === 'left' ? 'rotate3d(-1,1,0,-179deg) rotate3d(-1,1,0,-1deg)' : 'rotate3d(1,1,0,180deg)';
  279.                     break;
  280.                 case 1 :
  281.                     transformProperties = dir === 'left' ? 'rotate3d(0,1,0,-179deg) rotate3d(0,1,0,-1deg)' : 'rotate3d(0,1,0,180deg)';
  282.                     break;
  283.                 case 2 :
  284.                     transformProperties = dir === 'left' ? 'rotate3d(1,1,0,-179deg) rotate3d(1,1,0,-1deg)' : 'rotate3d(-1,1,0,179deg) rotate3d(-1,1,0,1deg)';
  285.                     break;
  286.             }
  287.        
  288.             this.$flipBack.css( 'transform', transformProperties );
  289.  
  290.             this.$frontContent.empty().html( $currentItem.html() );
  291.             this.$backContent.empty().html( $nextItem.html() );
  292.             this.$flipEl.show();
  293.  
  294.             var self = this;
  295.             setTimeout( function() {
  296.                
  297.                 self.$flipEl.css( 'transform', transformProperties );
  298.                 $overlayLight.css( 'opacity', dir === 'right' ? 1 : 0 );
  299.                 $overlayDark.css( 'opacity', dir === 'right' ? 0 : 1 );
  300.                 self.$flipEl.on( self.transEndEventName, function( event ) {
  301.                     if( event.target.className === 'fc-overlay-light' || event.target.className === 'fc-overlay-dark' ) return;
  302.                     self._ontransitionend( $nextItem );
  303.                 } );
  304.  
  305.             }, 25 );
  306.  
  307.         },
  308.         _ontransitionend : function( $nextItem ) {
  309.             $nextItem.show();
  310.             this.$flipEl.off( this.transEndEventName ).css( {
  311.                 transition : 'none',
  312.                 transform : 'none'
  313.             } ).hide();
  314.             this.isAnimating = false;
  315.         }
  316.     };
  317.  
  318.     var logError = function( message ) {
  319.         if ( window.console ) {
  320.             window.console.error( message );
  321.         }
  322.     };
  323.  
  324.     $.fn.flipshow = function( options ) {
  325.         if ( typeof options === 'string' ) {
  326.             var args = Array.prototype.slice.call( arguments, 1 );
  327.             this.each(function() {
  328.                 var instance = $.data( this, 'flipshow' );
  329.                 if ( !instance ) {
  330.                     logError( "cannot call methods on flipshow prior to initialization; " +
  331.                     "attempted to call method '" + options + "'" );
  332.                     return;
  333.                 }
  334.                 if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
  335.                     logError( "no such method '" + options + "' for flipshow instance" );
  336.                     return;
  337.                 }
  338.                 instance[ options ].apply( instance, args );
  339.             });
  340.         }
  341.         else {
  342.             this.each(function() { 
  343.                 var instance = $.data( this, 'flipshow' );
  344.                 if ( instance ) {
  345.                     instance._init();
  346.                 }
  347.                 else {
  348.                     instance = $.data( this, 'flipshow', new $.Flipshow( options, this ) );
  349.                 }
  350.             });
  351.         }
  352.         return this;
  353.     };
  354.  
  355. } )( jQuery, window );
Add Comment
Please, Sign In to add comment