main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Dean Attali / Beautiful Jekyll 2020
  2. var BeautifulJekyllJS = {
  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. } else {
  11. $(".navbar").removeClass("top-nav-short");
  12. }
  13. });
  14. // On mobile, hide the avatar when expanding the navbar menu
  15. $('#main-navbar').on('show.bs.collapse', function () {
  16. $(".navbar").addClass("top-nav-expanded");
  17. });
  18. $('#main-navbar').on('hidden.bs.collapse', function () {
  19. $(".navbar").removeClass("top-nav-expanded");
  20. });
  21. // show the big header image
  22. BeautifulJekyllJS.initImgs();
  23. },
  24. initImgs : function() {
  25. // If the page was large images to randomly select from, choose an image
  26. if ($("#header-big-imgs").length > 0) {
  27. BeautifulJekyllJS.bigImgEl = $("#header-big-imgs");
  28. BeautifulJekyllJS.numImgs = BeautifulJekyllJS.bigImgEl.attr("data-num-img");
  29. // 2fc73a3a967e97599c9763d05e564189
  30. // set an initial image
  31. var imgInfo = BeautifulJekyllJS.getImgInfo();
  32. var src = imgInfo.src;
  33. var desc = imgInfo.desc;
  34. BeautifulJekyllJS.setImg(src, desc);
  35. // For better UX, prefetch the next image so that it will already be loaded when we want to show it
  36. var getNextImg = function() {
  37. var imgInfo = BeautifulJekyllJS.getImgInfo();
  38. var src = imgInfo.src;
  39. var desc = imgInfo.desc;
  40. var prefetchImg = new Image();
  41. prefetchImg.src = src;
  42. // if I want to do something once the image is ready: `prefetchImg.onload = function(){}`
  43. setTimeout(function(){
  44. var img = $("<div></div>").addClass("big-img-transition").css("background-image", 'url(' + src + ')');
  45. $(".intro-header.big-img").prepend(img);
  46. setTimeout(function(){ img.css("opacity", "1"); }, 50);
  47. // after the animation of fading in the new image is done, prefetch the next one
  48. //img.one("transitioned webkitTransitionEnd oTransitionEnd MSTransitionEnd", function(){
  49. setTimeout(function() {
  50. BeautifulJekyllJS.setImg(src, desc);
  51. img.remove();
  52. getNextImg();
  53. }, 1000);
  54. //});
  55. }, 6000);
  56. };
  57. // If there are multiple images, cycle through them
  58. if (BeautifulJekyllJS.numImgs > 1) {
  59. getNextImg();
  60. }
  61. }
  62. },
  63. getImgInfo : function() {
  64. var randNum = Math.floor((Math.random() * BeautifulJekyllJS.numImgs) + 1);
  65. var src = BeautifulJekyllJS.bigImgEl.attr("data-img-src-" + randNum);
  66. var desc = BeautifulJekyllJS.bigImgEl.attr("data-img-desc-" + randNum);
  67. return {
  68. src : src,
  69. desc : desc
  70. }
  71. },
  72. setImg : function(src, desc) {
  73. $(".intro-header.big-img").css("background-image", 'url(' + src + ')');
  74. if (typeof desc !== typeof undefined && desc !== false) {
  75. $(".img-desc").text(desc).show();
  76. } else {
  77. $(".img-desc").hide();
  78. }
  79. }
  80. };
  81. // 2fc73a3a967e97599c9763d05e564189
  82. document.addEventListener('DOMContentLoaded', BeautifulJekyllJS.init);