'use strict' const Even = {} Even.backToTop = function () { const $backToTop = $('#back-to-top') $(window).scroll(function () { if ($(window).scrollTop() > 100) { $backToTop.fadeIn(1000) } else { $backToTop.fadeOut(1000) } }) $backToTop.click(function () { $('body,html').animate({ scrollTop: 0 }) }) } Even.mobileNavbar = function () { const $mobileNav = $('#mobile-navbar') const $mobileNavIcon = $('.mobile-navbar-icon') const slideout = new Slideout({ 'panel': document.getElementById('mobile-panel'), 'menu': document.getElementById('mobile-menu'), 'padding': 180, 'tolerance': 70 }) slideout.disableTouch() $mobileNavIcon.click(function () { slideout.toggle() }) slideout.on('beforeopen', function () { $mobileNav.addClass('fixed-open') $mobileNavIcon.addClass('icon-click').removeClass('icon-out') }) slideout.on('beforeclose', function () { $mobileNav.removeClass('fixed-open') $mobileNavIcon.addClass('icon-out').removeClass('icon-click') }) $('#mobile-panel').on('touchend', function () { slideout.isOpen() && $mobileNavIcon.click() }) } Even._initToc = function () { const SPACING = 20 const $toc = $('.post-toc') const $footer = $('.post-footer') if ($toc.length) { const minScrollTop = $toc.offset().top - SPACING const maxScrollTop = $footer.offset().top - $toc.height() - SPACING const tocState = { start: { 'position': 'absolute', 'top': minScrollTop }, process: { 'position': 'fixed', 'top': SPACING }, end: { 'position': 'absolute', 'top': maxScrollTop } } $(window).scroll(function () { const scrollTop = $(window).scrollTop() if (scrollTop < minScrollTop) { $toc.css(tocState.start) } else if (scrollTop > maxScrollTop) { $toc.css(tocState.end) } else { $toc.css(tocState.process) } }) } const HEADERFIX = 30 const $toclink = $('.toc-link') const $headerlink = $('.headerlink') const $tocLinkLis = $('.post-toc-content li') const headerlinkTop = $.map($headerlink, function (link) { return $(link).offset().top }) const headerLinksOffsetForSearch = $.map(headerlinkTop, function (offset) { return offset - HEADERFIX }) const searchActiveTocIndex = function (array, target) { for (let i = 0; i < array.length - 1; i++) { if (target > array[i] && target <= array[i + 1]) return i } if (target > array[array.length - 1]) return array.length - 1 return -1 } $(window).scroll(function () { const scrollTop = $(window).scrollTop() const activeTocIndex = searchActiveTocIndex(headerLinksOffsetForSearch, scrollTop) $($toclink).removeClass('active') $($tocLinkLis).removeClass('has-active') if (activeTocIndex !== -1) { $($toclink[activeTocIndex]).addClass('active') let ancestor = $toclink[activeTocIndex].parentNode while (ancestor.tagName !== 'NAV') { $(ancestor).addClass('has-active') ancestor = ancestor.parentNode.parentNode } } }) } Even.fancybox = function () { if ($.fancybox) { $('.post-content').each(function () { $(this).find('img').each(function () { $(this).wrap(``) }) }) $('.fancybox').fancybox({ selector: '.fancybox', protect: true }) } } Even.highlight = function () { const blocks = document.querySelectorAll('pre code') for (let i = 0; i < blocks.length; i++) { const block = blocks[i] const rootElement = block.parentElement const lineCodes = block.innerHTML.split(/\n/).slice(0, -1) const lineLength = lineCodes.length let codeLineHtml = '' for (let i = 0; i < lineLength; i++) { codeLineHtml += `
${codeLineHtml} | ${codeHtml} |