${decodeURIComponent(_ARTICLE_NAME)}
作者:${_ARTICLE_AUTHOR} | 分类:${_ARTICLE_CATEGORY}
/* ------------------------------------ * Harmony Hues主题 * * @author 星语社长 * @link https://biibii.cn * @update 2024-7-6 18:00:04 * --------------------------------- */ $(document).ready(function () { // 使用防抖技术优化事件 const debounce = (func, wait) => { var timeout; return function () { var context = this, args = arguments; clearTimeout(timeout); timeout = setTimeout(function () { func.apply(context, args); }, wait); }; } // 节流函数,减少滚动事件触发频率 const throttle = (func, wait) => { let timeout = null; return function () { if (!timeout) { timeout = setTimeout(() => { func.apply(this, arguments); timeout = null; }, wait); } }; }; /*---------------------点击“回到顶部”按钮时平滑滚动到顶部st---------------------*/ $('#nav-backtop').click(function () { $('html, body').animate({ scrollTop: 0 }, 200); return false; }); /*---------------------点击“回到顶部”按钮时平滑滚动到顶部end---------------------*/ /*---------------------夜间模式切换事件st---------------------*/ const THEMES = { LIGHT: 'light', DARK: 'dark', SYSTEM: 'system' }; // 设置主题 function setTheme(theme = THEMES.LIGHT) { let effectiveTheme = theme; if (theme === THEMES.SYSTEM) { effectiveTheme = getSystemTheme(); // 获取系统主题 } $('html').attr('data-theme', effectiveTheme); setCookie('theme', theme, 7); // 过期时间为 7 天 setCookie('system_theme', effectiveTheme, 1); // 过期时间为 1 天 } // 获取系统主题 function getSystemTheme() { return window.matchMedia('(prefers-color-scheme: dark)').matches ? THEMES.DARK : THEMES.LIGHT; } // 设置 Cookie function setCookie(name, value, days) { const expires = new Date(); expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000); document.cookie = `${name}=${value}; expires=${expires.toUTCString()}; path=/`; } // 按钮点击事件 $('.theme-toggle').on('click', 'button', function () { const theme = $(this).attr('title'); switch (theme) { case '浅色模式': setTheme(THEMES.LIGHT); break; case '深色模式': setTheme(THEMES.DARK); break; case '跟随系统': setTheme(THEMES.SYSTEM); break; } setActiveButton($(this)); }); // 设置激活按钮 function setActiveButton(button) { $('.theme-toggle button').removeClass('active'); button.addClass('active'); } // 初始化时设置激活按钮 function initActiveButton() { const currentTheme = getCookie('theme') || THEMES.LIGHT; let activeButton; if (currentTheme === THEMES.LIGHT) { activeButton = $('.theme-toggle button[title="浅色模式"]'); } else if (currentTheme === THEMES.DARK) { activeButton = $('.theme-toggle button[title="深色模式"]'); } else if (currentTheme === THEMES.SYSTEM) { activeButton = $('.theme-toggle button[title="跟随系统"]'); } else { activeButton = $('.theme-toggle button[title="浅色模式"]'); } setActiveButton(activeButton); } // 获取 Cookie function getCookie(name) { const cookies = document.cookie.split(';'); for (let cookie of cookies) { const [cookieName, cookieValue] = cookie.trim().split('='); if (cookieName === name) { return cookieValue; } } return null; } // 初始化 initActiveButton(); // 监听系统主题变化 window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => { setTheme(THEMES.SYSTEM); }); /*---------------------夜间模式切换事件end---------------------*/ /*---------------------滚动事件st---------------------*/ // 滚动进度条 const updateScrollProgress = () => { const scrollTop = $(window).scrollTop(); const docHeight = $(document).height(); const winHeight = $(window).height(); const scrollPercent = Math.round((scrollTop / (docHeight - winHeight)) * 100); if (scrollPercent > 0) { $('#nav-backtop').toggleClass('top-to-active', scrollPercent >= 90); $('.back-to-top').show(); $('#nav-backtop>.top-to-text').text(scrollPercent >= 90 ? "回到顶部" : scrollPercent); } else { $('.back-to-top').hide(); } } // 滚动Nav背景 let $isNav = false; if ($('#navbar').hasClass('nav-ui-one')) { $isNav = true; } toggleNavOnScroll = (navSelector, classOne, classTwo, scrollThreshold = 100) => { if ($isNav) { return; } const $nav = $(navSelector); const currentScrollTop = $(this).scrollTop(); if (currentScrollTop > scrollThreshold) { // 向下滚动超过阈值,切换到 classOne if ($nav.hasClass(classTwo)) { $nav.removeClass(classTwo).addClass(classOne); } } else { // 回到顶部,切换到 classTwo if ($nav.hasClass(classOne)) { $nav.removeClass(classOne).addClass(classTwo); } } } // 页面加载时更新进度 updateScrollProgress(); toggleNavOnScroll('#navbar', 'nav-ui-one', 'nav-ui-two', 100); /*---------------------滚动事件end---------------------*/ /*---------------------滚动进事件集合st---------------------*/ $(window).scroll(function () { /*滚动进度条*/ throttle(updateScrollProgress(), 100); /*滚动Nav背景*/ throttle(toggleNavOnScroll('#navbar', 'nav-ui-one', 'nav-ui-two', 50), 100); }); /*---------------------滚动进事件集合end---------------------*/ /*---------------------手机端导航栏st---------------------*/ let startY = 0; let currentY = 0; let dragging = false; // 点击菜单按钮打开/关闭侧边栏 $('#menu-line').click(function (event) { event.stopPropagation(); const $navComponents = $('#nav-components'); // 如果导航组件已经存在,则关闭它 if ($navComponents.length) { closeSidebar(); return; } // 获取导航组件元素 const $mobileNav = $('#mobile-nav'); // 添加背景虚化 $mobileNav.append(`
`); // 克隆导航内容 const navContent = $('.nav-inner > ul').clone(); // 添加样式 navContent.find('.sub-menu').addClass('mt-1'); navContent.find('.sub-menu>ul').addClass('row p-1 ml-2'); navContent.find('.sub-menu>ul>li').addClass('col-4 p-1'); navContent.find('.nav-item').addClass('pb-3'); navContent.find('.nav-item>.nav-a').addClass('font-weight-bold'); navContent.find('.nav-item>.nav-a').prepend(''); // 创建导航组件 $mobileNav.append(``); // 延迟添加 .open 类,确保动画生效 requestAnimationFrame(() => { $('#mobile-nav-bg').css({ 'opacity': '1', 'visibility': 'visible', 'transform': 'rotate(0) scale(1)' }); $('#nav-components').addClass('open'); }); // body关闭滚动 $('body').css('overflow', 'hidden'); }); // 点击文档关闭侧边栏(如果点击区域不在侧边栏内) $(document).click(function (event) { if ($('#nav-components').hasClass('open') && !$(event.target).closest('#nav-components').length && !$(event.target).closest('#menu-line').length) { closeSidebar(); } }); // 阻止侧边栏内部点击事件冒泡 $('#nav-components').click(function (event) { event.stopPropagation(); }); // 触摸开始时记录初始位置 $(document).on('touchstart', function (event) { if ($('#nav-components').hasClass('open')) { startY = event.originalEvent.touches[0].pageY; currentY = startY; dragging = true; } }); // 触摸移动时更新侧边栏位置 $(document).on('touchmove', function (event) { if (dragging) { currentY = event.originalEvent.touches[0].pageY; const offset = currentY - startY; if (offset > 0) { $('#nav-components').css('transform', `translateY(${offset}px)`); } } }); // 触摸结束时判断是否关闭侧边栏 $(document).on('touchend', function () { if (dragging) { const offset = currentY - startY; const threshold = $('#nav-components').height() / 2; if (offset > threshold) { closeSidebar(); } else { $('#nav-components').css('transform', ''); } dragging = false; } }); // 点击关闭按钮关闭侧边栏 $(document).on('click', '#mobile-close-btn', function () { closeSidebar(); }); // 关闭侧边栏的函数 const closeSidebar = () => { const $navComponents = $('#nav-components'); const $mobileNavBg = $('#mobile-nav-bg'); // 移除 open 类并添加关闭动画 $navComponents.removeClass('open'); // 淡出背景 $mobileNavBg.css({ 'opacity': 0, 'visibility': 'hidden' }); // 动画结束后清理侧边栏 $navComponents.one('transitionend', function () { $navComponents.remove(); $mobileNavBg.remove(); $('body').css('overflow', ''); }); }; /*---------------------手机端导航栏end---------------------*/ /*---------------------搜索框按钮开始---------------------*/ // 点击搜索按钮时,显示搜索面板 $('#search-btn').click(function () { $('.main-search').addClass('open'); $('body').css('overflow', 'hidden'); }); // 点击搜索关闭按钮时,隐藏搜索面板 $('.search-box').on('click', '#search-close-btn', function () { $('.main-search').removeClass('open'); $('body').css('overflow', ''); }); /*---------------------搜索框按钮结束---------------------*/ /*---------------------通用面板函数开始---------------------*/ // 生成二维码 const generateQRCodeBase64 = (text, size = 200) => { // 创建临时容器 const container = document.createElement('div'); // 生成二维码 $(container).qrcode({ text: text, width: size, height: size }); // 获取canvas元素 const canvas = container.querySelector('canvas'); if (!canvas) { console.error("二维码生成失败:未创建canvas元素"); return null; } try { // 转换为Base64 return canvas.toDataURL('image/png'); } catch (error) { console.error("Base64转换失败:", error); return null; } }; // 创建不同类型的面板 const createPanel = (type, content, closeBtnId) => { return $('作者:${_ARTICLE_AUTHOR} | 分类:${_ARTICLE_CATEGORY}
' + decodeURIComponent(_ARTICLE_URL) + '
' + '', 'share-close-btn' ); }; // 点击分享按钮时,显示分享面板 $('#share-btn').click(function () { showPanel('.main-share', createShareBox); }); // 通用二维码切换函数 function toggleQRCode(buttonId, qrClass, titleText) { const otherClass = qrClass === 'wechat-qrcode' ? 'zdyqr-qrcode' : 'wechat-qrcode'; $(document).on('click', buttonId, function () { const $qrElement = $(`.${qrClass}`); const $otherElement = $(`.${otherClass}`); const $shareBox = $('.share-box'); if ($qrElement.length) { $qrElement.remove(); } else { // 移除另一种二维码 $otherElement.remove(); // 生成二维码HTML const articleUrl = decodeURIComponent(_ARTICLE_URL); const qrHTML = `