Remember the Last Screen چالش 8 از جلسهی 14، سخت
اگر کاربر اپ را در صفحهی تنظیمات ببندد، دفعهی بعد باید همانجا باز شود. در show(name) اسم صفحه را با salam.save("screen", name) ذخیره کن. آخر اسکریپت بهجای show("home") بنویس show(salam.load("screen", "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>
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");
}
show("home");
</script>
</body>
</html>
راهنما و جواب این چالش · برگشت به درس جلسهی 14: چند صفحه
بیشتر بخوان: جاوااسکریپت چیست؟ از دکمهی وب تا Node.js