hexo-theme-stellar/dist/js/main.js

1 line
4.2 KiB
JavaScript
Raw Normal View History

2021-02-21 21:31:43 +08:00
const util={diffDate:(e,t=!1)=>{const l=new Date,a=new Date(e),i=l.getTime()-a.getTime(),n=36e5,s=24*n;let r;if(t){const e=i/2592e6,t=i/s,l=i/n,a=i/6e4;r=e>12?null:e>=1?parseInt(e)+" "+stellar.config.date_suffix.month:t>=1?parseInt(t)+" "+stellar.config.date_suffix.day:l>=1?parseInt(l)+" "+stellar.config.date_suffix.hour:a>=1?parseInt(a)+" "+stellar.config.date_suffix.min:stellar.config.date_suffix.just}else r=parseInt(i/s);return r},loadCSS:(e,t,l,a)=>{var i,n=window.document,s=n.createElement("link");if(t)i=t;else{var r=(n.body||n.getElementsByTagName("head")[0]).childNodes;i=r[r.length-1]}var o=n.styleSheets;if(a)for(var d in a)a.hasOwnProperty(d)&&s.setAttribute(d,a[d]);s.rel="stylesheet",s.href=e,s.media="only x",function e(t){if(n.body)return t();setTimeout((function(){e(t)}))}((function(){i.parentNode.insertBefore(s,t?i:i.nextSibling)}));var c=function(e){for(var t=s.href,l=o.length;l--;)if(o[l].href===t)return e();setTimeout((function(){c(e)}))};function u(){s.addEventListener&&s.removeEventListener("load",u),s.media=l||"all"}return s.addEventListener&&s.addEventListener("load",u),s.onloadcssdefined=c,c(u),s},loadScript:(e,t)=>new Promise(((l,a)=>{var i=document.createElement("script");if(i.src=e,t)for(let e of Object.keys(t))i[e]=t[e];else i.async=!0;i.onerror=a,i.onload=i.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(i.onload=i.onreadystatechange=null,l())},document.head.appendChild(i)})),jQuery:e=>{"undefined"==typeof jQuery?util.loadScript(stellar.plugins.jQuery).then(e):e()}},l_body=document.querySelector(".l_body"),sidebar={toggle:()=>{l_body&&(l_body.classList.add("mobile"),l_body.classList.toggle("sidebar"))}},init={toc:()=>{util.jQuery((()=>{var e=[];$("article.md :header").each((function(t,l){e.push(l)})),$(document,window).scroll((function(t){var l=$(this).scrollTop(),a=null;for(var i in e){var n=$(e[i]);n.offset().top>l+32||(a?n.offset().top>=a.offset().top&&(a=n):a=n)}if(a){$("#toc a.toc-link").removeClass("active");var s="#"+a.attr("id");"#undefined"!=s?$('#toc a.toc-link[href="'+encodeURI(s)+'"]').addClass("active"):$("#toc a.toc-link:first").addClass("active")}}))}))},sidebar:()=>{util.jQuery((()=>{$("#toc a.toc-link").click((function(e){l_body.classList.remove("sidebar")})),$("#toc a#s-top").click((function(e){l_body.classList.remove("sidebar")}))}))},relativeDate:e=>{e.forEach((e=>{const t=e,l=t.getAttribute("datetime");let a=util.diffDate(l,!0);a&&(t.innerText=a)}))}};if(init.toc(),init.sidebar(),init.relativeDate(document.querySelectorAll("#post-meta time")),stellar.plugins.scrollreveal&&util.loadScript(stellar.plugins.scrollreveal.js).then((function(){ScrollReveal().reveal("body .reveal",{distance:stellar.plugins.scrollreveal.distance,duration:stellar.plugins.scrollreveal.duration,interval:stellar.plugins.scrollreveal.interval,scale:stellar.plugins.scrollreveal.scale,easing:"ease-out"})})),stellar.plugins.lazyload&&(util.loadScript(stellar.plugins.lazyload.js,{defer:!0}),window.lazyLoadOptions={elements_selector:".lazyload",threshold:0},window.addEventListener("LazyLoad::Initialized",(function(e){window.lazyLoadInstance=e.detail.instance}),!1),document.addEventListener("DOMContentLoaded",(function(){lazyLoadInstance.update()}))),stellar.plugins.issuesjs){null!=document.getElementById("issues-api")&&util.jQuery((()=>{util.loadScript(stellar.plugins.issuesjs,{defer:!0})}))}if(stellar.plugins.swiper){null!=document.getElementById("swiper-api")&&(util.loadCSS(stellar.plugins.swiper.css),util.loadScript(stellar.plugins.swiper.js,{defer:!0}).then((function(){new Swiper(".swiper-container",{slidesPerView:"auto",spaceBetween:8,centeredSlides:!0,zoom:!0,pagination:{el:".swiper-pagination",clickable:!0},autoplay:{delay:5e3,disableOnInteraction:!1},navigation:{nextEl:".swiper-button-next",prevEl:".swiper-button-prev"}})})))}stellar.plugins.preload&&("instant_page"==stellar.plugins.preload.service?util.loadScript(stellar.plugins.preload.instant_page,{defer:!0,type:"module",integrity:"sha384-OeDn4XE77tdHo8pGtE1apMPmAipjoxUQ++eeJa6EtJCfHlvijigWiJpD7VDPWXV1"}):"fly