/public/assets/js/jquery.innerfade.js

https://github.com/aaycock/dashboard · JavaScript · 128 lines · 88 code · 7 blank · 33 comment · 30 complexity · b58b3be431f46fac9273d0e707d73475 MD5 · raw file

  1. /* =========================================================
  2. // jquery.innerfade.js
  3. // Datum: 2008-02-14
  4. // Firma: Medienfreunde Hofmann & Baldes GbR
  5. // Author: Torsten Baldes
  6. // Mail: t.baldes@medienfreunde.com
  7. // Web: http://medienfreunde.com
  8. // based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/
  9. // and Ralf S. Engelschall http://trainofthoughts.org/
  10. *
  11. * <ul id="news">
  12. * <li>content 1</li>
  13. * <li>content 2</li>
  14. * <li>content 3</li>
  15. * </ul>
  16. *
  17. * $('#news').innerfade({
  18. * animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'),
  19. * speed: Fading-/Sliding-Speed in milliseconds or keywords (slow, normal or fast) (Default: 'normal'),
  20. * timeout: Time between the fades in milliseconds (Default: '2000'),
  21. * type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'),
  22. * containerheight: Height of the containing element in any css-height-value (Default: 'auto'),
  23. * runningclass: CSS-Class which the container get�s applied (Default: 'innerfade'),
  24. * children: optional children selector (Default: null)
  25. * });
  26. *
  27. // ========================================================= */
  28. (function($) {
  29. $.fn.innerfade = function(options) {
  30. return this.each(function() {
  31. $.innerfade(this, options);
  32. });
  33. };
  34. $.innerfade = function(container, options) {
  35. var settings = {
  36. 'animationtype': 'fade',
  37. 'speed': 'normal',
  38. 'type': 'sequence',
  39. 'timeout': 2000,
  40. 'containerheight': 'auto',
  41. 'runningclass': 'innerfade',
  42. 'children': null
  43. };
  44. if (options)
  45. $.extend(settings, options);
  46. if (settings.children === null)
  47. var elements = $(container).children();
  48. else
  49. var elements = $(container).children(settings.children);
  50. if (elements.length > 1) {
  51. $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
  52. for (var i = 0; i < elements.length; i++) {
  53. $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
  54. };
  55. if (settings.type == "sequence") {
  56. setTimeout(function() {
  57. $.innerfade.next(elements, settings, 1, 0);
  58. }, settings.timeout);
  59. $(elements[0]).show();
  60. } else if (settings.type == "random") {
  61. var last = Math.floor ( Math.random () * ( elements.length ) );
  62. setTimeout(function() {
  63. do {
  64. current = Math.floor ( Math.random ( ) * ( elements.length ) );
  65. } while (last == current );
  66. $.innerfade.next(elements, settings, current, last);
  67. }, settings.timeout);
  68. $(elements[last]).show();
  69. } else if ( settings.type == 'random_start' ) {
  70. settings.type = 'sequence';
  71. var current = Math.floor ( Math.random () * ( elements.length ) );
  72. setTimeout(function(){
  73. $.innerfade.next(elements, settings, (current + 1) % elements.length, current);
  74. }, settings.timeout);
  75. $(elements[current]).show();
  76. } else {
  77. alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
  78. }
  79. }
  80. };
  81. $.innerfade.next = function(elements, settings, current, last) {
  82. if (settings.animationtype == 'slide') {
  83. $(elements[last]).slideUp(settings.speed);
  84. $(elements[current]).slideDown(settings.speed);
  85. } else if (settings.animationtype == 'fade') {
  86. $(elements[last]).fadeOut(settings.speed);
  87. $(elements[current]).fadeIn(settings.speed, function() {
  88. removeFilter($(this)[0]);
  89. });
  90. } else
  91. alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
  92. if (settings.type == "sequence") {
  93. if ((current + 1) < elements.length) {
  94. current = current + 1;
  95. last = current - 1;
  96. } else {
  97. current = 0;
  98. last = elements.length - 1;
  99. }
  100. } else if (settings.type == "random") {
  101. last = current;
  102. while (current == last)
  103. current = Math.floor(Math.random() * elements.length);
  104. } else
  105. alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
  106. setTimeout((function() {
  107. $.innerfade.next(elements, settings, current, last);
  108. }), settings.timeout);
  109. };
  110. })(jQuery);
  111. // **** remove Opacity-Filter in ie ****
  112. function removeFilter(element) {
  113. if(element.style.removeAttribute){
  114. element.style.removeAttribute('filter');
  115. }
  116. }