Start Screen and Title چالش 5 از جلسهی 14، متوسط
در این اپ CSS همهی صفحهها را اول پنهان میکند. دو کار کن: (۱) در show(name) متن سربرگ #title را از شیء titles بگذار، یعنی titles[name]. (۲) آخر اسکریپت show("home") را صدا بزن تا اپ با خانه شروع شود.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My App</title>
<style>
body { font-family: sans-serif; margin: 0; background-color: #f6f7fb; }
header { background-color: #3949ab; color: white; padding: 12px 16px; }
.screen { max-width: 420px; margin: 0 auto; padding: 16px 16px 90px; }
nav { position: fixed; bottom: 0; left: 0; right: 0; display: flex; background-color: white; }
nav button { flex: 1; min-height: 56px; font-size: 16px; border: none; background-color: white; }
nav button.active { background-color: #ffd23f; font-weight: bold; }
.screen { display: none; }
</style>
</head>
<body>
<header><h1 id="title">My App</h1></header>
<section class="screen" id="home">
<h2>Home</h2>
<p>Welcome! Pick a screen below.</p>
</section>
<section class="screen" id="list">
<h2>My List</h2>
<ul>
<li>Read a book</li>
<li>Drink water</li>
</ul>
</section>
<section class="screen" id="settings">
<h2>Settings</h2>
<p>Nothing here yet.</p>
</section>
<nav>
<button id="tab-home" onclick="show('home')">Home</button>
<button id="tab-list" onclick="show('list')">List</button>
<button id="tab-settings" onclick="show('settings')">Settings</button>
</nav>
<script>
let titles = { home: "Home", list: "My List", settings: "Settings" };
function show(name) {
let screens = document.querySelectorAll(".screen");
for (let screen of screens) {
screen.style.display = "none";
}
document.getElementById(name).style.display = "block";
let tabs = document.querySelectorAll("nav button");
for (let tab of tabs) {
tab.classList.remove("active");
}
document.getElementById("tab-" + name).classList.add("active");
}
</script>
</body>
</html>
راهنما و جواب این چالش · برگشت به درس جلسهی 14: چند صفحه · چالش بعدی: Welcome Steps
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js