变量
本课属于 Coddy HTML 学习旅程的“JavaScript 实战”单元。第 21 课,共 27 课。
在这个挑战中,我们将制作一个可折叠的侧边导航菜单。目标是完成整个脚本,并添加 CSS 过渡效果,让导航菜单能够平滑地展开和收起。
挑战
简单- 为菜单 div 设置 ID
sideNav。 - 在脚本中创建三个变量:
- 一个用于存储侧边导航元素(
sideNav) - 一个用于存储打开按钮(
openBtn) - 一个用于存储关闭按钮(
closeBtn)
亲手试试
<!DOCTYPE html>
<html>
<head>
<title>Space Exploration</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="sidenav">
<button id="closeBtn" class="close-btn">×</button>
<a href="#apollo">Apollo Missions</a>
<a href="#iss">International Space Station</a>
<a href="#mars">Mars Rovers</a>
<a href="#jameswebb">James Webb Telescope</a>
</div>
<header>
<button id="openBtn" class="open-btn">☰ Menu</button>
<h1>Space Exploration</h1>
</header>
<main>
<section id="apollo">
<h2>Apollo Missions</h2>
<p>
The Apollo program, carried out by NASA in the 1960s and 1970s,
successfully landed humans on the Moon for the first time in 1969.
</p>
</section>
<section id="iss">
<h2>International Space Station</h2>
<p>
The ISS is a multinational research laboratory in low Earth orbit,
continuously inhabited since 2000 by astronauts and scientists.
</p>
</section>
<section id="mars">
<h2>Mars Rovers</h2>
<p>
Rovers like Spirit, Opportunity, Curiosity, and Perseverance
have been exploring the Martian surface, searching for signs of past life.
</p>
</section>
<section id="jameswebb">
<h2>James Webb Space Telescope</h2>
<p>
Launched in 2021, the James Webb Telescope is providing
breathtaking images of the universe and studying distant galaxies.
</p>
</section>
</main>
<script src="script.js"></script>
</body>
</html>
这节课包含一个小测验。开始学习即可作答,并记录你的进度。
JavaScript 实战中的所有课
自己练习: Web 在线编辑器