Advertisement
Guest User

innerfade.js

a guest
Mar 17th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* =========================================================
  2.  
  3. // jquery.innerfade.js
  4.  
  5. // Datum: 2008-02-14
  6. // Firma: Medienfreunde Hofmann & Baldes GbR
  7. // Author: Torsten Baldes
  8. // Mail: t.baldes@medienfreunde.com
  9. // Web: http://medienfreunde.com
  10.  
  11. // based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/
  12. // and Ralf S. Engelschall http://trainofthoughts.org/
  13.  
  14.  *
  15.  *  <ul id="news">
  16.  *      <li>content 1</li>
  17.  *      <li>content 2</li>
  18.  *      <li>content 3</li>
  19.  *  </ul>
  20.  *
  21.  *  jQuery('#news').innerfade({
  22.  *    animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'),
  23.  *    speed: Fading-/Sliding-Speed in milliseconds or keywords (slow, normal or fast) (Default: 'normal'),
  24.  *    timeout: Time between the fades in milliseconds (Default: '2000'),
  25.  *    type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'),
  26.  *      containerheight: Height of the containing element in any css-height-value (Default: 'auto'),
  27.  *    runningclass: CSS-Class which the container get’s applied (Default: 'innerfade'),
  28.  *    children: optional children selector (Default: null)
  29.  *  });
  30.  *
  31.  
  32. // ========================================================= */
  33.  
  34.  
  35. (function(jQuery) {
  36.  
  37.   jQuery.fn.innerfade = function(options) {
  38.     return this.each(function() {
  39.       jQuery.innerfade(this, options);
  40.     });
  41.   };
  42.  
  43.   jQuery.innerfade = function(container, options) {
  44.     var settings = {
  45.       'animationtype':    'fade',
  46.       'speed':            'normal',
  47.       'type':             'sequence',
  48.       'timeout':          2000,
  49.       'containerheight':  'auto',
  50.       'runningclass':     'innerfade',
  51.       'children':         null
  52.     };
  53.  
  54.     if (options) {
  55.       jQuery.extend(settings, options);
  56.     }
  57.  
  58.     if (settings.children === null) {
  59.       var elements = jQuery(container).children();
  60.     }
  61.     else {
  62.       var elements = jQuery(container).children(settings.children);
  63.     }
  64.  
  65.     if (elements.length > 1) {
  66.       jQuery(container).css('position', 'relative').css('overflow', 'hidden').addClass(settings.runningclass);
  67.       var biggestHeight=0;
  68.       for (var i = 0; i < elements.length; i++) {
  69.         jQuery(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
  70.         if (jQuery(elements[i]).height() > biggestHeight) {
  71.           biggestHeight = jQuery(elements[i]).height();
  72.         }
  73.       }
  74.       if (settings.type == "sequence") {
  75.         setTimeout(function() {
  76.           jQuery.innerfade.next(container,elements, settings, 1, 0);
  77.         }, settings.timeout);
  78.         jQuery(elements[0]).show();
  79.         jQuery(container).css('height', biggestHeight);
  80.       }
  81.       else if (settings.type == "random") {
  82.         var last = Math.floor ( Math.random () * ( elements.length ) );
  83.         setTimeout(function() {
  84.           do {
  85.             current = Math.floor ( Math.random () * ( elements.length ) );
  86.           } while (last == current );
  87.           jQuery.innerfade.next(container,elements, settings, current, last);
  88.         }, settings.timeout);
  89.         jQuery(elements[last]).show();
  90.       }
  91.       else if ( settings.type == 'random_start' ) {
  92.         settings.type = 'sequence';
  93.         var current = Math.floor ( Math.random () * ( elements.length ) );
  94.         setTimeout(function(){
  95.           jQuery.innerfade.next(container,elements, settings, (current + 1) %  elements.length, current);
  96.         }, settings.timeout);
  97.         jQuery(elements[current]).show();
  98.       }
  99.       else {
  100.         alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
  101.       }
  102.     }
  103.   };
  104.  
  105.   jQuery.innerfade.next = function(container,elements, settings, current, last) {
  106.     if (settings.animationtype == 'slide') {
  107.       if(jQuery(elements[current]).outerHeight() > jQuery(container).outerHeight()) {
  108.         jQuery(container).css('height', jQuery(elements[current]).outerHeight());
  109.       }
  110.       jQuery(elements[last]).slideUp(settings.speed);
  111.       jQuery(elements[current]).slideDown(settings.speed);
  112.     }
  113.     else if (settings.animationtype == 'fade') {
  114.       jQuery(elements[last]).fadeOut(settings.speed);
  115.       jQuery(elements[current]).fadeIn(settings.speed, function() {
  116.         removeFilter(jQuery(this)[0]);
  117.       });
  118.     }
  119.     else {
  120.       alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
  121.     }
  122.  
  123.     if (settings.type == "sequence") {
  124.       if ((current + 1) < elements.length) {
  125.         current = current + 1;
  126.         last = current - 1;
  127.       }
  128.       else {
  129.         current = 0;
  130.         last = elements.length - 1;
  131.       }
  132.     }
  133.     else if (settings.type == "random") {
  134.       last = current;
  135.       while (current == last) {
  136.         current = Math.floor(Math.random() * elements.length);
  137.       }
  138.     }
  139.     else {
  140.       alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
  141.     }
  142.  
  143.     setTimeout((function() {
  144.       jQuery.innerfade.next(container,elements, settings, current, last);
  145.     }), settings.timeout);
  146.   };
  147.  
  148. })(jQuery);
  149.  
  150. // **** remove Opacity-Filter in ie ****
  151. function removeFilter(element) {
  152.   if(element.style.removeAttribute){
  153.     element.style.removeAttribute('filter');
  154.   }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement