add examples

This commit is contained in:
many2many 2024-09-30 08:10:21 +08:00
parent ed6d72e413
commit 3204d35e53
6 changed files with 471 additions and 0 deletions

View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Live User Filter</title>
</head>
<body>
<div class="container">
<header class="header">
<h4 class="title">Live User Filter</h4>
<small class="subtitle">Search by name and/or location</small>
<input type="text" name="filter" id="filter" placeholder="Search" />
</header>
<ul id="result" class="user-list">
<li><h3>Loading...</h3></li>
</ul>
</div>
<script src="script.js"></script>
</body>
</html>

View File

@ -0,0 +1,38 @@
const result = document.getElementById("result");
const filter = document.getElementById("filter");
const listItems = [];
const filterData = (searchTerm) => {
listItems.forEach((item) => {
if (item.innerText.toLowerCase().includes(searchTerm.toLowerCase())) {
item.classList.remove("hide");
} else {
item.classList.add("hide");
}
});
};
const getData = async () => {
const res = await fetch("https://randomuser.me/api?results=50");
const { results } = await res.json();
result.innerHTML = "";
results.forEach((user) => {
const li = document.createElement("li");
listItems.push(li);
li.innerHTML = `
<img
src="${user.picture.large}"
alt="${user.name.first}"
/>
<div class="user-info">
<h4>${user.name.first} ${user.name.last}</h4>
<p>${user.location.city}, ${user.location.country}</p>
</div>
`;
result.appendChild(li);
});
};
getData();
filter.addEventListener("input", (e) => filterData(e.target.value));

View File

@ -0,0 +1,94 @@
@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap");
* {
box-sizing: border-box;
}
body {
background-color: #f8f9fd;
font-family: "Roboto", sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}
.container {
border-radius: 5px;
box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2);
overflow: hidden;
width: 300px;
}
.title {
margin: 0;
}
.subtitle {
display: inline-block;
margin: 5px 0 20px;
opacity: 0.8;
}
.header {
background-color: #3e57db;
color: #fff;
padding: 30px 20px;
}
.header input {
background-color: rgba(0, 0, 0, 0.3);
border: 0;
border-radius: 50px;
color: #fff;
font-size: 14px;
padding: 10px 15px;
width: 100%;
}
.header input:focus {
outline: none;
}
.user-list {
background-color: #fff;
list-style-type: none;
margin: 0;
padding: 0;
max-height: 400px;
overflow-y: auto;
}
.user-list li {
display: flex;
padding: 20px;
}
.user-list img {
border-radius: 50%;
object-fit: cover;
height: 50px;
width: 50px;
}
.user-list .user-info {
margin-left: 10px;
}
.user-list .user-info h4 {
margin: 0 0 10px;
}
.user-list .user-info p {
font-size: 12px;
}
.user-list li:not(:last-of-type) {
border-bottom: 1px solid #eee;
}
.user-list li.hide {
display: none;
}

View File

@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Hangman</title>
</head>
<body>
<h1>Hangman</h1>
<p>Find the hidden word - Press a letter</p>
<div class="game-container">
<svg height="250" width="200" class="figure-container">
<!-- rod -->
<line x1="60" y1="20" x2="140" y2="20" />
<line x1="140" y1="20" x2="140" y2="50" />
<line x1="60" y1="20" x2="60" y2="230" />
<line x1="20" y1="230" x2="100" y2="230" />
<!-- head -->
<circle cx="140" cy="70" r="20" class="figure-part" />
<!-- body -->
<line x1="140" y1="90" x2="140" y2="150" class="figure-part" />
<!-- arms -->
<line x1="140" y1="120" x2="120" y2="100" class="figure-part" />
<line x1="140" y1="120" x2="160" y2="100" class="figure-part" />
<!-- legs -->
<line x1="140" y1="150" x2="120" y2="180" class="figure-part" />
<line x1="140" y1="150" x2="160" y2="180" class="figure-part" />
</svg>
<div class="wrong-letters-container">
<div id="wrong-letters"></div>
</div>
<div class="word" id="word"></div>
</div>
<!-- Popup -->
<div class="popup-container" id="popup-container">
<div class="popup">
<h2 id="final-message"></h2>
<h3 id="final-message-reveal-word"></h3>
<button id="play-button">Play Again</button>
</div>
</div>
<!-- Notification -->
<div class="notification-container" id="notification-container">
<p>You have already entered this letter</p>
</div>
<!--<script src="script.js"></script>-->
</body>
</html>

View File

@ -0,0 +1,125 @@
const wordElement = document.getElementById("word");
const wrongLettersElement = document.getElementById("wrong-letters");
const playAgainButton = document.getElementById("play-button");
const popup = document.getElementById("popup-container");
const notification = document.getElementById("notification-container");
const finalMessage = document.getElementById("final-message");
const finalMessageRevealWord = document.getElementById(
"final-message-reveal-word"
);
const figureParts = document.querySelectorAll(".figure-part");
const words = [
"application",
"programming",
"interface",
"wizard",
"element",
"prototype",
"callback",
"undefined",
"arguments",
"settings",
"selector",
"container",
"instance",
"response",
"console",
"constructor",
"token",
"function",
"return",
"length",
"type",
"node",
];
let selectedWord = words[Math.floor(Math.random() * words.length)];
let playable = true;
const correctLetters = [];
const wrongLetters = [];
function displayWord() {
wordElement.innerHTML = `
${selectedWord
.split("") // to array
.map(
(letter) => `
<span class="letter">
${correctLetters.includes(letter) ? letter : ""}
</span>
`
)
.join("")}
`; // to string
const innerWord = wordElement.innerText.replace(/\n/g, "");
if (innerWord === selectedWord) {
finalMessage.innerText = "Congratulations! You won! 😃";
finalMessageRevealWord.innerText = "";
popup.style.display = "flex";
playable = false;
}
}
function updateWrongLettersElement() {
wrongLettersElement.innerHTML = `
${wrongLetters.length > 0 ? "<p>Wrong</p>" : ""}
${wrongLetters.map((letter) => `<span>${letter}</span>`)}
`;
figureParts.forEach((part, index) => {
const errors = wrongLetters.length;
index < errors
? (part.style.display = "block")
: (part.style.display = "none");
});
if (wrongLetters.length === figureParts.length) {
finalMessage.innerText = "Unfortunately you lost. 😕";
finalMessageRevealWord.innerText = `...the word was: ${selectedWord}`;
popup.style.display = "flex";
playable = false;
}
}
function showNotification() {
notification.classList.add("show");
setTimeout(() => {
notification.classList.remove("show");
}, 2000);
}
window.addEventListener("keypress", (e) => {
if (playable) {
const letter = e.key.toLowerCase();
if (letter >= "a" && letter <= "z") {
if (selectedWord.includes(letter)) {
if (!correctLetters.includes(letter)) {
correctLetters.push(letter);
displayWord();
} else {
showNotification();
}
} else {
if (!wrongLetters.includes(letter)) {
wrongLetters.push(letter);
updateWrongLettersElement();
} else {
showNotification();
}
}
}
}
});
playAgainButton.addEventListener("click", () => {
playable = true;
correctLetters.splice(0);
wrongLetters.splice(0);
selectedWord = words[Math.floor(Math.random() * words.length)];
displayWord();
updateWrongLettersElement();
popup.style.display = "none";
});
// Init
displayWord();

View File

@ -0,0 +1,143 @@
@import url("https://fonts.googleapis.com/css2?family=DotGothic16&display=swap");
:root {
--primary-color: #1f2f61;
--secondary-color: #224ca4;
--light-color: #a7c2da;
}
* {
box-sizing: border-box;
}
body {
background-color: var(--primary-color);
color: var(--light-color);
font-family: "DotGothic16", sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 80vh;
overflow: hidden;
margin: 0;
}
h1 {
margin: 20px 0 0;
letter-spacing: 0.5rem;
text-transform: uppercase;
}
h2,
h3 {
letter-spacing: 0.2rem;
}
.game-container {
padding: 20px 30px;
position: relative;
margin: auto;
height: 350px;
width: 450px;
}
.figure-container {
fill: transparent;
stroke: var(--light-color);
stroke-width: 4px;
stroke-linecap: round;
}
.wrong-letters-container {
position: absolute;
top: 20px;
right: 20px;
display: flex;
flex-direction: column;
text-align: right;
}
.wrong-letters-container p {
margin: 0 0 5px;
}
.wrong-letters-container span {
font-size: 24px;
}
.word {
display: flex;
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
.letter {
border-bottom: 3px solid var(--secondary-color);
display: inline-flex;
font-size: 30px;
align-items: center;
justify-content: center;
margin: 0 3px;
height: 50px;
width: 20px;
}
.popup-container {
background-color: rgba(0, 0, 0, 0.3);
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: none;
align-items: center;
justify-content: center;
}
.popup {
background-color: var(--secondary-color);
border-radius: 5px;
box-shadow: 0 15px 10px 3px rgba(0, 0, 0, 0.1);
padding: 20px;
text-align: center;
}
.popup button {
cursor: pointer;
background-color: var(--light-color);
color: var(--secondary-color);
border: 0;
margin-top: 20px;
padding: 12px 20px;
font-size: 16px;
font-family: inherit;
border-radius: 5px;
}
.popup button:active {
transform: scale(0.98);
}
.popup button:focus {
outline: none;
}
.notification-container {
background-color: rgba(0, 0, 0, 0.3);
border-radius: 10px 10px 0 0;
padding: 15px 20px;
position: absolute;
bottom: -60px;
transition: transform 0.3s ease-in-out;
}
.notification-container.show {
transform: translateY(-60px);
}
.notification-container p {
margin: 0;
}