main.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Dean Attali / Beautiful Jekyll 2016
  2. var main = {
  3. bigImgEl : null,
  4. numImgs : null,
  5. init : function() {
  6. // Shorten the navbar after scrolling a little bit down
  7. $(window).scroll(function() {
  8. if ($(".navbar").offset().top > 50) {
  9. $(".navbar").addClass("top-nav-short");
  10. $(".navbar-custom .avatar-container").fadeOut(500);
  11. } else {
  12. $(".navbar").removeClass("top-nav-short");
  13. $(".navbar-custom .avatar-container").fadeIn(500);
  14. }
  15. });
  16. // On mobile, hide the avatar when expanding the navbar menu
  17. $('#main-navbar').on('show.bs.collapse', function () {
  18. $(".navbar").addClass("top-nav-expanded");
  19. });
  20. $('#main-navbar').on('hidden.bs.collapse', function () {
  21. $(".navbar").removeClass("top-nav-expanded");
  22. });
  23. // On mobile, when clicking on a multi-level navbar menu, show the child links
  24. $('#main-navbar').on("click", ".navlinks-parent", function(e) {
  25. var target = e.target;
  26. $.each($(".navlinks-parent"), function(key, value) {
  27. if (value == target) {
  28. $(value).parent().toggleClass("show-children");
  29. } else {
  30. $(value).parent().removeClass("show-children");
  31. }
  32. });
  33. });
  34. // Ensure nested navbar menus are not longer than the menu header
  35. var menus = $(".navlinks-container");
  36. if (menus.length > 0) {
  37. var navbar = $("#main-navbar ul");
  38. var fakeMenuHtml = "<li class='fake-menu' style='display:none;'><a></a></li>";
  39. navbar.append(fakeMenuHtml);
  40. var fakeMenu = $(".fake-menu");
  41. $.each(menus, function(i) {
  42. var parent = $(menus[i]).find(".navlinks-parent");
  43. var children = $(menus[i]).find(".navlinks-children a");
  44. var words = [];
  45. $.each(children, function(idx, el) { words = words.concat($(el).text().trim().split(/\s+/)); });
  46. var maxwidth = 0;
  47. $.each(words, function(id, word) {
  48. fakeMenu.html("<a>" + word + "</a>");
  49. var width = fakeMenu.width();
  50. if (width > maxwidth) {
  51. maxwidth = width;
  52. }
  53. });
  54. $(menus[i]).css('min-width', maxwidth + 'px')
  55. });
  56. fakeMenu.remove();
  57. }
  58. // show the big header image
  59. main.initImgs();
  60. },
  61. initImgs : function() {
  62. // If the page was large images to randomly select from, choose an image
  63. if ($("#header-big-imgs").length > 0) {
  64. main.bigImgEl = $("#header-big-imgs");
  65. main.numImgs = main.bigImgEl.attr("data-num-img");
  66. // 2fc73a3a967e97599c9763d05e564189
  67. // set an initial image
  68. var imgInfo = main.getImgInfo();
  69. var src = imgInfo.src;
  70. var desc = imgInfo.desc;
  71. main.setImg(src, desc);
  72. // For better UX, prefetch the next image so that it will already be loaded when we want to show it
  73. var getNextImg = function() {
  74. var imgInfo = main.getImgInfo();
  75. var src = imgInfo.src;
  76. var desc = imgInfo.desc;
  77. var prefetchImg = new Image();
  78. prefetchImg.src = src;
  79. // if I want to do something once the image is ready: `prefetchImg.onload = function(){}`
  80. setTimeout(function(){
  81. var img = $("<div></div>").addClass("big-img-transition").css("background-image", 'url(' + src + ')');
  82. $(".intro-header.big-img").prepend(img);
  83. setTimeout(function(){ img.css("opacity", "1"); }, 50);
  84. // after the animation of fading in the new image is done, prefetch the next one
  85. //img.one("transitioned webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){
  86. setTimeout(function() {
  87. main.setImg(src, desc);
  88. img.remove();
  89. getNextImg();
  90. }, 1000);
  91. //});
  92. }, 6000);
  93. };
  94. // If there are multiple images, cycle through them
  95. if (main.numImgs > 1) {
  96. getNextImg();
  97. }
  98. }
  99. },
  100. getImgInfo : function() {
  101. var randNum = Math.floor((Math.random() * main.numImgs) + 1);
  102. var src = main.bigImgEl.attr("data-img-src-" + randNum);
  103. var desc = main.bigImgEl.attr("data-img-desc-" + randNum);
  104. return {
  105. src : src,
  106. desc : desc
  107. }
  108. },
  109. setImg : function(src, desc) {
  110. $(".intro-header.big-img").css("background-image", 'url(' + src + ')');
  111. if (typeof desc !== typeof undefined && desc !== false) {
  112. $(".img-desc").text(desc).show();
  113. } else {
  114. $(".img-desc").hide();
  115. }
  116. }
  117. };
  118. // 2fc73a3a967e97599c9763d05e564189
  119. document.addEventListener('DOMContentLoaded', main.init);