/Assets/Scripts/cycle.jquery.js

https://github.com/sirbrad/Banner-Cycle · JavaScript · 167 lines · 86 code · 40 blank · 41 comment · 7 complexity · 253f7c8fd84473f051f2b7bd4d606e99 MD5 · raw file

  1. ;(function($, window, document, undefined){
  2. // Creating the defaults once
  3. var pluginName = 'cycling',
  4. defaults = {
  5. count: 4, // Determine how many elements to show
  6. delay: 3000 // Determine the delay time
  7. };
  8. // Plugin constructor
  9. function Plugin(element, options) {
  10. this.element = element;
  11. this.options = $.extend({}, defaults, options);
  12. this._name = pluginName;
  13. this.init();
  14. };
  15. // Initialization
  16. Plugin.prototype.init = function () {
  17. this.lis = this.element.getElementsByTagName('li');
  18. this.stopper = false;
  19. // Variables
  20. var len = this.lis.length,
  21. arr = [],
  22. margBtm = 0; // Didn't like me setting an empty variable?
  23. // If count is set to be more or equal to the length of our elements then return the function
  24. // as there is no point in doing anything.
  25. if (this.options.count >= len) {
  26. this.stopper = true;
  27. return false;
  28. }
  29. // Hide the banners that shouldn't be visible
  30. while(len--) {
  31. if (len > this.options.count-1) {
  32. this.lis[len].className = 'hide';
  33. }
  34. }
  35. /**
  36. * The toCamelCase method takes a hyphenated value and converts it into a camel case equivalent.
  37. * e.g. margin-left becomes marginLeft.
  38. * Hyphens are removed, and each word after the first begins with a capital letter.
  39. *
  40. * @param hyphenatedValue { String } hyphenated string to be converted
  41. * @return result { String } the camel case version of the string argument
  42. */
  43. var toCamelCase = function(hyphenatedValue) {
  44. var result = hyphenatedValue.replace(/-\D/g, function(character) {
  45. return character.charAt(1).toUpperCase();
  46. });
  47. return result;
  48. };
  49. /**
  50. * The toHyphens method performs the opposite conversion, taking a camel case string and converting it into a hyphenated one.
  51. * e.g. marginLeft becomes margin-left
  52. *
  53. * @param camelCaseValue { String } camel cased string to be converted
  54. * @return result { String } the hyphenated version of the string argument
  55. */
  56. var toHyphens = function(camelCaseValue) {
  57. var result = camelCaseValue.replace(/[A-Z]/g, function(character) {
  58. return ('-' + character.charAt(0).toLowerCase());
  59. });
  60. return result;
  61. };
  62. // Get Styles of particular attr
  63. var getStyle = (function(element) {
  64. if (window.getComputedStyle) {
  65. // W3C specific method. Expects a style property with hyphens
  66. return function(element, styleName) {
  67. return element.ownerDocument.defaultView.getComputedStyle(element, null).getPropertyValue(toHyphens(styleName));
  68. }
  69. } else if (element.currentStyle) {
  70. // Internet Explorer-specific method. Expects style property names in camel case
  71. return function(element, styleName) {
  72. return element.currentStyle[toCamelCase(styleName)];
  73. }
  74. }
  75. }(document.body));
  76. this.margBtm = getStyle(this.lis[0], 'marginBottom');
  77. // Get style returns the value with px on the end. So we just grab the number.
  78. this.margBtm = parseInt(this.margBtm);
  79. };
  80. $.fn[pluginName] = function (options) {
  81. return this.each(function() {
  82. if (!$.data(this, 'plugin_' + pluginName)) {
  83. $.data(this, 'plugin_' + pluginName, new Plugin(this, options));
  84. }
  85. // Store in a variable
  86. var constructor = $.data(this, 'plugin_' + pluginName);
  87. function run() {
  88. var firstElem = constructor.lis[0],
  89. len = constructor.lis.length,
  90. liCount = constructor.lis[constructor.options.count],
  91. cloned;
  92. // Add exact padding to the container, so nothing jumps
  93. $(this).css("padding-top", ($(firstElem).height() + constructor.margBtm) + 'px');
  94. // Apply class as it's more effecient. This class sets; position:absolute; top:0;
  95. $(firstElem).addClass('firstElem');
  96. // Clone first element
  97. cloned = $(firstElem).clone();
  98. // Pull in the first hidden element
  99. $(liCount).removeClass('hide').addClass('show');
  100. jQuery(liCount).animate({opacity:1});
  101. // Above we are added a show class to the next element we bring in,
  102. // Now we must remove that class by tracking back 1 and setting it to blank
  103. // It was conflicting with the first class we set to take our first element out
  104. // of the page!
  105. $(liCount).removeClass('show');
  106. // Animate the paddingTop of our container
  107. $(this).animate({paddingTop:0}, 400);
  108. // Animate the opacity of our element
  109. $(firstElem).animate({opacity:0}, 1000, function() {
  110. // Remove our element
  111. $(firstElem).remove();
  112. // Re-insert it
  113. $(self).append(cloned);
  114. // Then hide it
  115. $(constructor.lis[len-1]).removeAttr('class').addClass('hide');
  116. });
  117. };
  118. // This runs only if our count is less than the lis length
  119. if (!constructor.stopper) {
  120. var self = this,
  121. timer = window.setInterval(function(){
  122. run.call(self);
  123. }, constructor.options.delay);
  124. }
  125. });
  126. };
  127. })(jQuery, window, document);