欢迎提示组件
654 字
3 分钟
欢迎提示组件
提示
本文是在博主 年华 文章:Firefly 主题 WelcomeToast 组件分析 基础上增加了自己实践过程的一些细节,转载无需和我联系,但请注明文章来源。如果侵权之处,请联系博主进行删除,谢谢~

创建组件
文件路径:src/components/widget/WelcomeToast.astro
<script> // const VISIT_SESSION_KEY = 'blog_visit_flag'; let hasShownToast = false;
function createWelcomeToast() { const toast = document.createElement('div'); toast.id = 'welcome-toast'; // Tailwind 样式控制:自动适配深色模式 toast.className = 'fixed bottom-4 right-4 z-50 transform translate-y-full opacity-0 transition-all duration-500 ease-out';
//使用 bg-white dark:bg-zinc-800 强制适配颜色 toast.innerHTML = ` <div class="bg-white dark:bg-zinc-800 border border-gray-200 dark:border-zinc-700 rounded-lg shadow-lg p-4 max-w-xs"> <div class="flex items-start gap-3"> <div class="flex-shrink-0 text-2xl">👋</div> <div class="flex-1 min-w-0"> <p id="welcome-message" class="text-sm font-semibold text-gray-900 dark:text-gray-100"> 正在加载... </p> <p class="text-xs text-gray-600 dark:text-gray-400 mt-2 italic"> 欢迎来到我的博客 </p> </div> <button onclick="closeWelcomeToast()" class="flex-shrink-0 text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 transition-colors" aria-label="关闭" > <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path> </svg> </button> </div> </div> `; document.body.appendChild(toast); return toast; }
function closeWelcomeToast() { const toast = document.getElementById('welcome-toast'); if (toast) { toast.classList.add('translate-y-full', 'opacity-0'); toast.classList.remove('translate-y-0', 'opacity-100'); setTimeout(() => { toast.remove(); }, 500); } }
async function fetchLocation() { try { const response = await fetch('https://v2.xxapi.cn/api/ip'); const data = await response.json(); if (data.code === 200 && data.data) { const locationText = `你好,来自${data.data.address}的朋友`; const messageEl = document.getElementById('welcome-message'); if (messageEl) messageEl.textContent = locationText; } } catch (error) { const messageEl = document.getElementById('welcome-message'); if (messageEl) messageEl.textContent = '你好,欢迎来到我的博客'; } }
async function showWelcomeToast() { if (hasShownToast) return; const toast = createWelcomeToast(); setTimeout(() => { toast.classList.remove('translate-y-full', 'opacity-0'); toast.classList.add('translate-y-0', 'opacity-100'); }, 100); hasShownToast = true; await fetchLocation(); setTimeout(() => { closeWelcomeToast(); }, 5000); }
function initWelcome() { // 检查是否已显示过欢迎弹窗 // if (!sessionStorage.getItem(VISIT_SESSION_KEY)) { // sessionStorage.setItem(VISIT_SESSION_KEY, 'true'); // showWelcomeToast(); // } // 显示欢迎弹窗 showWelcomeToast(); }
if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initWelcome); document.addEventListener('astro:page-load', initWelcome); } else { initWelcome(); }
(window as any).closeWelcomeToast = closeWelcomeToast;</script>
<style> /* 样式保持不变 */ #welcome-toast { max-width: calc(100vw - 2rem); } @media (max-width: 640px) { #welcome-toast { top: auto; bottom: 1rem; left: 50%; right: auto; transform: translateX(-50%) translateY(100%); width: 90%; max-width: none; transition: transform 0.5s ease-out, opacity 0.5s ease-out; } #welcome-toast.translate-y-0 { transform: translateX(-50%) translateY(0); } #welcome-toast.translate-y-full { transform: translateX(-50%) translateY(100%); } }</style>使用组件
文件路径:src/layouts/MainGridLayout.astro
---import WelcomeToast from "@components/widget/WelcomeToast.astro"; //欢迎提示组件---
<!-- 欢迎提示组件 --><WelcomeToast></WelcomeToast>显示逻辑配置
- 访客只在首次访问时显示欢迎弹窗(原作者)
function initWelcome() { // 检查是否已显示过欢迎弹窗 if (!sessionStorage.getItem(VISIT_SESSION_KEY)) { sessionStorage.setItem(VISIT_SESSION_KEY, 'true'); showWelcomeToast(); // 显示欢迎弹窗 } }- 访客只会在首页刷新时显示,非首页刷新不会显示(正在使用)
function initWelcome() { // 检查是否为首页(路径为 "/" 或 "/index.html") const isHomePage = window.location.pathname === '/' || window.location.pathname === '/index.html';
// 只在首页显示欢迎弹窗 if (isHomePage) { showWelcomeToast(); } }- 访客每次刷新页面时,欢迎提示组件会重新显示,不限于文章页。
function initWelcome() { // 显示欢迎弹窗 showWelcomeToast(); }支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!