PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/env/js/vendor/responsiveslides.js

https://gitlab.com/vince.omega/mcb-nov-build
JavaScript | 391 lines | 266 code | 59 blank | 66 comment | 43 complexity | bcaca8905ccc8550a319351b9ba8a7d6 MD5 | raw file
  1. /*! ResponsiveSlides.js v1.54
  2. * http://responsiveslides.com
  3. * http://viljamis.com
  4. *
  5. * Copyright (c) 2011-2012 @viljamis
  6. * Available under the MIT license
  7. */
  8. /*jslint browser: true, sloppy: true, vars: true, plusplus: true, indent: 2 */
  9. (function ($, window, i) {
  10. $.fn.responsiveSlides = function (options) {
  11. // Default settings
  12. var settings = $.extend({
  13. "auto": true, // Boolean: Animate automatically, true or false
  14. "speed": 500, // Integer: Speed of the transition, in milliseconds
  15. "timeout": 4000, // Integer: Time between slide transitions, in milliseconds
  16. "pager": false, // Boolean: Show pager, true or false
  17. "nav": false, // Boolean: Show navigation, true or false
  18. "random": false, // Boolean: Randomize the order of the slides, true or false
  19. "pause": false, // Boolean: Pause on hover, true or false
  20. "pauseControls": true, // Boolean: Pause when hovering controls, true or false
  21. "prevText": "Previous", // String: Text for the "previous" button
  22. "nextText": "Next", // String: Text for the "next" button
  23. "maxwidth": "", // Integer: Max-width of the slideshow, in pixels
  24. "navContainer": "", // Selector: Where auto generated controls should be appended to, default is after the <ul>
  25. "manualControls": "", // Selector: Declare custom pager navigation
  26. "namespace": "rslides", // String: change the default namespace used
  27. "before": $.noop, // Function: Before callback
  28. "after": $.noop // Function: After callback
  29. }, options);
  30. return this.each(function () {
  31. // Index for namespacing
  32. i++;
  33. var $this = $(this),
  34. // Local variables
  35. vendor,
  36. selectTab,
  37. startCycle,
  38. restartCycle,
  39. rotate,
  40. $tabs,
  41. // Helpers
  42. index = 0,
  43. $slide = $this.children(),
  44. length = $slide.size(),
  45. fadeTime = parseFloat(settings.speed),
  46. waitTime = parseFloat(settings.timeout),
  47. maxw = parseFloat(settings.maxwidth),
  48. // Namespacing
  49. namespace = settings.namespace,
  50. namespaceIdx = namespace + i,
  51. // Classes
  52. navClass = namespace + "_nav " + namespaceIdx + "_nav",
  53. activeClass = namespace + "_here",
  54. visibleClass = namespaceIdx + "_on",
  55. slideClassPrefix = namespaceIdx + "_s",
  56. // Pager
  57. $pager = $("<ul class='" + namespace + "_tabs " + namespaceIdx + "_tabs' />"),
  58. // Styles for visible and hidden slides
  59. visible = {"float": "left", "position": "relative", "opacity": 1, "zIndex": 2},
  60. hidden = {"float": "none", "position": "absolute", "opacity": 0, "zIndex": 1},
  61. // Detect transition support
  62. supportsTransitions = (function () {
  63. var docBody = document.body || document.documentElement;
  64. var styles = docBody.style;
  65. var prop = "transition";
  66. if (typeof styles[prop] === "string") {
  67. return true;
  68. }
  69. // Tests for vendor specific prop
  70. vendor = ["Moz", "Webkit", "Khtml", "O", "ms"];
  71. prop = prop.charAt(0).toUpperCase() + prop.substr(1);
  72. var i;
  73. for (i = 0; i < vendor.length; i++) {
  74. if (typeof styles[vendor[i] + prop] === "string") {
  75. return true;
  76. }
  77. }
  78. return false;
  79. })(),
  80. // Fading animation
  81. slideTo = function (idx) {
  82. settings.before(idx);
  83. // If CSS3 transitions are supported
  84. if (supportsTransitions) {
  85. $slide
  86. .removeClass(visibleClass)
  87. .css(hidden)
  88. .eq(idx)
  89. .addClass(visibleClass)
  90. .css(visible);
  91. index = idx;
  92. setTimeout(function () {
  93. settings.after(idx);
  94. }, fadeTime);
  95. // If not, use jQuery fallback
  96. } else {
  97. $slide
  98. .stop()
  99. .fadeOut(fadeTime, function () {
  100. $(this)
  101. .removeClass(visibleClass)
  102. .css(hidden)
  103. .css("opacity", 1);
  104. })
  105. .eq(idx)
  106. .fadeIn(fadeTime, function () {
  107. $(this)
  108. .addClass(visibleClass)
  109. .css(visible);
  110. settings.after(idx);
  111. index = idx;
  112. });
  113. }
  114. };
  115. // Random order
  116. if (settings.random) {
  117. $slide.sort(function () {
  118. return (Math.round(Math.random()) - 0.5);
  119. });
  120. $this
  121. .empty()
  122. .append($slide);
  123. }
  124. // Add ID's to each slide
  125. $slide.each(function (i) {
  126. this.id = slideClassPrefix + i;
  127. });
  128. // Add max-width and classes
  129. $this.addClass(namespace + " " + namespaceIdx);
  130. if (options && options.maxwidth) {
  131. $this.css("max-width", maxw);
  132. }
  133. // Hide all slides, then show first one
  134. $slide
  135. .hide()
  136. .css(hidden)
  137. .eq(0)
  138. .addClass(visibleClass)
  139. .css(visible)
  140. .show();
  141. // CSS transitions
  142. if (supportsTransitions) {
  143. $slide
  144. .show()
  145. .css({
  146. // -ms prefix isn't needed as IE10 uses prefix free version
  147. "-webkit-transition": "opacity " + fadeTime + "ms ease-in-out",
  148. "-moz-transition": "opacity " + fadeTime + "ms ease-in-out",
  149. "-o-transition": "opacity " + fadeTime + "ms ease-in-out",
  150. "transition": "opacity " + fadeTime + "ms ease-in-out"
  151. });
  152. }
  153. // Only run if there's more than one slide
  154. if ($slide.size() > 1) {
  155. // Make sure the timeout is at least 100ms longer than the fade
  156. if (waitTime < fadeTime + 100) {
  157. return;
  158. }
  159. // Pager
  160. if (settings.pager && !settings.manualControls) {
  161. var tabMarkup = [];
  162. $slide.each(function (i) {
  163. var n = i + 1;
  164. tabMarkup +=
  165. "<li>" +
  166. "<a href='#' class='" + slideClassPrefix + n + "'>" + n + "</a>" +
  167. "</li>";
  168. });
  169. $pager.append(tabMarkup);
  170. // Inject pager
  171. if (options.navContainer) {
  172. $(settings.navContainer).append($pager);
  173. } else {
  174. $this.after($pager);
  175. }
  176. }
  177. // Manual pager controls
  178. if (settings.manualControls) {
  179. $pager = $(settings.manualControls);
  180. $pager.addClass(namespace + "_tabs " + namespaceIdx + "_tabs");
  181. }
  182. // Add pager slide class prefixes
  183. if (settings.pager || settings.manualControls) {
  184. $pager.find('li').each(function (i) {
  185. $(this).addClass(slideClassPrefix + (i + 1));
  186. });
  187. }
  188. // If we have a pager, we need to set up the selectTab function
  189. if (settings.pager || settings.manualControls) {
  190. $tabs = $pager.find('a');
  191. // Select pager item
  192. selectTab = function (idx) {
  193. $tabs
  194. .closest("li")
  195. .removeClass(activeClass)
  196. .eq(idx)
  197. .addClass(activeClass);
  198. };
  199. }
  200. // Auto cycle
  201. if (settings.auto) {
  202. startCycle = function () {
  203. rotate = setInterval(function () {
  204. // Clear the event queue
  205. $slide.stop(true, true);
  206. var idx = index + 1 < length ? index + 1 : 0;
  207. // Remove active state and set new if pager is set
  208. if (settings.pager || settings.manualControls) {
  209. selectTab(idx);
  210. }
  211. slideTo(idx);
  212. }, waitTime);
  213. };
  214. // Init cycle
  215. startCycle();
  216. }
  217. // Restarting cycle
  218. restartCycle = function () {
  219. if (settings.auto) {
  220. // Stop
  221. clearInterval(rotate);
  222. // Restart
  223. startCycle();
  224. }
  225. };
  226. // Pause on hover
  227. if (settings.pause) {
  228. $this.hover(function () {
  229. clearInterval(rotate);
  230. }, function () {
  231. restartCycle();
  232. });
  233. }
  234. // Pager click event handler
  235. if (settings.pager || settings.manualControls) {
  236. $tabs.bind("click", function (e) {
  237. e.preventDefault();
  238. if (!settings.pauseControls) {
  239. restartCycle();
  240. }
  241. // Get index of clicked tab
  242. var idx = $tabs.index(this);
  243. // Break if element is already active or currently animated
  244. if (index === idx || $("." + visibleClass).queue('fx').length) {
  245. return;
  246. }
  247. // Remove active state from old tab and set new one
  248. selectTab(idx);
  249. // Do the animation
  250. slideTo(idx);
  251. })
  252. .eq(0)
  253. .closest("li")
  254. .addClass(activeClass);
  255. // Pause when hovering pager
  256. if (settings.pauseControls) {
  257. $tabs.hover(function () {
  258. clearInterval(rotate);
  259. }, function () {
  260. restartCycle();
  261. });
  262. }
  263. }
  264. // Navigation
  265. if (settings.nav) {
  266. var navMarkup =
  267. "<a href='#' class='" + navClass + " prev'>" + settings.prevText + "</a>" +
  268. "<a href='#' class='" + navClass + " next'>" + settings.nextText + "</a>";
  269. // Inject navigation
  270. if (options.navContainer) {
  271. $(settings.navContainer).append(navMarkup);
  272. } else {
  273. $this.after(navMarkup);
  274. }
  275. var $trigger = $("." + namespaceIdx + "_nav"),
  276. $prev = $trigger.filter(".prev");
  277. // Click event handler
  278. $trigger.bind("click", function (e) {
  279. e.preventDefault();
  280. var $visibleClass = $("." + visibleClass);
  281. // Prevent clicking if currently animated
  282. if ($visibleClass.queue('fx').length) {
  283. return;
  284. }
  285. // Adds active class during slide animation
  286. // $(this)
  287. // .addClass(namespace + "_active")
  288. // .delay(fadeTime)
  289. // .queue(function (next) {
  290. // $(this).removeClass(namespace + "_active");
  291. // next();
  292. // });
  293. // Determine where to slide
  294. var idx = $slide.index($visibleClass),
  295. prevIdx = idx - 1,
  296. nextIdx = idx + 1 < length ? index + 1 : 0;
  297. // Go to slide
  298. slideTo($(this)[0] === $prev[0] ? prevIdx : nextIdx);
  299. if (settings.pager || settings.manualControls) {
  300. selectTab($(this)[0] === $prev[0] ? prevIdx : nextIdx);
  301. }
  302. if (!settings.pauseControls) {
  303. restartCycle();
  304. }
  305. });
  306. // Pause when hovering navigation
  307. if (settings.pauseControls) {
  308. $trigger.hover(function () {
  309. clearInterval(rotate);
  310. }, function () {
  311. restartCycle();
  312. });
  313. }
  314. }
  315. }
  316. // Max-width fallback
  317. if (typeof document.body.style.maxWidth === "undefined" && options.maxwidth) {
  318. var widthSupport = function () {
  319. $this.css("width", "100%");
  320. if ($this.width() > maxw) {
  321. $this.css("width", maxw);
  322. }
  323. };
  324. // Init fallback
  325. widthSupport();
  326. $(window).bind("resize", function () {
  327. widthSupport();
  328. });
  329. }
  330. });
  331. };
  332. })(jQuery, this, 0);