fader.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. function smf_NewsFader(oOptions)
  2. {
  3. this.opt = oOptions;
  4. this.oFaderHandle = document.getElementById(this.opt.sFaderControlId);
  5. // Surround each item with... anything special?
  6. this.sItemTemplate = 'sItemTemplate' in this.opt ? this.opt.sItemTemplate : '%1$s';
  7. // Fade delay (in milliseconds).
  8. this.iFadeDelay = 'iFadeDelay' in this.opt ? this.opt.iFadeDelay : 5000;
  9. // The array that contains all the lines of the news for display.
  10. this.aFaderItems = 'aFaderItems' in this.opt ? this.opt.aFaderItems : [];
  11. // Should we look for fader data, still?
  12. this.bReceivedItemsOnConstruction = 'aFaderItems' in this.opt;
  13. // The current item in smfFadeContent.
  14. this.iFadeIndex = -1;
  15. // Just make sure the page is loaded before calling the init.
  16. var fader = this;
  17. $(document).ready(function() {fader.init();});
  18. }
  19. smf_NewsFader.prototype.init = function init()
  20. {
  21. var oForeEl, oForeColor, oBackEl, oBackColor;
  22. // Did we get our fader items on construction, or should we be gathering them instead?
  23. if (!this.bReceivedItemsOnConstruction)
  24. {
  25. // Get the news from the list in boardindex
  26. var oNewsItems = this.oFaderHandle.getElementsByTagName('li');
  27. // Fill the array that has previously been created
  28. for (var i = 0, n = oNewsItems.length; i < n; i ++)
  29. this.aFaderItems[i] = oNewsItems[i].innerHTML;
  30. }
  31. // Start the fader!
  32. this.fade();
  33. }
  34. // Main fading function... called 50 times every second.
  35. smf_NewsFader.prototype.fade = function fade()
  36. {
  37. if (this.aFaderItems.length <= 1)
  38. return;
  39. var currentText;
  40. // Starting out? Set up the first item.
  41. if (this.iFadeIndex == -1)
  42. {
  43. currentText = this.sItemTemplate.replace('%1$s', this.aFaderItems[0]);
  44. this.iFadeIndex = 1;
  45. }
  46. else
  47. {
  48. // Go to the next item, or first if we're out of items.
  49. currentText = this.sItemTemplate.replace('%1$s', this.aFaderItems[this.iFadeIndex ++]);
  50. if (this.iFadeIndex >= this.aFaderItems.length)
  51. this.iFadeIndex = 0;
  52. }
  53. $('#' + this.opt.sFaderControlId).each(function() {
  54. temp_elem = $(this).clone().css({height: 'auto'}).appendTo('body').html(currentText);
  55. final_height = parseInt(temp_elem.height()) + parseInt($(this).css('padding-top').replace(/[^-\d\.]/g, '')) + parseInt($(this).css('padding-bottom').replace(/[^-\d\.]/g, ''));
  56. temp_elem.remove();
  57. $(this).height($(this).height());
  58. }).html(currentText).animate({height: final_height}, 'slow');
  59. // Keep going.
  60. window.setTimeout(this.opt.sSelf + '.fade();', this.iFadeDelay);
  61. }