main.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. Dimension by HTML5 UP
  3. html5up.net | @ajlkn
  4. Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
  5. */
  6. (function($) {
  7. var $window = $(window),
  8. $body = $('body'),
  9. $wrapper = $('#wrapper'),
  10. $header = $('#header'),
  11. $footer = $('#footer'),
  12. $main = $('#main'),
  13. $main_articles = $main.children('article');
  14. // Breakpoints.
  15. breakpoints({
  16. xlarge: [ '1281px', '1680px' ],
  17. large: [ '981px', '1280px' ],
  18. medium: [ '737px', '980px' ],
  19. small: [ '481px', '736px' ],
  20. xsmall: [ '361px', '480px' ],
  21. xxsmall: [ null, '360px' ]
  22. });
  23. // Play initial animations on page load.
  24. $window.on('load', function() {
  25. window.setTimeout(function() {
  26. $body.removeClass('is-preload');
  27. }, 100);
  28. });
  29. // Fix: Flexbox min-height bug on IE.
  30. if (browser.name == 'ie') {
  31. var flexboxFixTimeoutId;
  32. $window.on('resize.flexbox-fix', function() {
  33. clearTimeout(flexboxFixTimeoutId);
  34. flexboxFixTimeoutId = setTimeout(function() {
  35. if ($wrapper.prop('scrollHeight') > $window.height())
  36. $wrapper.css('height', 'auto');
  37. else
  38. $wrapper.css('height', '100vh');
  39. }, 250);
  40. }).triggerHandler('resize.flexbox-fix');
  41. }
  42. // Nav.
  43. var $nav = $header.children('nav'),
  44. $nav_li = $nav.find('li');
  45. // Add "middle" alignment classes if we're dealing with an even number of items.
  46. if ($nav_li.length % 2 == 0) {
  47. $nav.addClass('use-middle');
  48. $nav_li.eq( ($nav_li.length / 2) ).addClass('is-middle');
  49. }
  50. // Main.
  51. var delay = 325,
  52. locked = false;
  53. // Methods.
  54. $main._show = function(id, initial) {
  55. var $article = $main_articles.filter('#' + id);
  56. // No such article? Bail.
  57. if ($article.length == 0)
  58. return;
  59. // Handle lock.
  60. // Already locked? Speed through "show" steps w/o delays.
  61. if (locked || (typeof initial != 'undefined' && initial === true)) {
  62. // Mark as switching.
  63. $body.addClass('is-switching');
  64. // Mark as visible.
  65. $body.addClass('is-article-visible');
  66. // Deactivate all articles (just in case one's already active).
  67. $main_articles.removeClass('active');
  68. // Hide header, footer.
  69. $header.hide();
  70. $footer.hide();
  71. // Show main, article.
  72. $main.show();
  73. $article.show();
  74. // Activate article.
  75. $article.addClass('active');
  76. // Unlock.
  77. locked = false;
  78. // Unmark as switching.
  79. setTimeout(function() {
  80. $body.removeClass('is-switching');
  81. }, (initial ? 1000 : 0));
  82. return;
  83. }
  84. // Lock.
  85. locked = true;
  86. // Article already visible? Just swap articles.
  87. if ($body.hasClass('is-article-visible')) {
  88. // Deactivate current article.
  89. var $currentArticle = $main_articles.filter('.active');
  90. $currentArticle.removeClass('active');
  91. // Show article.
  92. setTimeout(function() {
  93. // Hide current article.
  94. $currentArticle.hide();
  95. // Show article.
  96. $article.show();
  97. // Activate article.
  98. setTimeout(function() {
  99. $article.addClass('active');
  100. // Window stuff.
  101. $window
  102. .scrollTop(0)
  103. .triggerHandler('resize.flexbox-fix');
  104. // Unlock.
  105. setTimeout(function() {
  106. locked = false;
  107. }, delay);
  108. }, 25);
  109. }, delay);
  110. }
  111. // Otherwise, handle as normal.
  112. else {
  113. // Mark as visible.
  114. $body
  115. .addClass('is-article-visible');
  116. // Show article.
  117. setTimeout(function() {
  118. // Hide header, footer.
  119. $header.hide();
  120. $footer.hide();
  121. // Show main, article.
  122. $main.show();
  123. $article.show();
  124. // Activate article.
  125. setTimeout(function() {
  126. $article.addClass('active');
  127. // Window stuff.
  128. $window
  129. .scrollTop(0)
  130. .triggerHandler('resize.flexbox-fix');
  131. // Unlock.
  132. setTimeout(function() {
  133. locked = false;
  134. }, delay);
  135. }, 25);
  136. }, delay);
  137. }
  138. };
  139. $main._hide = function(addState) {
  140. var $article = $main_articles.filter('.active');
  141. // Article not visible? Bail.
  142. if (!$body.hasClass('is-article-visible'))
  143. return;
  144. // Add state?
  145. if (typeof addState != 'undefined'
  146. && addState === true)
  147. history.pushState(null, null, '#');
  148. // Handle lock.
  149. // Already locked? Speed through "hide" steps w/o delays.
  150. if (locked) {
  151. // Mark as switching.
  152. $body.addClass('is-switching');
  153. // Deactivate article.
  154. $article.removeClass('active');
  155. // Hide article, main.
  156. $article.hide();
  157. $main.hide();
  158. // Show footer, header.
  159. $footer.show();
  160. $header.show();
  161. // Unmark as visible.
  162. $body.removeClass('is-article-visible');
  163. // Unlock.
  164. locked = false;
  165. // Unmark as switching.
  166. $body.removeClass('is-switching');
  167. // Window stuff.
  168. $window
  169. .scrollTop(0)
  170. .triggerHandler('resize.flexbox-fix');
  171. return;
  172. }
  173. // Lock.
  174. locked = true;
  175. // Deactivate article.
  176. $article.removeClass('active');
  177. // Hide article.
  178. setTimeout(function() {
  179. // Hide article, main.
  180. $article.hide();
  181. $main.hide();
  182. // Show footer, header.
  183. $footer.show();
  184. $header.show();
  185. // Unmark as visible.
  186. setTimeout(function() {
  187. $body.removeClass('is-article-visible');
  188. // Window stuff.
  189. $window
  190. .scrollTop(0)
  191. .triggerHandler('resize.flexbox-fix');
  192. // Unlock.
  193. setTimeout(function() {
  194. locked = false;
  195. }, delay);
  196. }, 25);
  197. }, delay);
  198. };
  199. // Articles.
  200. $main_articles.each(function() {
  201. var $this = $(this);
  202. // Close.
  203. $('<div class="close">Close</div>')
  204. .appendTo($this)
  205. .on('click', function() {
  206. location.hash = '';
  207. });
  208. // Prevent clicks from inside article from bubbling.
  209. $this.on('click', function(event) {
  210. event.stopPropagation();
  211. });
  212. });
  213. // Events.
  214. $body.on('click', function(event) {
  215. // Article visible? Hide.
  216. if ($body.hasClass('is-article-visible'))
  217. $main._hide(true);
  218. });
  219. $window.on('keyup', function(event) {
  220. switch (event.keyCode) {
  221. case 27:
  222. // Article visible? Hide.
  223. if ($body.hasClass('is-article-visible'))
  224. $main._hide(true);
  225. break;
  226. default:
  227. break;
  228. }
  229. });
  230. $window.on('hashchange', function(event) {
  231. // Empty hash?
  232. if (location.hash == ''
  233. || location.hash == '#') {
  234. // Prevent default.
  235. event.preventDefault();
  236. event.stopPropagation();
  237. // Hide.
  238. $main._hide();
  239. }
  240. // Otherwise, check for a matching article.
  241. else if ($main_articles.filter(location.hash).length > 0) {
  242. // Prevent default.
  243. event.preventDefault();
  244. event.stopPropagation();
  245. // Show article.
  246. $main._show(location.hash.substr(1));
  247. }
  248. });
  249. // Scroll restoration.
  250. // This prevents the page from scrolling back to the top on a hashchange.
  251. if ('scrollRestoration' in history)
  252. history.scrollRestoration = 'manual';
  253. else {
  254. var oldScrollPos = 0,
  255. scrollPos = 0,
  256. $htmlbody = $('html,body');
  257. $window
  258. .on('scroll', function() {
  259. oldScrollPos = scrollPos;
  260. scrollPos = $htmlbody.scrollTop();
  261. })
  262. .on('hashchange', function() {
  263. $window.scrollTop(oldScrollPos);
  264. });
  265. }
  266. // Initialize.
  267. // Hide main, articles.
  268. $main.hide();
  269. $main_articles.hide();
  270. // Initial article.
  271. if (location.hash != ''
  272. && location.hash != '#')
  273. $window.on('load', function() {
  274. $main._show(location.hash.substr(1), true);
  275. });
  276. })(jQuery);