Advertisement
Guest User

doggcantevenjsbro

a guest
Jan 30th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. /*!
  2. * jQuery Carousel
  3. *
  4. * By Kieran Marshall
  5. */
  6.  
  7. function Carousel()
  8. {
  9. /**
  10. * The carousel element.
  11. *
  12. */
  13. this.carousel = null;
  14.  
  15. /**
  16. * The button to scroll the carousel to the left
  17. *
  18. */
  19. this.leftButton = null;
  20.  
  21. /**
  22. * The button to scroll the carousel to the right
  23. *
  24. */
  25. this.rightButton = null;
  26.  
  27. /**
  28. * All of the items in the carousel
  29. *
  30. */
  31. this.items = [];
  32.  
  33. /**
  34. * The width of each item
  35. *
  36. */
  37. this.itemWidth = 0;
  38.  
  39. /**
  40. * The number of items that should be visible at any given time.
  41. *
  42. */
  43. this.itemsVisible = 0;
  44.  
  45. /**
  46. * The index of the first visible item.
  47. *
  48. */
  49. this.itemIndex = 0;
  50.  
  51. /**
  52. * The page buttons for the carousel
  53. *
  54. */
  55. this.buttons = [];
  56.  
  57. this.hasInterval = false;
  58. this.interval = null;
  59.  
  60. /**
  61. * Initiate the carousel
  62. *
  63. * @return null
  64. */
  65. this.init = function (element, leftButton, rightButton)
  66. {
  67. this.carousel = $(element);
  68.  
  69. this.items = this.carousel.children('li');
  70. this.itemWidth = this.items.eq(0).outerWidth(false);
  71.  
  72. this.leftButton = (leftButton == null) ? null
  73. : $(leftButton)
  74. ;
  75.  
  76. this.rightButton = (rightButton == null) ? null
  77. : $(rightButton)
  78. ;
  79.  
  80. var obj = this;
  81.  
  82. // If we've been given a left button, assign the click function.
  83. if(this.leftButton != null)
  84. this.leftButton.click( function(e) { obj.slideLeft(); e.preventDefault(); });
  85.  
  86. // If we've been given a right button, assign the click function.
  87. if(this.rightButton != null)
  88. this.rightButton.click( function(e) { obj.slideRight(); e.preventDefault(); });
  89.  
  90.  
  91. // Add an event handler to the resizing of the window to fix the carousel
  92. $(window).resize(function() {
  93. obj.adapt();
  94. });
  95.  
  96. // Adapt the carousel so that it fits the right amount of items on the
  97. // screen.
  98. this.adapt();
  99. }
  100.  
  101. /**
  102. * Slide the carousel to the left.
  103. *
  104. * @return null
  105. */
  106. this.slideLeft = function ()
  107. {
  108. // If the carousel is already at the first item
  109. this.itemIndex = (this.itemIndex == 0) ? this.items.length - this.itemsVisible
  110. : this.itemIndex - this.itemsVisible;
  111.  
  112. this.itemIndex = (this.itemIndex < 0) ? 0
  113. : this.itemIndex;
  114.  
  115. // Animate the slider
  116. this.animate();
  117. }
  118.  
  119. /**
  120. * Slide the carousel to the right
  121. *
  122. * @return null
  123. */
  124. this.slideRight = function ()
  125. {
  126. // If the carousel is already at the last item
  127. this.itemIndex = (this.itemIndex == this.items.length - this.itemsVisible) ? 0
  128. : this.itemIndex + this.itemsVisible;
  129.  
  130. // If the index is greater than the the number of visible items, put it to the
  131. // right amount.
  132. this.itemIndex = (this.itemIndex > (this.items.length - this.itemsVisible)) ? this.items.length - this.itemsVisible
  133. : this.itemIndex;
  134.  
  135. // Animate the slider
  136. this.animate();
  137. }
  138.  
  139. /**
  140. * Animate the slider to the newly set index
  141. *
  142. * @return null
  143. */
  144. this.animate = function ()
  145. {
  146. this.carousel.animate({
  147. left: 0 - (this.itemIndex * this.itemWidth)
  148. } , {
  149. duration: 250,
  150. queue: false
  151. });
  152.  
  153.  
  154. // Update the buttons (if we have any)
  155. if(this.buttons.length > 0)
  156. {
  157. this.buttons.each(function() { $(this).removeClass('current'); });
  158. this.buttons.eq(this.itemIndex).addClass('current');
  159. }
  160. }
  161.  
  162. /**
  163. * Fix the slider when the carousel adapts to a given width
  164. *
  165. * @return null
  166. */
  167. this.fix = function ()
  168. {
  169. //console.log(this.itemIndex + " " + this.items.length+ " " + this.itemsVisible);
  170. this.itemIndex = (this.itemIndex > (this.items.length - this.itemsVisible)) ? this.items.length - this.itemsVisible
  171. : 0;
  172.  
  173. var maxLocation = this.itemIndex * this.itemWidth;
  174.  
  175. this.carousel.css('left', 0 - maxLocation);
  176. }
  177.  
  178. /**
  179. * Adapt the carousel on resize
  180. *
  181. */
  182. this.adapt = function ()
  183. {
  184.  
  185. this.itemWidth = this.items.eq(0).outerWidth();
  186. var maxWidth = this.carousel.parent().parent().width();
  187.  
  188. maxWidth = (this.leftButton == null) ? maxWidth
  189. : maxWidth - this.leftButton.width();
  190.  
  191. maxWidth = (this.rightButton == null) ? maxWidth
  192. : maxWidth - this.rightButton.width();
  193.  
  194. this.itemsVisible = Math.floor(maxWidth / this.itemWidth);
  195. this.itemsVisible = (this.itemsVisible == 0) ? 1
  196. : this.itemsVisible
  197. ;
  198.  
  199. var newWidth = this.itemsVisible * this.itemWidth;
  200. newWidth = (newWidth == 0) ? this.itemWidth
  201. : newWidth;
  202.  
  203. this.carousel.parent().css('width', newWidth);
  204.  
  205. this.fix();
  206. }
  207.  
  208. /**
  209. * Add page buttons to the carousel
  210. *
  211. * @return null
  212. */
  213. this.setButtons = function (elem)
  214. {
  215. var obj = this;
  216.  
  217.  
  218. this.buttons = $(elem).find('a');
  219.  
  220. this.buttons.each( function() {
  221. $(this).click( function(e) {
  222. $(obj.buttons).each(function() { $(this).removeClass('current') });
  223.  
  224. $(this).addClass('current');
  225.  
  226. obj.itemIndex = $(this).parent().index();
  227. obj.animate();
  228.  
  229. e.preventDefault();
  230. });
  231. });
  232.  
  233. // Set the first item to have the "current" class
  234. this.buttons.eq(0).addClass('current');
  235. }
  236.  
  237. /**
  238. * Set an interval for the carousel to automatically move on
  239. *
  240. * @return null
  241. */
  242. this.addInterval = function ( ms )
  243. {
  244. var obj = this;
  245. this.hasInterval = true;
  246.  
  247. this.carousel.hover( function() {
  248. clearInterval(obj.interval);
  249. } , function() {
  250. if(this.hasInterval) {
  251. obj.doInterval( ms );
  252. }
  253. });
  254.  
  255. this.doInterval( ms );
  256. }
  257.  
  258. this.doInterval = function( ms )
  259. {
  260. var obj = this;
  261.  
  262. this.interval = window.setInterval( function() {
  263. obj.slideRight();
  264. }, ms);
  265. }
  266.  
  267. this.stopInterval = function() {
  268. this.hasInterval = false;
  269. clearInterval(this.interval);
  270. }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement