Skip to content

回到陆地上。让你的博客从白天到晚上,无需群岛!

既然你可以为交互式元素构建 Astro 岛,请不要忘记仅使用普通 JavaScript 和 CSS 就可以走得很远!

¥Now that you can build Astro islands for interactive elements, don’t forget that you can go pretty far with just vanilla JavaScript and CSS!

让我们构建一个可点击的图标,让你的用户使用另一个 <script> 标签在浅色或夜间模式之间切换以实现交互…没有框架 JavaScript 发送到浏览器。

¥Let’s build a clickable icon to let your users toggle between light or dark mode using another <script> tag for interactivity… with no framework JavaScript sent to the browser.

Get ready to…

  • 仅使用 JavaScript 和 CSS 构建交互式主题切换

  • 向浏览器发送尽可能少的 JavaScript!

添加主题切换图标并设置其样式

Section titled 添加主题切换图标并设置其样式

¥Add and style a theme toggle icon

  1. Create a new file at src/components/ThemeIcon.astro and paste the following code into it:

    src/components/ThemeIcon.astro
    ---
    ---
    <button id="themeToggle">
    <svg width="30px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
    <path class="sun" fill-rule="evenodd" d="M12 17.5a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zm0 1.5a7 7 0 1 0 0-14 7 7 0 0 0 0 14zm12-7a.8.8 0 0 1-.8.8h-2.4a.8.8 0 0 1 0-1.6h2.4a.8.8 0 0 1 .8.8zM4 12a.8.8 0 0 1-.8.8H.8a.8.8 0 0 1 0-1.6h2.5a.8.8 0 0 1 .8.8zm16.5-8.5a.8.8 0 0 1 0 1l-1.8 1.8a.8.8 0 0 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM6.3 17.7a.8.8 0 0 1 0 1l-1.7 1.8a.8.8 0 1 1-1-1l1.7-1.8a.8.8 0 0 1 1 0zM12 0a.8.8 0 0 1 .8.8v2.5a.8.8 0 0 1-1.6 0V.8A.8.8 0 0 1 12 0zm0 20a.8.8 0 0 1 .8.8v2.4a.8.8 0 0 1-1.6 0v-2.4a.8.8 0 0 1 .8-.8zM3.5 3.5a.8.8 0 0 1 1 0l1.8 1.8a.8.8 0 1 1-1 1L3.5 4.6a.8.8 0 0 1 0-1zm14.2 14.2a.8.8 0 0 1 1 0l1.8 1.7a.8.8 0 0 1-1 1l-1.8-1.7a.8.8 0 0 1 0-1z"/>
    <path class="moon" fill-rule="evenodd" d="M16.5 6A10.5 10.5 0 0 1 4.7 16.4 8.5 8.5 0 1 0 16.4 4.7l.1 1.3zm-1.7-2a9 9 0 0 1 .2 2 9 9 0 0 1-11 8.8 9.4 9.4 0 0 1-.8-.3c-.4 0-.8.3-.7.7a10 10 0 0 0 .3.8 10 10 0 0 0 9.2 6 10 10 0 0 0 4-19.2 9.7 9.7 0 0 0-.9-.3c-.3-.1-.7.3-.6.7a9 9 0 0 1 .3.8z"/>
    </svg>
    </button>
    <style>
    #themeToggle {
    border: 0;
    background: none;
    }
    .sun { fill: black; }
    .moon { fill: transparent; }
    :global(.dark) .sun { fill: transparent; }
    :global(.dark) .moon { fill: white; }
    </style>
  2. Add the icon to Header.astro so that it will be displayed on all pages. Don’t forget to import the component.

    src/components/Header.astro
    ---
    import Hamburger from './Hamburger.astro';
    import Navigation from './Navigation.astro';
    import ThemeIcon from './ThemeIcon.astro';
    ---
    <header>
    <nav>
    <Hamburger />
    <ThemeIcon />
    <Navigation />
    </nav>
    </header>
  3. Visit your browser preview at http://localhost:4321 to see the icon now on all your pages. You can try clicking it, but you have not written a script to make it interactive yet.

为深色主题添加 CSS 样式

Section titled 为深色主题添加 CSS 样式

¥Add CSS styling for a dark theme

选择一些在夜间模式下使用的替代颜色。

¥Choose some alternate colors to use in dark mode.

  1. In global.css, define some dark styles. You can choose your own, or copy and paste:

    src/styles/global.css
    html.dark {
    background-color: #0d0950;
    color: #fff;
    }
    .dark .nav-links a {
    color: #fff;
    }

¥Add client-side interactivity

要向 Astro 组件添加交互性,你可以使用 <script> 标签。该脚本可以检查并设置 localStorage 中的当前主题,并在单击图标时切换主题。

¥To add interactivity to an Astro component, you can use a <script> tag. This script can check and set the current theme from localStorage and toggle the theme when the icon is clicked.

  1. Add the following <script> tag in src/components/ThemeIcon.astro after your <style> tag:

    src/components/ThemeIcon.astro
    <style>
    .sun { fill: black; }
    .moon { fill: transparent; }
    :global(.dark) .sun { fill: transparent; }
    :global(.dark) .moon { fill: white; }
    </style>
    <script is:inline>
    const theme = (() => {
    if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) {
    return localStorage.getItem('theme');
    }
    if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
    return 'dark';
    }
    return 'light';
    })();
    if (theme === 'light') {
    document.documentElement.classList.remove('dark');
    } else {
    document.documentElement.classList.add('dark');
    }
    window.localStorage.setItem('theme', theme);
    const handleToggleClick = () => {
    const element = document.documentElement;
    element.classList.toggle("dark");
    const isDark = element.classList.contains("dark");
    localStorage.setItem("theme", isDark ? "dark" : "light");
    }
    document.getElementById("themeToggle").addEventListener("click", handleToggleClick);
    </script>
  2. Check your browser preview at http://localhost:4321 and click the theme icon. Verify that you can change between light and dark modes.

¥Test your knowledge

选择以下每个语句是否描述 Astro <script> 标签、UI 框架组件或两者。

¥Choose whether each of the following statements describes Astro <script> tags, UI framework components, or both.

  1. 它们允许你在网站上包含交互式 UI 元素。
  1. 他们将在你的网站上创建静态元素,除非你包含 client: 将其 JavaScript 发送到客户端并在浏览器中运行。
  1. 它们允许你 “试用” 一个新框架,而不需要你使用该技术堆栈启动一个全新的项目。
  1. 它们允许你重用在其他框架中编写的代码,并且通常可以将它们直接放入你的站点中。
  1. 它们允许你添加交互性,而无需了解或学习任何其他 JavaScript 框架。

¥Checklist

¥Resources

Astro 中文网 - 粤ICP备13048890号