这段代码是一个简单的网页应用,用于显示一个“Pokedex”(来自Pokemon系列的怪物图鉴)。下面是代码的分析和解释:
HTML结构
<html>
: 定义HTML文档的根元素。<head>
: 包含文档的元数据,如字符集、视口设置、样式和标题。<body>
: 包含主要的页面内容,包括标题和一个用于显示Pokemon卡片的容器。
CSS样式
* { box-sizing: border-box; }
: 确保所有元素的padding和border都包含在元素的总宽度和高度内。body
: 设置了背景渐变、字体、布局方式(flex)以及居中对齐。h1
: 增加了字母间距。.poke-container
: 设置为flex容器,用于包裹所有的Pokemon卡片,支持换行和居中对齐。.pokemon
: 定义了每个Pokemon卡片的样式,包括背景色、圆角、阴影、边距和填充。.img-container
和img
: 定义了图片容器的样式和图片的最大宽度。.info
,.number
,.name
: 定义了Pokemon信息的样式,如编号、名字和类型的展示方式。
JavaScript功能
- 变量定义:
poke_container
: 获取页面中用于放置Pokemon卡片的容器。pokemon_count
: 定义了要显示的Pokemon总数(150个)。colors
: 一个对象,存储了不同Pokemon类型对应的颜色。main_types
: 获取colors
对象的所有键,即Pokemon的所有类型。
- 函数:
fetchPokemons
: 异步函数,循环调用getPokemon
函数以获取每个Pokemon的数据。getPokemon
: 异步函数,用于从API获取特定id的Pokemon数据。这里的url
变量为空,需要填写正确的API URL。createPokemonCard
: 根据获取到的Pokemon数据创建一个卡片,并添加到页面上。这个函数处理了名字的首字母大写、编号的格式化、类型的颜色匹配以及HTML结构的创建。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
* {
box-sizing: border-box;
}
body {
background: #efefbb;
background: linear-gradient(to right, #d4d3dd, #efefbb);
font-family: 'Lato', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin: 0;
}
h1 {
letter-spacing: 3px;
}
.poke-container {
display: flex;
flex-wrap: wrap;
align-items: space-between;
justify-content: center;
margin: 0 auto;
max-width: 1200px;
}
.pokemon {
background-color: #eee;
border-radius: 10px;
box-shadow: 0 3px 15px rgba(100, 100, 100, 0.5);
margin: 10px;
padding: 20px;
text-align: center;
}
.pokemon .img-container {
background-color: rgba(255, 255, 255, 0.6);
border-radius: 50%;
width: 120px;
height: 120px;
text-align: center;
}
.pokemon .img-container img {
max-width: 90%;
margin-top: 20px;
}
.pokemon .info {
margin-top: 20px;
}
.pokemon .info .number {
background-color: rgba(0, 0, 0, 0.1);
padding: 5px 10px;
border-radius: 10px;
font-size: 0.8em;
}
.pokemon .info .name {
margin: 15px 0 7px;
letter-spacing: 1px;
}
</style>
<title>Pokedex</title>
</head>
<body>
<h1>Pokedex</h1>
<div class="poke-container" id="poke-container"></div>
<!-- Design inspired by this Dribbble shot: https://dribbble.com/shots/5611109--Pokemon -->
<script>
const poke_container = document.getElementById('poke-container')
const pokemon_count = 150
const colors = {
fire: '#FDDFDF',
grass: '#DEFDE0',
electric: '#FCF7DE',
water: '#DEF3FD',
ground: '#f4e7da',
rock: '#d5d5d4',
fairy: '#fceaff',
poison: '#98d7a5',
bug: '#f8d5a3',
dragon: '#97b3e6',
psychic: '#eaeda1',
flying: '#F5F5F5',
fighting: '#E6E0D4',
normal: '#F5F5F5'
}
const main_types = Object.keys(colors)
const fetchPokemons = async () => {
for (let i = 1; i <= pokemon_count; i++) {
await getPokemon(i)
}
}
const getPokemon = async (id) => {
const url = `https://pokeapi.co/api/v2/pokemon/${id}`
const res = await fetch(url)
const data = await res.json()
createPokemonCard(data)
}
const createPokemonCard = (pokemon) => {
const pokemonEl = document.createElement('div')
pokemonEl.classList.add('pokemon')
const name = pokemon.name[0].toUpperCase() + pokemon.name.slice(1)
const id = pokemon.id.toString().padStart(3, '0')
const poke_types = pokemon.types.map(type => type.type.name)
const type = main_types.find(type => poke_types.indexOf(type) > -1)
const color = colors[type]
pokemonEl.style.backgroundColor = color
const pokemonInnerHTML = `
<div class="img-container">
<img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon.id}.png"" alt="${name}">
</div>
<div class="info">
<span class="number">#${id}</span>
<h3 class="name">${name}</h3>
<small class="type">Type: <span>${type}</span> </small>
</div>
`
pokemonEl.innerHTML = pokemonInnerHTML
poke_container.appendChild(pokemonEl)
}
fetchPokemons()
</script>
</body>
</html>