Advertisement
rcain

promo_slider.js - v3.1.4 - bug fixes

Dec 13th, 2011
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 8.20 KB | None | 0 0
  1. //mod jrc 131211: systemcore.co.uk - recommended bug fixes, various. see mod marks 'mod jrc ...'
  2. //based on code: http://wordpress.org/extend/plugins/promotion-slider/
  3. //based on version: V3.1.4
  4. //problem reference: http://wordpress.org/support/topic/plugin-promotion-slider-browser-scroll-bug-using-image-width-auto-suggested-fix  
  5.  
  6.  
  7. // Function returns a random number between 1 and n
  8. function rand(n){return(Math.floor(Math.random()*n+1));}
  9.  
  10. jQuery(document).ready(function(){
  11.  
  12.   // Disable HTML title attribute for slider images
  13.   jQuery('.promo_slider img').removeAttr('title');
  14.  
  15.   // Get all instances of promo_slider on the page
  16.   var sliders = jQuery('.promo_slider_wrapper');
  17.  
  18.   // Cycle through each slider
  19.   jQuery.each(sliders, function(){
  20.    
  21.     // Define current slider
  22.     var currentSlider = jQuery(this);
  23.     var thisSlider = jQuery('.promo_slider', currentSlider);
  24.    
  25.     // Get all panels
  26.     var panels = jQuery('.panel', thisSlider);
  27.    
  28.     // Get total count of panels
  29.     var panelCount = panels.length;
  30.    
  31.     // Set number for first panel
  32. //mod jrc 121211 - fix no options bug
  33. // orig::   if( promo_slider_options.startOn == 'first' ) var initialPanel = 1;
  34.     if( promo_slider_options.start_on == 'first' ) var initialPanel = 1;
  35. //end mod jrc 121211
  36.     else var initialPanel = rand(panelCount);
  37.     if( currentSlider.hasClass('random') ) initialPanel = rand(panelCount);
  38.     if( currentSlider.hasClass('first') ) initialPanel = 1;
  39.    
  40.     // Should we pause the slider on mouseover?
  41.     var pauseOnMouseover;
  42.     if( currentSlider.hasClass('pause') ) pauseOnMouseover = true;
  43.     else pauseOnMouseover = false;
  44.    
  45.     // Assign variable for setInterval
  46.     var sliderInterval;
  47.    
  48.     // Set time delay
  49. //mod jrc 111211 - fix no options bug
  50.     // orig:: var timeDelay = promo_slider_options.timeDelay;
  51.     var timeDelay = promo_slider_options.time_delay;
  52. //end mod jrc 111211
  53.  
  54.     if( jQuery('.promo_slider_time_delay', thisSlider).html() ){
  55.         timeDelay = jQuery('.promo_slider_time_delay', thisSlider).html();
  56.     }
  57.    
  58.     // Set auto advance variable
  59. //mod jrc 121211 - fix no options bug
  60. //orig:     var autoAdvance = promo_slider_options.autoAdvance;
  61.     var autoAdvance = promo_slider_options.auto_advance;
  62. //end mod jrc 121211
  63.     if( thisSlider.hasClass('auto_advance') ) autoAdvance = true;
  64.     if( thisSlider.hasClass('no_auto_advance') ) autoAdvance = false;
  65.     if( panelCount < 2 ) autoAdvance = false;
  66.    
  67.     // Set navigation option
  68.     var navOption = promo_slider_options.nav_option;
  69.     if( currentSlider.hasClass('default_nav') ) navOption = 'default';
  70.     else if( currentSlider.hasClass('fancy_nav') ) navOption = 'fancy';
  71.     else if( currentSlider.hasClass('links_nav') ) navOption = 'links';
  72.     else if( currentSlider.hasClass('thumb_nav') ) navOption = 'thumb';
  73.     else navOption = false;
  74.    
  75.     // Hide all panels
  76.     panels.hide();
  77.    
  78.     // Show initial panel and add class 'current' to active panel
  79.     jQuery('.panel-' + initialPanel, currentSlider).show().addClass('current');
  80.    
  81.     if(panelCount > 1 && (navOption == 'default' || navOption == 'fancy' || navOption == 'thumb') ){
  82.  
  83.       jQuery('.promo_slider_nav').show();
  84.       jQuery('.promo_slider_thumb_nav').show();
  85.    
  86.       if(navOption == 'fancy' || navOption == 'default'){
  87.           // Generate HTML for navigation
  88.           var navHTML = '';
  89.           jQuery.each(panels, function(index, object){
  90.             // Set panel title
  91.             panelTitle = jQuery('.panel-'+(index+1)+' span.panel-title', currentSlider).html();
  92.               newSpan = '<span class="'+(index+1)+'" title="'+panelTitle+'">'+(index+1)+'</span>';
  93.               if( (index + 1) != panelCount){newSpan = newSpan + '<b class="promo_slider_sep"> | </b>';}
  94.             navHTML = navHTML + newSpan;
  95.           });
  96.          
  97.           // Insert HTML into nav
  98.           jQuery('.slider_selections', currentSlider).html(navHTML);
  99.       }
  100.      
  101.       // Set click functions for each span in the slider nav
  102.       var slideNav = jQuery('.slider_selections span', currentSlider);
  103.       jQuery.each(slideNav, function(index, object){
  104.         jQuery(object).click(function(){
  105.           clearInterval(sliderInterval);
  106.           if( !jQuery(object).hasClass('current') ) progress(jQuery(object).attr('class'), currentSlider, panelCount);
  107. //mod jrc 111211 - fix timing bugs::
  108. //not reqd: orig:: if(autoAdvance) sliderInterval = setInterval(function(){progress('forward', currentSlider, panelCount);}, (timeDelay * 1000));
  109. //end mod jrc 111211
  110.         });
  111.       });
  112.      
  113.       // Set active span class to 'current'
  114.       jQuery('.slider_selections span[class=' + initialPanel + ']', currentSlider).addClass('current');
  115.      
  116.     }
  117.  
  118.     // Create click functions for navigational elements
  119.     jQuery('.move_forward', currentSlider).click(function(){
  120.       clearInterval(sliderInterval);
  121.       progress('forward', currentSlider, panelCount);
  122. //mod jrc 111211 - fix timing bugs::
  123. //not reqd: orig::    if(autoAdvance) sliderInterval = setInterval(function(){progress('forward', currentSlider, panelCount);}, (timeDelay * 1000));
  124. //end mod jrc 111211
  125.     });
  126.     jQuery('.move_backward', currentSlider).click(function(){
  127.       clearInterval(sliderInterval);
  128.       progress('backward', currentSlider, panelCount);
  129. //mod jrc 111211 - fix timing bugs::
  130. //not reqd: orig::    if(autoAdvance) sliderInterval = setInterval(function(){progress('forward', currentSlider, panelCount);}, (timeDelay * 1000));
  131. //end mod jrc 111211
  132.     });
  133.    
  134.     if( autoAdvance ){
  135.  
  136.         // Begin auto advancement of slides
  137.         sliderInterval = setInterval(function(){progress('forward', currentSlider, panelCount);}, (timeDelay * 1000));
  138.    
  139.         if( pauseOnMouseover ){
  140.        
  141.             // Pause slide advancement on mouseover
  142.             jQuery(thisSlider).mouseover(function(){
  143.                 clearInterval(sliderInterval);
  144.             });
  145.        
  146.             // Continue slide advancement on mouseout
  147.             jQuery(thisSlider).mouseout(function(){
  148.                 sliderInterval = setInterval(function(){progress('forward', currentSlider, panelCount);}, (timeDelay * 1000));
  149.             });
  150.        
  151.         }
  152.        
  153.     }
  154.    
  155.   });
  156.  
  157.   // Progress to selected panel
  158.   function progress(value, currentSlider, panelCount){
  159.  
  160.     //mod jrc 111211 - fix broswer scoller jag on css auto-sized images
  161.     var scl_h = currentSlider.height();
  162.      var scl_old_scrollTop = jQuery(window).scrollTop();
  163.     //alert(' - hi jrc - scl_old_scrollTop=|||>'+scl_old_scrollTop+'<|||- ');
  164.     //end mod jrc 111211
  165.          
  166.       // Find number of current panel
  167.       var currentValue = jQuery('div.promo_slider > .panel', currentSlider).index(jQuery('div.panel.current', currentSlider)) + 1;
  168.  
  169.       // Set value of new panel
  170.       if(value == 'forward'){
  171.         var newValue = currentValue + 1;
  172.         if(newValue > panelCount){newValue = 1;}
  173.       }
  174.       else if(value == 'backward'){
  175.         var newValue = currentValue - 1;
  176.         if(newValue == 0){newValue = panelCount;}
  177.       }
  178.       else{
  179.         var newValue = value;
  180.       }
  181.      
  182.       // Assign variables for ease of use
  183.       var currentItem = jQuery('.panel-' + currentValue, currentSlider);
  184.       var newItem = jQuery('.panel-' + newValue, currentSlider);
  185.       var currentSpan = jQuery('.slider_selections span.current', currentSlider);
  186.       var newSpan = jQuery('.slider_selections span.' + newValue, currentSlider);
  187.  
  188.  
  189. //mod jrc 111211 - fix timing bugs:: move to (queue up) inside event callbacks
  190.       // Add / Remove classes
  191. /*
  192.       currentItem.removeClass('current');
  193.       newItem.addClass('current');
  194.       currentSpan.removeClass('current');
  195.       newSpan.addClass('current');
  196. */
  197. //end mod jrc 111211
  198.      
  199.       // Fade in / out
  200.       currentItem.fadeOut('fast', function(){
  201.  
  202. //mod jrc 111211 - fix timing bugs:: move to (queue up inside) inside event callbacks
  203.       // Add / Remove classes
  204.       currentItem.removeClass('current');
  205.       newItem.addClass('current');
  206.       currentSpan.removeClass('current');
  207.       newSpan.addClass('current');
  208. //end mod jrc 111211
  209.  
  210. //mod jrc 111211 - fix css focus highlight bug on last image on nav doubleclick
  211.     if(document.selection && document.selection.empty) {
  212.         //document.selection.empty(); //may be required for older versions of IE, BUT breaks IE9!
  213.     } else if(window.getSelection) {
  214.         var sel = window.getSelection();
  215.         sel.removeAllRanges();
  216.     }
  217. //mod jrc 111211
  218.  
  219. // mod jrc 111211 - nest callback - orig::      newItem.fadeIn('fast');
  220.         newItem.fadeIn('fast', function(){
  221.             });
  222.  
  223. //mod jrc 111211 - fix browser scoller jag on css auto-sized images
  224.         jQuery(window).scrollTop(scl_old_scrollTop);
  225. //end mod jrc 111211
  226.  
  227.       });
  228.  
  229.   }
  230.  
  231. });
  232.  
  233.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement