94 lines
2.3 KiB
HTML
94 lines
2.3 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>What to Eat Today?</title>
|
|
<style>
|
|
html, body {
|
|
width: 100%;
|
|
height: 100%;
|
|
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
.container {
|
|
display: flex;
|
|
height: 100%;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
.wrapper {
|
|
max-width: 1200px;
|
|
text-align: center;
|
|
}
|
|
#dish-name {
|
|
font-size: 3rem;
|
|
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
|
|
}
|
|
#re-random {
|
|
width: 3rem;
|
|
margin: auto;
|
|
border-radius: 10%;
|
|
padding: .8rem;
|
|
background-color: cadetblue;
|
|
color: white;
|
|
|
|
user-select: none;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="wrapper">
|
|
<p>今天吃</p>
|
|
<p id="dish-name">Undefined</p>
|
|
<p id="re-random">换一个</p>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
<script>
|
|
class DishRandom {
|
|
|
|
constructor(dishNameElement, dishes, reRandomElement) {
|
|
this.dishNameEl = dishNameElement
|
|
this.dishes = dishes
|
|
|
|
this.random()
|
|
|
|
if(reRandomElement) {
|
|
reRandomElement.addEventListener("click", () => {
|
|
this.random()
|
|
})
|
|
}
|
|
}
|
|
|
|
get dishName() {
|
|
return this.dishNameEl.innerText
|
|
}
|
|
|
|
set dishName(name) {
|
|
this.dishNameEl.innerText = name
|
|
}
|
|
|
|
random() {
|
|
let index = Math.floor(Math.random() * this.dishes.length)
|
|
this.dishName = this.dishes[index]
|
|
}
|
|
|
|
}
|
|
|
|
fetch("./dishes.json").then(r => r.json()).then(dishes => {
|
|
new DishRandom(
|
|
document.getElementById("dish-name"),
|
|
dishes,
|
|
document.getElementById("re-random")
|
|
)
|
|
}).catch(e => {
|
|
document.getElementById("dish-name").innerText = "菜谱丢了"
|
|
console.error(e)
|
|
})
|
|
|
|
|
|
</script>
|
|
</html> |