jquery.flexisel.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * File: jquery.flexisel.js
  3. * Version: 1.0.0
  4. * Description: Responsive carousel jQuery plugin
  5. * Author: 9bit Studios
  6. * Copyright 2012, 9bit Studios
  7. * http://www.9bitstudios.com
  8. * Free to use and abuse under the MIT license.
  9. * http://www.opensource.org/licenses/mit-license.php
  10. */
  11. (function ($) {
  12. $.fn.flexisel = function (options) {
  13. var defaults = $.extend({
  14. visibleItems: 4,
  15. animationSpeed: 200,
  16. autoPlay: false,
  17. autoPlaySpeed: 3000,
  18. pauseOnHover: true,
  19. setMaxWidthAndHeight: false,
  20. enableResponsiveBreakpoints: false,
  21. responsiveBreakpoints: {
  22. portrait: {
  23. changePoint:480,
  24. visibleItems: 1
  25. },
  26. landscape: {
  27. changePoint:640,
  28. visibleItems: 2
  29. },
  30. tablet: {
  31. changePoint:768,
  32. visibleItems: 3
  33. }
  34. }
  35. }, options);
  36. /******************************
  37. Private Variables
  38. *******************************/
  39. var object = $(this);
  40. var settings = $.extend(defaults, options);
  41. var itemsWidth; // Declare the global width of each item in carousel
  42. var canNavigate = true;
  43. var itemsVisible = settings.visibleItems;
  44. /******************************
  45. Public Methods
  46. *******************************/
  47. var methods = {
  48. init: function() {
  49. return this.each(function () {
  50. methods.appendHTML();
  51. methods.setEventHandlers();
  52. methods.initializeItems();
  53. });
  54. },
  55. /******************************
  56. Initialize Items
  57. *******************************/
  58. initializeItems: function() {
  59. var listParent = object.parent();
  60. var innerHeight = listParent.height();
  61. var childSet = object.children();
  62. var innerWidth = listParent.width(); // Set widths
  63. itemsWidth = (innerWidth)/itemsVisible;
  64. childSet.width(itemsWidth);
  65. childSet.last().insertBefore(childSet.first());
  66. childSet.last().insertBefore(childSet.first());
  67. object.css({'left' : -itemsWidth});
  68. object.fadeIn();
  69. $(window).trigger("resize"); // needed to position arrows correctly
  70. },
  71. /******************************
  72. Append HTML
  73. *******************************/
  74. appendHTML: function() {
  75. object.addClass("nbs-flexisel-ul");
  76. object.wrap("<div class='nbs-flexisel-container'><div class='nbs-flexisel-inner'></div></div>");
  77. object.find("li").addClass("nbs-flexisel-item");
  78. if(settings.setMaxWidthAndHeight) {
  79. var baseWidth = $(".nbs-flexisel-item > img").width();
  80. var baseHeight = $(".nbs-flexisel-item > img").height();
  81. $(".nbs-flexisel-item > img").css("max-width", baseWidth);
  82. $(".nbs-flexisel-item > img").css("max-height", baseHeight);
  83. }
  84. $("<div class='nbs-flexisel-nav-left'></div><div class='nbs-flexisel-nav-right'></div>").insertAfter(object);
  85. var cloneContent = object.children().clone();
  86. object.append(cloneContent);
  87. },
  88. /******************************
  89. Set Event Handlers
  90. *******************************/
  91. setEventHandlers: function() {
  92. var listParent = object.parent();
  93. var childSet = object.children();
  94. var leftArrow = listParent.find($(".nbs-flexisel-nav-left"));
  95. var rightArrow = listParent.find($(".nbs-flexisel-nav-right"));
  96. $(window).on("resize", function(event){
  97. methods.setResponsiveEvents();
  98. var innerWidth = $(listParent).width();
  99. var innerHeight = $(listParent).height();
  100. itemsWidth = (innerWidth)/itemsVisible;
  101. childSet.width(itemsWidth);
  102. object.css({'left' : -itemsWidth});
  103. var halfArrowHeight = (leftArrow.height())/2;
  104. var arrowMargin = (innerHeight/2) - halfArrowHeight;
  105. leftArrow.css("top", arrowMargin + "px");
  106. rightArrow.css("top", arrowMargin + "px");
  107. });
  108. $(leftArrow).on("click", function (event) {
  109. methods.scrollLeft();
  110. });
  111. $(rightArrow).on("click", function (event) {
  112. methods.scrollRight();
  113. });
  114. if(settings.pauseOnHover == true) {
  115. $(".nbs-flexisel-item").on({
  116. mouseenter: function () {
  117. canNavigate = false;
  118. },
  119. mouseleave: function () {
  120. canNavigate = true;
  121. }
  122. });
  123. }
  124. if(settings.autoPlay == true) {
  125. setInterval(function () {
  126. if(canNavigate == true)
  127. methods.scrollRight();
  128. }, settings.autoPlaySpeed);
  129. }
  130. },
  131. /******************************
  132. Set Responsive Events
  133. *******************************/
  134. setResponsiveEvents: function() {
  135. var contentWidth = $('html').width();
  136. if(settings.enableResponsiveBreakpoints == true) {
  137. if(contentWidth < settings.responsiveBreakpoints.portrait.changePoint) {
  138. itemsVisible = settings.responsiveBreakpoints.portrait.visibleItems;
  139. }
  140. else if(contentWidth > settings.responsiveBreakpoints.portrait.changePoint && contentWidth < settings.responsiveBreakpoints.landscape.changePoint) {
  141. itemsVisible = settings.responsiveBreakpoints.landscape.visibleItems;
  142. }
  143. else if(contentWidth > settings.responsiveBreakpoints.landscape.changePoint && contentWidth < settings.responsiveBreakpoints.tablet.changePoint) {
  144. itemsVisible = settings.responsiveBreakpoints.tablet.visibleItems;
  145. }
  146. else {
  147. itemsVisible = settings.visibleItems;
  148. }
  149. }
  150. },
  151. /******************************
  152. Scroll Left
  153. *******************************/
  154. scrollLeft:function() {
  155. if(canNavigate == true) {
  156. canNavigate = false;
  157. var listParent = object.parent();
  158. var innerWidth = listParent.width();
  159. itemsWidth = (innerWidth)/itemsVisible;
  160. var childSet = object.children();
  161. object.animate({
  162. 'left' : "+=" + itemsWidth
  163. },
  164. {
  165. queue:false,
  166. duration:settings.animationSpeed,
  167. easing: "linear",
  168. complete: function() {
  169. childSet.last().insertBefore(childSet.first()); // Get the first list item and put it after the last list item (that's how the infinite effects is made)
  170. methods.adjustScroll();
  171. canNavigate = true;
  172. }
  173. }
  174. );
  175. }
  176. },
  177. /******************************
  178. Scroll Right
  179. *******************************/
  180. scrollRight:function() {
  181. if(canNavigate == true) {
  182. canNavigate = false;
  183. var listParent = object.parent();
  184. var innerWidth = listParent.width();
  185. itemsWidth = (innerWidth)/itemsVisible;
  186. var childSet = object.children();
  187. object.animate({
  188. 'left' : "-=" + itemsWidth
  189. },
  190. {
  191. queue:false,
  192. duration:settings.animationSpeed,
  193. easing: "linear",
  194. complete: function() {
  195. childSet.first().insertAfter(childSet.last()); // Get the first list item and put it after the last list item (that's how the infinite effects is made)
  196. methods.adjustScroll();
  197. canNavigate = true;
  198. }
  199. }
  200. );
  201. }
  202. },
  203. /******************************
  204. Adjust Scroll
  205. *******************************/
  206. adjustScroll: function() {
  207. var listParent = object.parent();
  208. var childSet = object.children();
  209. var innerWidth = listParent.width();
  210. itemsWidth = (innerWidth)/itemsVisible;
  211. childSet.width(itemsWidth);
  212. object.css({'left' : -itemsWidth});
  213. }
  214. };
  215. if (methods[options]) { // $("#element").pluginName('methodName', 'arg1', 'arg2');
  216. return methods[options].apply(this, Array.prototype.slice.call(arguments, 1));
  217. } else if (typeof options === 'object' || !options) { // $("#element").pluginName({ option: 1, option:2 });
  218. return methods.init.apply(this);
  219. } else {
  220. $.error( 'Method "' + method + '" does not exist in flexisel plugin!');
  221. }
  222. };
  223. })(jQuery);