/Assets/Scripts/cycle.js

https://github.com/sirbrad/Banner-Cycle · JavaScript · 136 lines · 63 code · 30 blank · 43 comment · 6 complexity · c985d1241135b4daed97c8419faf4a10 MD5 · raw file

  1. /* Cycle.js
  2. **********************************************************************/
  3. /*
  4. * Cycling your way through a nice batch of banners
  5. * Github: https://github.com/sirbrad
  6. * Twitter: http://twitter.com/#!/Bradleyfew
  7. * Any questions, hit me up on twitter.
  8. */
  9. (function cycle(global) {
  10. var container = document.getElementById('cycle'),
  11. lis = container.getElementsByTagName('li'),
  12. len = lis.length,
  13. arr = [],
  14. stopper = false,
  15. margBtm;
  16. // Determing how many banners we want visible at a given time is simple.
  17. var count = 4; // Determine how many banners should be visible
  18. // If count is set to be more or equal to the length of our elements then return the function
  19. // as there is no point in doing anything.
  20. if (count >= len) {
  21. stopper = true;
  22. return false;
  23. }
  24. // Hide the banners that shouldn't be visible
  25. while(len--) {
  26. if (len > count-1) {
  27. lis[len].className = 'hide';
  28. }
  29. }
  30. /**
  31. * The toCamelCase method takes a hyphenated value and converts it into a camel case equivalent.
  32. * e.g. margin-left becomes marginLeft.
  33. * Hyphens are removed, and each word after the first begins with a capital letter.
  34. *
  35. * @param hyphenatedValue { String } hyphenated string to be converted
  36. * @return result { String } the camel case version of the string argument
  37. */
  38. var toCamelCase = function(hyphenatedValue) {
  39. var result = hyphenatedValue.replace(/-\D/g, function(character) {
  40. return character.charAt(1).toUpperCase();
  41. });
  42. return result;
  43. };
  44. /**
  45. * The toHyphens method performs the opposite conversion, taking a camel case string and converting it into a hyphenated one.
  46. * e.g. marginLeft becomes margin-left
  47. *
  48. * @param camelCaseValue { String } camel cased string to be converted
  49. * @return result { String } the hyphenated version of the string argument
  50. */
  51. var toHyphens = function(camelCaseValue) {
  52. var result = camelCaseValue.replace(/[A-Z]/g, function(character) {
  53. return ('-' + character.charAt(0).toLowerCase());
  54. });
  55. return result;
  56. };
  57. // Get Styles of particular attr
  58. var getStyle = (function(element) {
  59. if (window.getComputedStyle) {
  60. // W3C specific method. Expects a style property with hyphens
  61. return function(element, styleName) {
  62. return element.ownerDocument.defaultView.getComputedStyle(element, null).getPropertyValue(toHyphens(styleName));
  63. }
  64. } else if (element.currentStyle) {
  65. // Internet Explorer-specific method. Expects style property names in camel case
  66. return function(element, styleName) {
  67. return element.currentStyle[toCamelCase(styleName)];
  68. }
  69. }
  70. }(document.body));
  71. margBtm = getStyle(lis[0], 'marginBottom');
  72. // Get style returns the value with px on the end. So we just grab the number.
  73. margBtm = parseInt(margBtm);
  74. function run() {
  75. var firstElem = lis[0],
  76. len = lis.length,
  77. cloned;
  78. // Add exact padding to the container, so nothing jumps
  79. container.style.paddingTop = (firstElem.clientHeight + margBtm) + 'px';
  80. // Apply class as it's more effecient. This class sets; position:absolute; top:0;
  81. firstElem.className = 'firstElem';
  82. // Clone first element
  83. cloned = firstElem.cloneNode(true);
  84. // Pull in the first hidden element
  85. lis[count].className = 'show';
  86. jQuery(lis[count]).animate({opacity:1});
  87. // Above we are added a show class to the next element we bring in,
  88. // Now we must remove that class by tracking back 1 and setting it to blank
  89. // It was conflicting with the first class we set to take our first element out
  90. // of the page!
  91. lis[count-1].className = '';
  92. // Animate the paddingTop of our container
  93. jQuery(container).animate({paddingTop:0}, 400);
  94. // Animate the opacity of our element
  95. jQuery(firstElem).animate({opacity:0}, 1000, function() {
  96. // Remove our element
  97. container.removeChild(firstElem);
  98. container.appendChild(cloned);
  99. lis[len-1].className = 'hide';
  100. });
  101. }
  102. // This runs only if our count is less than the lis length
  103. if (!stopper) {
  104. var timer = window.setInterval(run, 3000);
  105. }
  106. }(this));