beautifuljekyll.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Dean Attali / Beautiful Jekyll 2020
  2. var BeautifulJekyllJS = {
  3. bigImgEl : null,
  4. numImgs : null,
  5. init : function() {
  6. setTimeout(BeautifulJekyllJS.initNavbar, 10);
  7. // Shorten the navbar after scrolling a little bit down
  8. $(window).scroll(function() {
  9. if ($(".navbar").offset().top > 50) {
  10. $(".navbar").addClass("top-nav-short");
  11. } else {
  12. $(".navbar").removeClass("top-nav-short");
  13. }
  14. });
  15. // On mobile, hide the avatar when expanding the navbar menu
  16. $('#main-navbar').on('show.bs.collapse', function () {
  17. $(".navbar").addClass("top-nav-expanded");
  18. });
  19. $('#main-navbar').on('hidden.bs.collapse', function () {
  20. $(".navbar").removeClass("top-nav-expanded");
  21. });
  22. // show the big header image
  23. BeautifulJekyllJS.initImgs();
  24. BeautifulJekyllJS.initSearch();
  25. },
  26. initNavbar : function() {
  27. // Set the navbar-dark/light class based on its background color
  28. const rgb = $('.navbar').css("background-color").replace(/[^\d,]/g,'').split(",");
  29. const brightness = Math.round(( // http://www.w3.org/TR/AERT#color-contrast
  30. parseInt(rgb[0]) * 299 +
  31. parseInt(rgb[1]) * 587 +
  32. parseInt(rgb[2]) * 114
  33. ) / 1000);
  34. if (brightness <= 125) {
  35. $(".navbar").removeClass("navbar-light").addClass("navbar-dark");
  36. } else {
  37. $(".navbar").removeClass("navbar-dark").addClass("navbar-light");
  38. }
  39. },
  40. initImgs : function() {
  41. // If the page was large images to randomly select from, choose an image
  42. if ($("#header-big-imgs").length > 0) {
  43. BeautifulJekyllJS.bigImgEl = $("#header-big-imgs");
  44. BeautifulJekyllJS.numImgs = BeautifulJekyllJS.bigImgEl.attr("data-num-img");
  45. // 2fc73a3a967e97599c9763d05e564189
  46. // set an initial image
  47. var imgInfo = BeautifulJekyllJS.getImgInfo();
  48. var src = imgInfo.src;
  49. var desc = imgInfo.desc;
  50. BeautifulJekyllJS.setImg(src, desc);
  51. // For better UX, prefetch the next image so that it will already be loaded when we want to show it
  52. var getNextImg = function() {
  53. var imgInfo = BeautifulJekyllJS.getImgInfo();
  54. var src = imgInfo.src;
  55. var desc = imgInfo.desc;
  56. var prefetchImg = new Image();
  57. prefetchImg.src = src;
  58. // if I want to do something once the image is ready: `prefetchImg.onload = function(){}`
  59. setTimeout(function(){
  60. var img = $("<div></div>").addClass("big-img-transition").css("background-image", 'url(' + src + ')');
  61. $(".intro-header.big-img").prepend(img);
  62. setTimeout(function(){ img.css("opacity", "1"); }, 50);
  63. // after the animation of fading in the new image is done, prefetch the next one
  64. //img.one("transitioned webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){
  65. setTimeout(function() {
  66. BeautifulJekyllJS.setImg(src, desc);
  67. img.remove();
  68. getNextImg();
  69. }, 1000);
  70. //});
  71. }, 6000);
  72. };
  73. // If there are multiple images, cycle through them
  74. if (BeautifulJekyllJS.numImgs > 1) {
  75. getNextImg();
  76. }
  77. }
  78. },
  79. getImgInfo : function() {
  80. var randNum = Math.floor((Math.random() * BeautifulJekyllJS.numImgs) + 1);
  81. var src = BeautifulJekyllJS.bigImgEl.attr("data-img-src-" + randNum);
  82. var desc = BeautifulJekyllJS.bigImgEl.attr("data-img-desc-" + randNum);
  83. return {
  84. src : src,
  85. desc : desc
  86. }
  87. },
  88. setImg : function(src, desc) {
  89. $(".intro-header.big-img").css("background-image", 'url(' + src + ')');
  90. if (typeof desc !== typeof undefined && desc !== false) {
  91. $(".img-desc").text(desc).show();
  92. } else {
  93. $(".img-desc").hide();
  94. }
  95. },
  96. initSearch : function() {
  97. if (!document.getElementById("beautifuljekyll-search-overlay")) {
  98. return;
  99. }
  100. $("#nav-search-link").click(function(e) {
  101. e.preventDefault();
  102. $("#beautifuljekyll-search-overlay").show();
  103. $("#nav-search-input").focus().select();
  104. $("body").addClass("overflow-hidden");
  105. });
  106. $("#nav-search-exit").click(function(e) {
  107. e.preventDefault();
  108. $("#beautifuljekyll-search-overlay").hide();
  109. $("body").removeClass("overflow-hidden");
  110. });
  111. $(document).on('keyup', function(e) {
  112. if (e.key == "Escape") {
  113. $("#beautifuljekyll-search-overlay").hide();
  114. $("body").removeClass("overflow-hidden");
  115. }
  116. });
  117. }
  118. };
  119. // 2fc73a3a967e97599c9763d05e564189
  120. document.addEventListener('DOMContentLoaded', BeautifulJekyllJS.init);