Files
modelname/index.js
2025-11-07 08:36:00 +00:00

323 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

< !DOCTYPE html >
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Index.js 示例应用</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
color: #fff;
min-height: 100vh;
padding: 20px;
}
.container {
max - width: 1200px;
margin: 0 auto;
}
header {
text - align: center;
padding: 30px 0;
margin-bottom: 40px;
}
h1 {
font - size: 2.8rem;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}
.subtitle {
font - size: 1.2rem;
opacity: 0.9;
}
.card-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 25px;
margin-bottom: 40px;
}
.card {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 25px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
}
.card h2 {
font - size: 1.5rem;
margin-bottom: 15px;
display: flex;
align-items: center;
gap: 10px;
}
.card h2 i {
font - size: 1.8rem;
}
.card p {
line - height: 1.6;
margin-bottom: 20px;
}
.btn {
display: inline-block;
background: #ff6b6b;
color: white;
padding: 10px 20px;
border-radius: 50px;
text-decoration: none;
font-weight: 600;
transition: all 0.3s ease;
border: none;
cursor: pointer;
}
.btn:hover {
background: #ff5252;
transform: scale(1.05);
}
.demo-area {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-radius: 15px;
padding: 30px;
margin-top: 30px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}
.demo-area h2 {
margin - bottom: 20px;
font-size: 1.8rem;
}
.counter {
display: flex;
align-items: center;
justify-content: center;
gap: 20px;
margin-bottom: 30px;
}
.counter button {
width: 50px;
height: 50px;
border-radius: 50%;
font-size: 1.5rem;
display: flex;
align-items: center;
justify-content: center;
}
.counter span {
font - size: 2.5rem;
font-weight: bold;
min-width: 80px;
text-align: center;
}
.user-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 15px;
margin-top: 20px;
}
.user-card {
background: rgba(255, 255, 255, 0.15);
padding: 15px;
border-radius: 10px;
text-align: center;
}
.user-card img {
width: 60px;
height: 60px;
border-radius: 50%;
margin-bottom: 10px;
object-fit: cover;
}
.user-card h3 {
font - size: 1rem;
margin-bottom: 5px;
}
.user-card p {
font - size: 0.8rem;
opacity: 0.8;
}
footer {
text - align: center;
margin-top: 50px;
padding: 20px;
font-size: 0.9rem;
opacity: 0.8;
}
@media (max-width: 768px) {
h1 {
font - size: 2.2rem;
}
.card-container {
grid - template - columns: 1fr;
}
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>Index.js 示例应用</h1>
<p class="subtitle">展示现代JavaScript在前端开发中的强大功能</p>
</header>
<div class="card-container">
<div class="card">
<h2><i>📁</i> </h2>
<p>使用ES6模块系统组织代码提高可维护性和复用性</p>
<button class="btn" onclick="showModuleExample()">查看示例</button>
</div>
<div class="card">
<h2><i>🔄</i> </h2>
<p>使用async/await和Promise处理异步操作提高代码可读性</p>
<button class="btn" onclick="showAsyncExample()">查看示例</button>
</div>
<div class="card">
<h2><i>🎯</i> DOM</h2>
<p>使用现代JavaScript高效操作DOM创建动态用户界面</p>
<button class="btn" onclick="showDOMExample()">查看示例</button>
</div>
</div>
<div class="demo-area">
<h2>JavaScript功能演示</h2>
<div class="counter">
<button class="btn" onclick="decrement()">-</button>
<span id="count">0</span>
<button class="btn" onclick="increment()">+</button>
</div>
<button class="btn" onclick="fetchUsers()">加载用户数据</button>
<div id="user-list" class="user-list">
<!-- 用户数据将在这里显示 -->
</div>
</div>
<footer>
<p>Index.js 示例应用 &copy; 2023 | 展示现代JavaScript开发实践</p>
</footer>
</div>
<script>
// 计数器状态
let count = 0;
// DOM元素引用
const countElement = document.getElementById('count');
const userListElement = document.getElementById('user-list');
// 计数器功能
function increment() {
count++;
updateCountDisplay();
}
function decrement() {
count--;
updateCountDisplay();
}
function updateCountDisplay() {
countElement.textContent = count;
// 添加动画效果
countElement.style.transform = 'scale(1.2)';
setTimeout(() => {
countElement.style.transform = 'scale(1)';
}, 200);
}
// 异步获取用户数据
async function fetchUsers() {
try {
userListElement.innerHTML = '<p>加载中...</p>';
// 使用公共API获取随机用户数据
const response = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await response.json();
displayUsers(users.slice(0, 6)); // 只显示前6个用户
} catch (error) {
userListElement.innerHTML = '<p>加载用户数据时出错</p>';
console.error('Error fetching users:', error);
}
}
// 显示用户数据
function displayUsers(users) {
userListElement.innerHTML = '';
users.forEach(user => {
const userCard = document.createElement('div');
userCard.className = 'user-card';
// 生成随机头像(使用占位符服务)
const randomId = Math.floor(Math.random() * 100);
const avatarUrl = `https://i.pravatar.cc/100?img=${randomId}`;
userCard.innerHTML = `
<img src="${avatarUrl}" alt="${user.name}">
<h3>${user.name}</h3>
<p>${user.email}</p>
`;
userListElement.appendChild(userCard);
});
}
// 示例功能
function showModuleExample() {
alert('模块化示例:\n\n// math.js\nexport function add(a, b) {\n return a + b;\n}\n\n// index.js\nimport { add } from \'./math.js\';\nconsole.log(add(2, 3)); // 5');
}
function showAsyncExample() {
alert('异步操作示例:\n\nasync function fetchData() {\n try {\n const response = await fetch(\'api/data\');\n const data = await response.json();\n return data;\n } catch (error) {\n console.error(\'Error:\', error);\n }\n}');
}
function showDOMExample() {
alert('DOM操作示例\n\n// 创建元素\nconst newElement = document.createElement(\'div\');\nnewElement.textContent = \'Hello World!\';\n\n// 添加到DOM\ndocument.body.appendChild(newElement);\n\n// 事件监听\nnewElement.addEventListener(\'click\', () => {\n alert(\'元素被点击了!\');\n});');
}
// 初始化
document.addEventListener('DOMContentLoaded', () => {
updateCountDisplay();
console.log('Index.js 示例应用已加载完成!');
});
</script>
</body>
</html>