123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- /* eslint-disable */
- var customSearch;
- (function ($) {
- "use strict";
- const scrollCorrection = 70; // (header height = 50px) + (gap = 20px)
- function scrolltoElement(elem, correction) {
- correction = correction || scrollCorrection;
- const $elem = elem.href ? $(elem.getAttribute('href')) : $(elem);
- $('html, body').animate({ 'scrollTop': $elem.offset().top - correction }, 400);
- };
- function setHeader() {
- if (!window.subData) return;
- const $wrapper = $('header .wrapper');
- const $comment = $('.s-comment', $wrapper);
- const $toc = $('.s-toc', $wrapper);
- const $top = $('.s-top',$wrapper);
- $wrapper.find('.nav-sub .logo').text(window.subData.title);
- let pos = document.body.scrollTop;
- $(document, window).scroll(() => {
- const scrollTop = $(window).scrollTop();
- const del = scrollTop - pos;
- if (del >= 20) {
- pos = scrollTop;
- $wrapper.addClass('sub');
- } else if (del <= -20) {
- pos = scrollTop;
- $wrapper.removeClass('sub');
- }
- });
- // bind events to every btn
- const $commentTarget = $('#comments');
- if ($commentTarget.length) {
- $comment.click(e => { e.preventDefault(); e.stopPropagation(); scrolltoElement($commentTarget); });
- } else $comment.remove();
- const $tocTarget = $('.toc-wrapper');
- if ($tocTarget.length && $tocTarget.children().length) {
- $toc.click((e) => { e.stopPropagation(); $tocTarget.toggleClass('active'); });
- } else $toc.remove();
- $top.click(()=>scrolltoElement(document.body));
- }
- function setHeaderMenu() {
- var $headerMenu = $('header .menu');
- var $underline = $headerMenu.find('.underline');
- function setUnderline($item, transition) {
- $item = $item || $headerMenu.find('li a.active');//get instant
- transition = transition === undefined ? true : !!transition;
- if (!transition) $underline.addClass('disable-trans');
- if ($item && $item.length) {
- $item.addClass('active').siblings().removeClass('active');
- $underline.css({
- left: $item.position().left,
- width: $item.innerWidth()
- });
- } else {
- $underline.css({
- left: 0,
- width: 0
- });
- }
- if (!transition) {
- setTimeout(function () { $underline.removeClass('disable-trans') }, 0);//get into the queue.
- }
- }
- $headerMenu.on('mouseenter', 'li', function (e) {
- setUnderline($(e.currentTarget));
- });
- $headerMenu.on('mouseout', function () {
- setUnderline();
- });
- //set current active nav
- var $active_link = null;
- if (location.pathname === '/' || location.pathname.startsWith('/page/')) {
- $active_link = $('.nav-home', $headerMenu);
- } else {
- var name = location.pathname.match(/\/(.*?)\//);
- if (name.length > 1) {
- $active_link = $('.nav-' + name[1], $headerMenu);
- }
- }
- setUnderline($active_link, false);
- }
- function setHeaderMenuPhone() {
- var $switcher = $('.l_header .switcher .s-menu');
- $switcher.click(function (e) {
- e.stopPropagation();
- $('body').toggleClass('z_menu-open');
- $switcher.toggleClass('active');
- });
- $(document).click(function (e) {
- $('body').removeClass('z_menu-open');
- $switcher.removeClass('active');
- });
- }
- function setHeaderSearch() {
- var $switcher = $('.l_header .switcher .s-search');
- var $header = $('.l_header');
- var $search = $('.l_header .m_search');
- if ($switcher.length === 0) return;
- $switcher.click(function (e) {
- e.stopPropagation();
- $header.toggleClass('z_search-open');
- $search.find('input').focus();
- });
- $(document).click(function (e) {
- $header.removeClass('z_search-open');
- });
- $search.click(function (e) {
- e.stopPropagation();
- })
- }
- function setWaves() {
- Waves.attach('.flat-btn', ['waves-button']);
- Waves.attach('.float-btn', ['waves-button', 'waves-float']);
- Waves.attach('.float-btn-light', ['waves-button', 'waves-float', 'waves-light']);
- Waves.attach('.flat-box', ['waves-block']);
- Waves.attach('.float-box', ['waves-block', 'waves-float']);
- Waves.attach('.waves-image');
- Waves.init();
- }
- function setScrollReveal() {
- const $reveal = $('.reveal');
- if ($reveal.length === 0) return;
- const sr = ScrollReveal({ distance: 0 });
- sr.reveal('.reveal');
- }
- function setTocToggle() {
- const $toc = $('.toc-wrapper');
- if ($toc.length === 0) return;
- $toc.click((e) => { e.stopPropagation(); $toc.addClass('active'); });
- $(document).click(() => $toc.removeClass('active'));
- $toc.on('click', 'a', (e) => {
- e.preventDefault();
- e.stopPropagation();
- scrolltoElement(e.target.tagName.toLowerCase === 'a' ? e.target : e.target.parentElement);
- });
- const liElements = Array.from($toc.find('li a'));
- //function animate above will convert float to int.
- const getAnchor = () => liElements.map(elem => Math.floor($(elem.getAttribute('href')).offset().top - scrollCorrection));
- let anchor = getAnchor();
- const scrollListener = () => {
- const scrollTop = $('html').scrollTop() || $('body').scrollTop();
- if (!anchor) return;
- //binary search.
- let l = 0, r = anchor.length - 1, mid;
- while (l < r) {
- mid = (l + r + 1) >> 1;
- if (anchor[mid] === scrollTop) l = r = mid;
- else if (anchor[mid] < scrollTop) l = mid;
- else r = mid - 1;
- }
- $(liElements).removeClass('active').eq(l).addClass('active');
- }
- $(window)
- .resize(() => {
- anchor = getAnchor();
- scrollListener();
- })
- .scroll(() => {
- scrollListener()
- });
- scrollListener();
- }
- // function getPicture() {
- // const $banner = $('.banner');
- // if ($banner.length === 0) return;
- // const url = ROOT + 'js/lovewallpaper.json';
- // $.get(url).done(res => {
- // if (res.data.length > 0) {
- // const index = Math.floor(Math.random() * res.data.length);
- // $banner.css('background-image', 'url(' + res.data[index].big + ')');
- // }
- // })
- // }
- // function getHitokoto() {
- // const $hitokoto = $('#hitokoto');
- // if($hitokoto.length === 0) return;
- // const url = 'http://api.hitokoto.us/rand?length=80&encode=jsc&fun=handlerHitokoto';
- // $('body').append('<script src="%s"></script>'.replace('%s',url));
- // window.handlerHitokoto = (data) => {
- // $hitokoto
- // .css('color','transparent')
- // .text(data.hitokoto)
- // if(data.source) $hitokoto.append('<cite> —— %s</cite>'.replace('%s',data.source));
- // else if(data.author) $hitokoto.append('<cite> —— %s</cite>'.replace('%s',data.author));
- // $hitokoto.css('color','white');
- // }
- // }
- $(function () {
- //set header
- setHeader();
- setHeaderMenu();
- setHeaderMenuPhone();
- setHeaderSearch();
- setWaves();
- setScrollReveal();
- setTocToggle();
- // getHitokoto();
- // getPicture();
- $(".article .video-container").fitVids();
- setTimeout(function () {
- $('#loading-bar-wrapper').fadeOut(500);
- }, 300);
- if (SEARCH_SERVICE === 'google') {
- customSearch = new GoogleCustomSearch({
- apiKey: GOOGLE_CUSTOM_SEARCH_API_KEY,
- engineId: GOOGLE_CUSTOM_SEARCH_ENGINE_ID,
- imagePath: "/images/"
- });
- }
- else if (SEARCH_SERVICE === 'algolia') {
- customSearch = new AlgoliaSearch({
- apiKey: ALGOLIA_API_KEY,
- appId: ALGOLIA_APP_ID,
- indexName: ALGOLIA_INDEX_NAME,
- imagePath: "/images/"
- });
- }
- else if (SEARCH_SERVICE === 'hexo') {
- customSearch = new HexoSearch({
- imagePath: "/images/"
- });
- }
- else if (SEARCH_SERVICE === 'azure') {
- customSearch = new AzureSearch({
- serviceName: AZURE_SERVICE_NAME,
- indexName: AZURE_INDEX_NAME,
- queryKey: AZURE_QUERY_KEY,
- imagePath: "/images/"
- });
- }
- else if (SEARCH_SERVICE === 'baidu') {
- customSearch = new BaiduSearch({
- apiId: BAIDU_API_ID,
- imagePath: "/images/"
- });
- }
- });
- })(jQuery);
|