Advertisement
thorbj

jqFlick.js modified

Feb 1st, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 5.03 KB | None | 0 0
  1. (function($){
  2.  
  3.     $.fn.jqFlick = function(options){
  4.        
  5.         // Default options:
  6.        
  7.         options = $.extend({
  8.             width:640,
  9.             height:400,
  10.             maxFetch:50,
  11.             captions:false,
  12.             autoAdvance:false,
  13.             advancePeriod:10000,
  14.             apiKey:''
  15.         },options);
  16.        
  17.         // Using YQL and the flickr.photosets.photos table to query the Flickr API.
  18.        
  19.         var YQL = 'http://query.yahooapis.com/v1/public/yql?q={QUERY}&format=json&callback=?',
  20.             query = "SELECT * FROM flickr.photosets.photos({MAX}) WHERE photoset_id='{PHOTOSET}' AND api_key='{KEY}'";
  21.        
  22.         // Replacing the "{EXAMPLE}" keywords from the strings:
  23.        
  24.         YQL = templateReplace(YQL,{
  25.             "query": encodeURIComponent(
  26.                 templateReplace(query,{
  27.                     photoset : options.photosetID,
  28.                     max : options.maxFetch,
  29.                     key: options.apiKey
  30.                 }
  31.             ))
  32.         });
  33.        
  34.         // Template for building Flickr's image URLs:
  35.        
  36.         var flickrSRC = 'http://farm{FARM}.static.flickr.com/{SERVER}/{ID}_{SECRET}_z.jpg',
  37.             flickrSlider = this;
  38.        
  39.         flickrSlider.trigger('jqFlickRemove');
  40.        
  41.         // Fetching the images using Flickr's API:
  42.        
  43.         $.getJSON(YQL,function(r){
  44.             if(!r || !r.query || !r.query.count){
  45.                 throw "There is no such photoset!";
  46.             }
  47.  
  48.             var currentPos = 1,
  49.                 cnt = r.query.count;
  50.  
  51.             var caption = $('<span>',{
  52.                 className: 'caption'
  53.             }).appendTo(flickrSlider);
  54.  
  55.             var ul = $('<ul>',{
  56.                 css:{
  57.                     width: options.width*r.query.count,
  58.                     height:options.height
  59.                 }
  60.             });
  61.            
  62.             // Looping through the photo results:
  63.            
  64.             $.each(r.query.results.photo,function(){
  65.                 data = this;
  66.            
  67.                 // Creating a new LI element with the photo as its
  68.                 // centered background image:
  69.                
  70.                 $('<li>',{
  71.                     css : {
  72.                         backgroundImage: 'url('+templateReplace(flickrSRC,data)+')',
  73.                         width: options.width
  74.                     }
  75.                 }).appendTo(ul);
  76.             });
  77.  
  78.             flickrSlider.addClass('flickrSliderHolder')
  79.                         .width(options.width)
  80.                         .height(options.height) //+25
  81.                         .append(ul);
  82.            
  83.             // Binding a custom "slide" event.
  84.             // You can then flickrSlider.trigger("slide",2)
  85.             // to go to the second slide:
  86.            
  87.             flickrSlider.bind('slide',function(e,slide){
  88.                 if(slide < 1 || slide > cnt || ul.is(':animated')){
  89.                     return false;
  90.                 }
  91.                
  92.                 ul.animate({
  93.                     left:-(slide-1)*options.width
  94.                 },{
  95.                     easing: 'easeInOutCirc',
  96.                     duration: 1000
  97.                 });
  98.                
  99.                 if(options.captions){
  100.                    
  101.                     // Animating the transition between
  102.                     // the captions (if enabled):
  103.                    
  104.                     caption.fadeOut(150,function(){
  105.                         caption.html(r.query.results.photo[slide-1].title);
  106.                     }).fadeIn(150);
  107.                 }
  108.                
  109.                 currentPos = slide;
  110.             });
  111.            
  112.             var arrows = $('<div>',{
  113.                 className: 'arrows'
  114.             });
  115.            
  116.             // Creating the previous / next arrows, and
  117.             // binding event listeners for the click events:
  118.            
  119.             var arrowPrev = $('<a>',{
  120.                 className: 'previous',
  121.                 href: '#',
  122.                 click : function(){
  123.                     var toShow = currentPos - 1;
  124.                     if(toShow < 1){
  125.                         toShow = cnt;
  126.                     }
  127.                    
  128.                     flickrSlider.trigger('slide',[toShow]);
  129.                     return false;
  130.                 }
  131.             }).appendTo(arrows);
  132.  
  133.             var arrowNext = $('<a>',{
  134.                 className: 'next',
  135.                 href: '#',
  136.                 click : function(){
  137.                     var toShow = currentPos + 1;
  138.                     if(toShow > cnt){
  139.                         toShow = 1;
  140.                     }
  141.                    
  142.                     flickrSlider.trigger('slide',[toShow]);
  143.                     return false;
  144.                 }
  145.             }).appendTo(arrows);
  146.            
  147.             arrows.appendTo(flickrSlider);
  148.  
  149.             // Showing the first slide by default:
  150.            
  151.             flickrSlider.trigger('slide',[1]);
  152.            
  153.             if(options.autoAdvance){
  154.                
  155.                 // If auto advance is enabled, listen for
  156.                 // the load event and schedule a periodic slide change.
  157.                 //
  158.                 // Read more here:
  159.                 // http://tutorialzine.com/2011/01/how-to-make-auto-advancing-slideshows/
  160.                
  161.                 $(window).load(function(){
  162.  
  163.                     $.fn.jqFlick.timeOut = null;
  164.                
  165.                     arrowPrev.add(arrowNext).click(function(e,simulated){
  166.                         if(!simulated){
  167.                             clearTimeout($.fn.jqFlick.timeOut);
  168.                         }
  169.                     });
  170.                
  171.                     (function autoAdvance(){
  172.                         if($.fn.jqFlick.timeOut){
  173.                             arrowNext.trigger('click',[true]);
  174.                         }
  175.                         $.fn.jqFlick.timeOut = setTimeout(autoAdvance,options.advancePeriod);      
  176.                     })();
  177.                 });
  178.             }
  179.         });
  180.  
  181.         // This custom event removes all event listeners,
  182.         // and empties the slider holder:
  183.  
  184.         flickrSlider.bind('jqFlickRemove',function(){
  185.             if($.fn.jqFlick.timeOut){
  186.                 clearTimeout($.fn.jqFlick.timeOut);
  187.             }
  188.            
  189.             flickrSlider.empty().unbind('jqFlickRemove slide');
  190.            
  191.         });
  192.  
  193.         return flickrSlider;
  194.        
  195.     };
  196.    
  197.     // Helper function for replacing "{KEYWORD}" with
  198.     // the respectful values of an object:
  199.    
  200.     function templateReplace(template,data){
  201.         return template.replace(/{([^}]+)}/g,function(match,group){
  202.             return data[group.toLowerCase()];
  203.         });
  204.     }
  205.    
  206.     // A custom easing functon. For more info visit:
  207.     // http://gsgd.co.uk/sandbox/jquery/easing/
  208.    
  209.     $.easing.easeInOutCirc = function (x, t, b, c, d) {
  210.         if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
  211.         return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
  212.     };
  213.    
  214. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement