@@ -12,74 +12,65 @@ import Option from '~/components/tutorial/Option.astro';
1212import PreCheck from ' ~/components/tutorial/PreCheck.astro' ;
1313import { Steps } from ' @astrojs/starlight/components' ;
1414
15- 让我们在移动屏幕上添加一个汉堡菜单(Hamburger Menus)以打开和关闭你的导航链接 ,这需要一些客户端的交互!
15+ 让我们在移动屏幕上添加一个按钮以打开和关闭你的导航菜单 ,这需要一些客户端的交互!
1616
1717<PreCheck >
18- - 创建一个汉堡菜单组件
18+ - 创建一个菜单组件
1919 - 编写一个 ` <script> ` 使你的网站访问者可以打开和关闭导航菜单
2020 - 将你的 JavaScript 移动到 ` .js ` 文件中
2121</PreCheck >
2222
23- ## 搭建一个汉堡菜单组件
23+ ## 搭建一个菜单组件
2424
25- 创建一个 ` <Hamburger /> ` 组件,用于打开和关闭移动端菜单。
25+ 创建一个 ` <Menu /> ` 组件,用于打开和关闭移动端菜单。
2626
2727<Steps >
28- 1 . 在 ` src/components/ ` 中创建一个名为 ` Hamburger .astro` 的文件。
28+ 1 . 在 ` src/components/ ` 中创建一个名为 ` Menu .astro` 的文件。
2929
30- 2 . 将以下代码复制到你的组件中。这将表示你的三行「汉堡」菜单,在移动设备上打开和关闭导航链接菜单 。(稍后你将把新的 CSS 样式添加到 ` global.css ` 中。)
30+ 2 . 将以下代码复制到你的组件中。这将创建一个按钮,用于在移动端切换导航链接的可见性 。(稍后你将把新的 CSS 样式添加到 ` global.css ` 中。)
3131
32- ``` astro title="src/components/Hamburger .astro"
32+ ``` astro title="src/components/Menu .astro"
3333 ---
3434 ---
35- <div class="hamburger">
36- <span class="line"></span>
37- <span class="line"></span>
38- <span class="line"></span>
39- </div>
35+ <button aria-expanded="false" aria-controls="main-menu" class="menu">
36+ Menu
37+ </button>
4038 ```
4139
42- 3 . 将这个新的 ` <Hamburger /> ` 组件放置在 ` Header.astro ` 的 ` <Navigation /> ` 组件之前。
40+ 3 . 将这个新的 ` <Menu /> ` 组件放置在 ` Header.astro ` 的 ` <Navigation /> ` 组件之前。
4341
4442 <details >
4543 <summary >给我看看代码!</summary >
4644
4745 ``` astro title="src/components/Header.astro" ins={2,7}
4846 ---
49- import Hamburger from './Hamburger .astro';
47+ import Menu from './Menu .astro';
5048 import Navigation from './Navigation.astro';
5149 ---
5250 <header>
5351 <nav>
54- <Hamburger />
52+ <Menu />
5553 <Navigation />
5654 </nav>
5755 </header>
5856 ```
5957 </details >
6058
61- 4 . 为你的 Hamburger 组件添加以下样式:
59+ 4 . 为你的 Menu 组件添加以下样式,包括一些响应式样式 :
6260
63- ``` css title="src/styles/global.css" ins={2-13, 56-58 }
61+ ``` css title="src/styles/global.css" ins={2-9, 33-35, 51-53 }
6462 /* 导航样式 */
65- .hamburger {
66- padding-right : 20px ;
67- cursor : pointer ;
68- }
69-
70- .hamburger .line {
71- display : block ;
72- width : 40px ;
73- height : 5px ;
74- margin-bottom : 10px ;
75- background-color : #ff9776 ;
63+ .menu {
64+ background-color : #0d0950 ;
65+ border : none ;
66+ color : #fff ;
67+ font-size : 1.2rem ;
68+ font-weight : bold ;
69+ padding : 5px 10px ;
7670 }
7771
7872 .nav-links {
7973 width : 100% ;
80- top : 5rem ;
81- left : 48px ;
82- background-color : #ff9776 ;
8374 display : none ;
8475 margin : 0 ;
8576 }
@@ -92,13 +83,15 @@ import { Steps } from '@astrojs/starlight/components';
9283 font-size : 1.2rem ;
9384 font-weight : bold ;
9485 text-transform : uppercase ;
86+ color : #0d0950 ;
9587 }
9688
97- .nav-links a :hover , a :focus {
89+ .nav-links a :hover ,
90+ .nav-links a :focus {
9891 background-color : #ff9776 ;
9992 }
100-
101- . expanded {
93+
94+ :has ( .menu [ aria- expanded= " true " ]) .nav-links {
10295 display : unset ;
10396 }
10497
@@ -116,7 +109,7 @@ import { Steps } from '@astrojs/starlight/components';
116109 padding : 15px 20px ;
117110 }
118111
119- .hamburger {
112+ .menu {
120113 display : none ;
121114 }
122115 }
@@ -125,24 +118,27 @@ import { Steps } from '@astrojs/starlight/components';
125118
126119## 编写你的第一个 script 标签
127120
128- 你的页眉还不具有** 交互性** ,因为它无法响应用户输入,比如点击汉堡菜单来显示或隐藏导航链接菜单 。
121+ 你的页眉还不具有** 交互性** ,因为它无法响应用户输入,比如点击菜单来显示或隐藏导航链接菜单 。
129122
130123添加一个 ` <script> ` 标签提供客户端 JavaScript,以「监听」用户事件并做出相应的响应。
131124
132125<Steps >
1331261 . 将以下 ` <script> ` 标签添加到 ` index.astro ` ,就在结束的 ` </body> ` 标签之前。
134127
135- ``` astro title="src/pages/index.astro" ins={2-6 }
128+ ``` astro title="src/pages/index.astro" ins={2-9 }
136129 <Footer />
137130 <script>
138- document.querySelector('.hamburger')?.addEventListener('click', () => {
139- document.querySelector('.nav-links')?.classList.toggle('expanded');
131+ const menu = document.querySelector('.menu');
132+
133+ menu.addEventListener('click', () => {
134+ const isExpanded = menu.getAttribute('aria-expanded') === 'true';
135+ menu.setAttribute('aria-expanded', !isExpanded);
140136 });
141137 </script>
142138 </body>
143139 ```
144140
145- 2 . 再次在各种尺寸下检查你的浏览器预览,并验证你是否在这个页面上拥有一个能够根据屏幕尺寸响应并对用户输入作出响应的正常运作的汉堡菜单 。
141+ 2 . 再次在各种尺寸下检查你的浏览器预览,并验证你是否在这个页面上拥有一个能够根据屏幕尺寸响应并对用户输入作出响应的正常运作的导航菜单 。
146142</Steps >
147143
148144### 导入一个 ` .js ` 文件
@@ -153,26 +149,32 @@ import { Steps } from '@astrojs/starlight/components';
1531491 . 创建 ` src/scripts/menu.js ` (你将不得不创建一个新的 ` /scripts/ ` 文件夹),并将你的 JavaScript 移动到其中。
154150
155151 ``` js title="src/scripts/menu.js"
156- document .querySelector (' .hamburger' ).addEventListener (' click' , () => {
157- document .querySelector (' .nav-links' ).classList .toggle (' expanded' );
152+ const menu = document .querySelector (' .menu' );
153+
154+ menu .addEventListener (' click' , () => {
155+ const isExpanded = menu .getAttribute (' aria-expanded' ) === ' true' ;
156+ menu .setAttribute (' aria-expanded' , ! isExpanded);
158157 });
159158 ```
160159
1611602 . 用以下文件导入替换 ` index.astro ` 中的 ` <script> ` 标签的内容:
162161
163- ``` astro title="src/pages/index.astro" ins={7 } del={3-5 }
162+ ``` astro title="src/pages/index.astro" ins={10 } del={3-8 }
164163 <Footer />
165164 <script>
166- document.querySelector('.hamburger')?.addEventListener('click', () => {
167- document.querySelector('.nav-links')?.classList.toggle('expanded');
165+ const menu = document.querySelector('.menu');
166+
167+ menu.addEventListener('click', () => {
168+ const isExpanded = menu.getAttribute('aria-expanded') === 'true';
169+ menu.setAttribute('aria-expanded', !isExpanded);
168170 });
169171
170172 import "../scripts/menu.js";
171173 </script>
172174 </body>
173175 ```
174176
175- 3 . 再次在较小的尺寸下检查你的浏览器预览,验证汉堡菜单是否仍然可以打开和关闭导航链接菜单 。
177+ 3 . 再次在较小的尺寸下检查你的浏览器预览,验证菜单是否仍然可以打开和关闭导航链接菜单 。
176178
1771794 . 在你的其他两个页面 ` about.astro ` 和 ` blog.astro ` 中添加相同的带有导入的 ` <script> ` ,并验证每个页面上是否有一个响应式、交互式的页眉。
178180
0 commit comments