Advertisement
kfrank

Fix to testimonialrotator.js in modulo theme

Jan 13th, 2013
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Quote Rotator - Simple jQuery plugin which cause a set of list
  3.  * items to fade-in and fade-out.
  4.  *
  5.  * Homepage: http://coryschires.com/jquery-quote-rotator-plugin/
  6.  * Source Code: https://github.com/coryschires/quote-rotator
  7.  *
  8.  * Copyright (c) 2011 Cory Schires (coryschires.com)
  9.  * Dual licensed under the MIT and GPL licenses:
  10.  * http://www.opensource.org/licenses/mit-license.php
  11.  * http://www.gnu.org/licenses/gpl.html
  12.  *
  13.  * Version: 1.0.0
  14.  * With tweeks by Keith Frank 1/12/13 to prevent multiple quotes showing at once if buttons are clicked to quickly
  15.  * list items(testimonials) must have their display set to none in a the stylesheet for my script modification to work correctly
  16.  * This also prevents all of the list items being displayed as a long list "onload".  
  17.  */
  18.  
  19.  
  20. (function($) {
  21.  
  22.   $.quote_rotator = {
  23.     defaults: {
  24.       rotation_speed: 5000,
  25.       pause_on_hover: true,
  26.       randomize_first_quote: false,
  27.       buttons: false
  28.     }
  29.   }
  30.  
  31.   $.fn.extend({
  32.     quote_rotator: function(config) {
  33.      
  34.       var config = $.extend({}, $.quote_rotator.defaults, config);
  35.      
  36.       return this.each(function() {
  37.         var rotation;
  38.         var quote_list = $(this);
  39.         var list_items = quote_list.find('li');
  40.         var rotation_active = true;
  41.         var buttons_click_enabled = true;
  42.         var rotation_speed = config.rotation_speed < 2000 ? 2000 : config.rotation_speed;
  43.        
  44.         var add_active_class = function() {
  45.           var active_class_not_already_applied = quote_list.find('li.active').length === 0;
  46.           if (config.randomize_first_quote) {
  47.             var random_list_item = $(list_items[Math.floor( Math.random() * (list_items.length) )]);
  48.             random_list_item.addClass('active');
  49.           } else if (active_class_not_already_applied) {
  50.               quote_list.find('li:first').addClass('active');
  51.           }
  52.         }();
  53.        
  54.         var get_next_quote = function(quote) {
  55.           return quote.next('li').length ? quote.next('li') : quote_list.find('li:first');
  56.         }
  57.        
  58.         var get_previous_quote = function(quote) {
  59.           return quote.prev('li').length ? quote.prev('li') : quote_list.find('li:last');
  60.         }
  61.        
  62.         var rotate_quotes = function(direction) {
  63.           var active_quote = quote_list.find('li.active');
  64.           var next_quote = direction === 'forward' ? get_next_quote(active_quote) : get_previous_quote(active_quote)
  65.          
  66.           active_quote.animate({
  67.             opacity: 0
  68.           }, 1000, function() {
  69.             active_quote.hide();
  70.             active_quote.removeClass('active');
  71.             next_quote.addClass('active');
  72.             list_items.css('opacity', 1);
  73.             next_quote.fadeIn(1000, function(){
  74.                 //begin registering button clicks again since our classes have been swapped and our fadeIn is complete
  75.                 buttons_click_enabled = true;
  76.             });
  77.           });
  78.        };
  79.        
  80.         var start_automatic_rotation = function() {
  81.           rotation = setInterval(function() {
  82.             if (rotation_active) { rotate_quotes('forward'); }
  83.           }, rotation_speed);
  84.         };
  85.  
  86.         var pause_rotation_on_hover = function() {
  87.           quote_list.hover(function() {
  88.             rotation_active = false;
  89.           }, function() {
  90.             rotation_active = true;
  91.           });
  92.         };
  93.        
  94.         var include_next_previous_buttons = function() {
  95.             quote_list.append(
  96.                 '<div class="qr_buttons">\
  97.                     <button class="qr_previous">'+ config.buttons.previous +'</button>\
  98.                     <button class="qr_next">'+ config.buttons.next +'</button>\
  99.                 </div>'
  100.             );
  101.             quote_list.find('button').click(function() {
  102.                 if(buttons_click_enabled) {
  103.                     buttons_click_enabled = false;// don't register button clicks until fadeIn is complete
  104.                     clearInterval(rotation);
  105.                     rotate_quotes( $(this).hasClass('qr_next') ? 'forward' : 'backward' );
  106.                     start_automatic_rotation();
  107.                 }
  108.                 else{
  109.                     return false;//animations have not finished so do nothing if button is clicked
  110.                 }
  111.             });
  112.         };
  113.        
  114.         if (config.buttons) { include_next_previous_buttons(); }
  115.         if (config.pause_on_hover) { pause_rotation_on_hover(); }
  116.        
  117.         //list_items.not('.active').hide();
  118.         //Commented out by KF: display of 'li' is now set to none in style sheet
  119.         list_items.each(function (){
  120.             if ($(this).hasClass('active')){
  121.                 $(this).show();
  122.             }
  123.         });
  124.        
  125.         start_automatic_rotation();
  126.       })
  127.     }
  128.   })
  129.  
  130. })(jQuery);
  131.  
  132. jQuery(document).ready(function() {
  133.     jQuery('#home-testimonials ul').quote_rotator({
  134.         rotation_speed: 15000,
  135.         pause_on_hover: true,
  136.         randomize_first_quote: true,
  137.         buttons: { next: '>>', previous: '<<' }
  138.     });
  139. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement