Guest User

Untitled

a guest
Jun 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. (function($) {
  2. $.fn.billboard = function(options) {
  3. $.fn.billboard.settings = $.extend({}, $.fn.billboard.defaults, options);
  4.  
  5. return this.each(function() {
  6. var context = $(this), settings = $.fn.billboard.settings, scroller = settings.scroller;
  7. var controller = new $.fn.billboard.controller(settings.scrollingElements);
  8.  
  9. $.fn.billboard.init(scroller, context); // Initialize the billboard
  10.  
  11. if (settings.arrows) {
  12. $.each(settings.arrows, function() {
  13. var arrow = $(this);
  14. arrow.bind('click', function() {
  15. if (arrow[0] === settings.arrows.prev[0]) {
  16. scroller.animate({scrollLeft: scroller.scrollLeft() + controller.prevBoard.position().left}, 300, function() {
  17. controller.currentBoard = controller.prevBoard;
  18. controller.prevBoard = controller.getPrevBoard();
  19. controller.nextBoard = controller.getNextBoard();
  20. if (settings.navigation) {
  21. var i = controller.getCurrentNav(settings.scrollingElements);
  22. settings.navigation.removeClass(settings.activeNavClass);
  23. settings.navigation.eq(i).addClass(settings.activeNavClass);
  24. }
  25. });
  26. }
  27. if (arrow[0] === settings.arrows.next[0]) {
  28. scroller.animate({scrollLeft: scroller.scrollLeft() + controller.nextBoard.position().left}, 300, function() {
  29. controller.currentBoard = controller.nextBoard;
  30. controller.prevBoard = controller.getPrevBoard();
  31. controller.nextBoard = controller.getNextBoard();
  32. if (settings.navigation) {
  33. var i = controller.getCurrentNav(settings.scrollingElements);
  34. settings.navigation.removeClass(settings.activeNavClass);
  35. settings.navigation.eq(i).addClass(settings.activeNavClass);
  36. }
  37. });
  38. }
  39. return false;
  40. });
  41. });
  42. }
  43. if (settings.navigation) {
  44. settings.navigation.each(function(i) {
  45. var nav = $(this), selectedBoard;
  46. nav.bind('click', function() {
  47. selectedBoard = settings.scrollingElements.eq(i);
  48.  
  49. scroller.animate({scrollLeft: scroller.scrollLeft() + selectedBoard.position().left}, 300, function() {
  50. controller.currentBoard = selectedBoard;
  51. controller.prevBoard = controller.getPrevBoard();
  52. controller.nextBoard = controller.getNextBoard();
  53. });
  54.  
  55. settings.navigation.removeClass(settings.activeNavClass);
  56. nav.addClass(settings.activeNavClass);
  57. return false;
  58. });
  59. });
  60. }
  61. });
  62. };
  63.  
  64. $.fn.billboard.init = function(scroller, context) { // Initializes the billboard display
  65. var settings = $.fn.billboard.settings;
  66. if (settings.displayType === 'scroll') {
  67. scroller.css('overflow', 'hidden');
  68. settings.scrollerWindow.css('width', settings.scrollingElements.width() * settings.scrollingElements.length);
  69. scroller.scrollLeft(0);
  70. }
  71. if (settings.arrows) {
  72. settings.arrows.prev.css('outline', 'none');
  73. settings.arrows.next.css('outline', 'none');
  74. if (settings.hoverForArrows) {
  75. // Will not fade in navigation if browser is IE
  76. var animateArrows = jQuery.support.htmlSerialize;
  77. context.hover(function() {
  78. if (animateArrows) {
  79. $.each(settings.arrows, function() {
  80. var arrow = $(this);
  81. arrow.show().css('opacity', '0');
  82. arrow.stop().animate({ opacity: 1 }, 300);
  83. });
  84. } else {
  85. settings.arrows.prev.show();
  86. settings.arrows.next.show();
  87. }
  88. }, function() {
  89. if (animateArrows) {
  90. settings.arrows.prev.stop().animate({ opacity: 0 }, 300);
  91. settings.arrows.next.stop().animate({ opacity: 0 }, 300);
  92. } else {
  93. settings.arrows.prev.hide();
  94. settings.arrows.next.hide();
  95. }
  96. });
  97. } else {
  98. settings.arrows.prev.show();
  99. settings.arrows.next.show();
  100. }
  101. }
  102. if (settings.navigation) {
  103. settings.navigation.first().addClass(settings.activeNavClass);
  104. }
  105. };
  106.  
  107. $.fn.billboard.controller = function(boards) {
  108. this.firstBoard = boards.first();
  109. this.lastBoard = boards.last();
  110. this.currentBoard = this.firstBoard;
  111. this.nextBoard = this.currentBoard.next();
  112. this.prevBoard = this.lastBoard;
  113. };
  114.  
  115. $.fn.billboard.controller.prototype.getNextBoard = function() {
  116. if (this.currentBoard.next().length !== 0) {
  117. return this.currentBoard.next();
  118. } else {
  119. return this.firstBoard;
  120. }
  121. };
  122.  
  123. $.fn.billboard.controller.prototype.getPrevBoard = function() {
  124. if (this.currentBoard.prev().length !== 0) {
  125. return this.currentBoard.prev();
  126. } else {
  127. return this.lastBoard;
  128. }
  129. };
  130.  
  131. $.fn.billboard.controller.prototype.getCurrentNav = function(boards) {
  132. var currentBoard = this.currentBoard, eq;
  133. $.each(boards, function(i, board) {
  134. if (currentBoard[0] === board) {
  135. eq = i;
  136. }
  137. });
  138. return eq;
  139. }
  140. // default properties
  141. $.fn.billboard.defaults = {
  142. activeNavClass: 'active',
  143. displayType: 'scroll', // options are fade and scroll
  144. hoverForArrows: false
  145. };
  146.  
  147. })(jQuery);
Add Comment
Please, Sign In to add comment