day37-Pokedex(神奇宝贝图鉴卡牌展示)

news2024/9/29 19:19:31

50 天学习 50 个项目 - HTMLCSS and JavaScript

day37-Pokedex(神奇宝贝图鉴卡牌展示)

效果

在这里插入图片描述

index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Pokedex</title>
    <link rel="stylesheet" href="style.css" />
</head>

<body>
    <h1>神奇宝贝图鉴</h1>
    <!-- 容器 -->
    <div class="poke-container" id="poke-container"></div>

    <!-- Design inspired by this Dribbble shot: https://dribbble.com/shots/5611109--Pokemon -->
    <script src="script.js"></script>
</body>

</html>

style.css

@import url('https://fonts.googleapis.com/css?family=Lato:300,400&display=swap');

* {
    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 {
    /* 字符之间的间距为3px */
    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;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

/* 卡片图片容器 */
.pokemon .img-container {
    background-color: rgba(255, 255, 255, 0.6);
    border-radius: 50%;
    width: 120px;
    height: 120px;
    text-align: center;
    display: flex;
    justify-content: center;
}

/* 图片 */
.pokemon .img-container img {
    max-width: 100%;
    margin-top: 20px;
}

/* 卡片信息容器 */
.pokemon .info {
    margin-top: 20px;
}

/* id */
.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;
}

script.js

// 重点 flex Object.keys() pokemon.name[0].toUpperCase() + pokemon.name.slice(1)
// pokemon.id.toString().padStart(3, '0') async await
// 1.获取元素节点
const poke_container = document.getElementById('poke-container')//卡片容器
const pokemon_count = 100 //设置图鉴的数量
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 namesChineseList = {

}

// ['fire', 'grass', 'electric', 'water', 'ground', 'rock', 'fairy', 'poison', 'bug', 'dragon', 'psychic', 'flying', 'fighting', 'normal']
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)
    // console.log(res);
    const data = await res.json()
    // console.log(data);
    createPokemonCard(data)
}
// 创建卡片
const createPokemonCard = async (pokemon) => {
    // 创建卡片 添加相关类
    const pokemonEl = document.createElement('div')
    pokemonEl.classList.add('pokemon')
    // pokemon.name 为bulbasaur 
    // 第一个字母大写并拼接之后的字符
    const name = pokemon.name[0].toUpperCase() + pokemon.name.slice(1)
    // 调用qq翻译API,将其翻译为中文
    // const nameResult = await fetch(`https://api.oioweb.cn/api/txt/QQFanyi?sourceText=${name}`)
    // const nameData = await nameResult.json()
    // console.log(nameData.result.targetText);
    // pokemon.id 1
    // 转为字符串 并padStart()方法在字符串前面填充零,使其总长度达到3位。
    const id = pokemon.id.toString().padStart(3, '0')
    // pokemon.types 一个对象数组
    /**
     * {
            "slot": 1, 
            "type": {
                "name": "grass", 
                "url": "https://pokeapi.co/api/v2/type/12/"
            }
        }, 等等
     */
    // 卡片所属类型
    const poke_types = pokemon.types.map(type => type.type.name)
    // console.log(poke_types);//['grass', 'poison']
    // 找出第一个的所属类型 并竖直颜色 grass
    const type = main_types.find(type => poke_types.indexOf(type) > -1)
    // #DEFDE0
    const color = colors[type]

    pokemonEl.style.backgroundColor = color
    //图片 https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/1.png
    // pokemon.sprites.front_default
    const pokemonInnerHTML = `
    <div class="img-container">
        <img src="${pokemon.sprites.front_default}" 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>
    `
    // 设置卡片的html
    pokemonEl.innerHTML = pokemonInnerHTML
    // 添加置容器中
    poke_container.appendChild(pokemonEl)
}
// 初始化执行
fetchPokemons()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/788871.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vscode默认gbk编码格式打开

目录 1. 问题描述2. 解决方案 1. 问题描述 每次打开vscode都是utf-8格式打开文件&#xff0c;然后满屏的中文乱码&#xff0c;自己手动换成gbk编码 后中文显示正常&#xff0c;但是换多了很烦。 2. 解决方案 ctrlshiftP 点首选项&#xff1a;打开用户设置 加上这行在最后&…

文件包含漏洞利用思路

简介 通过PHP函数引入文件时&#xff0c;传入的文件名没有经过合理的验证&#xff0c;从而操作了预想之外的文件&#xff0c;导致意外的文件泄漏甚至恶意代码注入。 常见的文件包含函数 php中常见的文件包含函数有以下四种&#xff1a; include()require()include_once()re…

教你合法安全匿名访问网站的10个技巧

大家常说互联网大数据使得每个人上网更透明&#xff0c;确实隐私性和安全性已成为人们在进行互联网浏览时所关注的焦点。无论您想要对匿名访问目标网站进行调查研究&#xff0c;又或者是爬虫业务、跨境业务&#xff0c;完全保持匿名都可能成为关键。 为了帮助您保护更多隐私和安…

【OC总结-weak的底层原理】

文章目录 1. SideTables1.1 StripedMap1.2 SideTable1.3 引用计数refcnts 存储结构RefcountMap1.4 weak_table_t结构体1.4.1 .weak_entry_t结构体 2.1 weak的实现及其调用的相关函数2.1 初始化时&#xff1a;2.1.1 objc_initWeak方法2.1.2 storeWeak方法 2.2 添加引用时&#x…

【C++从0到王者】第十站:手撕string

文章目录 一、String的基本结构二、String的构造函数1.string(const char* str)2.string()3.string(const char* str " ")4.string(const string& s) 二、String的析构函数三、获取String中的字符串四、获取有效元素个数五、operator[]运算符重载六、增删查改之…

Fastify系列-从0到1超详细手把手教你使用Fastify构建快速的API

什么是Fastify&#xff1f; Fastify是一个web框架&#xff0c;高度专注于以最少的开销和强大的插件架构提供最佳的开发体验。它的灵感来自于Hapi和Express&#xff0c;据我们所知&#xff0c;它是运行在Node.js上的最快的Web框架之一。 为什么使用Fastify&#xff1f; 这些是…

PostgreSQL--实现数据库备份恢复详细教学

前言 这是我在这个网站整理的笔记&#xff0c;关注我&#xff0c;接下来还会持续更新。 作者&#xff1a;RodmaChen PostgreSQL--实现数据库备份恢复详细教学 一. 数据库备份二. 数据库恢复三. 存留问题 数据库备份恢复功能是每个产品所需的&#xff0c;以下是简单的脚本案例&a…

遇到了一个存在XSS(存储型)漏洞的网站

第一个漏洞self xss&#xff08;存储型&#xff09; 存在漏洞的网站是https://www.kuangstudy.com/ 然后点击个人设置 在编辑主页中&#xff0c;我们可以用最简单的script语句进行注入&#xff0c;提交&#xff1b; 出现弹窗&#xff0c;说明它已经把代码进行解析&#x…

【设计模式学习1】什么是单例模式?单例模式的几种实现。

一、什么是单例模式 单例模式是在内存中只创建一个对象的模式&#xff0c;它保证一个类只有一个实例。 二、单例模式的几种实现 &#xff08;一&#xff09;懒汉式单例模式 /*** 懒汉式单例模式* &#xff08;懒加载&#xff0c;需要的时候在去加载&#xff09;* 优点&…

递归——另类加法、走方格的方案数

大家好&#xff0c;这里是bang_bang&#xff0c;今天来记录2道递归的典型题目 目录 1.另类加法 2.走方格的方案数 1.另类加法 另类加法__牛客网 (nowcoder.com) 给定两个int A和B。编写一个函数返回AB的值&#xff0c;但不得使用或其他算数运算符。 测试样例&#xff1a;…

【Linux】system V IPC原理分析

目录 System V IPC分类 key_t 键和ftok函数 ipc_perm结构 创建和打开IPC通道 IPC权限问题 理解IPC工作原理 System V IPC分类 System V 消息队列System V 共享内存System V 信号量 之所以称为System V IPC是因为这三种IPC机制都是来源于System V Unix的实现。 消息队列信…

一款好用的思维导图软件drawio

最近需要画思维导图&#xff0c;结果发现既然被人用来收费了。所以记录一下&#xff0c;免得大家上当。 首先说明&#xff0c;这个东东在github上是免费开源的&#xff0c;收费的是一些不法分子搞得。下面是收费版本得界面。 开源地址&#xff1a; https://github.com/jgraph…

坑爹的shadow -- 总结 与 各种坑

作者&#xff1a;snwrking 最近公司来了新UX总监, 很喜欢给设计添加浓重的, 而且是好几层的阴影. 这下就苦了我们Android开发了. 因为是Android不支持啊, 巧妇也难为无米之炊啊. (折中方法也不是没有, 就是自己把阴影做个view, 但它的blur这些比较麻烦, 做过Android的都知道这个…

DAY1,Qt [ 手动实现登录框(信息调试类,按钮类,行编辑器类,标签类的使用)]

1.手动实现登录框&#xff1b; ---mychat.h---头文件 #ifndef MYCHAT_H #define MYCHAT_H#include <QWidget> #include <QDebug> //打印信息 #include <QIcon> //图标 #include <QPushButton> //按钮 #include <QLineEdit> //行编辑器类 #in…

ERC20 allowance,approve 和 transferFrom

allowance&#xff0c;approve 和 transferFrom&#xff0c;这几个函数提供了一些高级功能&#xff0c;用于授权其他以太坊地址的所有者(spender)代表你使用你的token。这个“其他以太坊地址”可能是一个智能合约&#xff0c;也可能只是一个普通token账户。 ● approve函数。T…

output delay 约束

output delay 约束 一、output delay约束概述二、output delay约束系统同步三、output delay约束源同步 一、output delay约束概述 特别注意&#xff1a;在源同步接口中&#xff0c;定义接口约束之前&#xff0c;需要用create_generated_clock 先定义送出的随路时钟。 二、out…

labview 多线程同步

所谓通讯的同步是指多个线程同时进行或严格按照顺序执行&#xff0c;数据的严格性是指发送多少数据接收多少数据&#xff0c;不能出现数据丢失或重复接收的现象。 labview的同步机制有事件发生、集合点、通知器、信号量。 可以这么来记忆&#xff1a;事急&#xff08;集&…

kotlin高阶函数

kotlin高阶函数 函数式API:一个函数的入参数为Lambda表达式的函数就是函数式api 例子: public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {return filterTo(ArrayList<T>(), predicate) }上面这段函数: 首先这个函…

Nginx配置server_name讲解

文章目录 1.Nginx配置中没有server_name会怎样&#xff1f;2.Nginx配置server_name的匹配规则3.正则表达式规则 1.Nginx配置中没有server_name会怎样&#xff1f; 此时Nginx会自动设置成 server_name ""; 它不会匹配任何域名&#xff0c;导致Nginx会优先将HTTP请求交…

2023年7月第3周大模型荟萃

2023年7月第3周大模型荟萃 2023.7.25版权声明&#xff1a;本文为博主chszs的原创文章&#xff0c;未经博主允许不得转载。 1、华为发布大模型时代 AI 存储新品 7 月 14 日华为在深圳发布了大模型时代 AI 存储新品&#xff0c;为基础模型训练、行业模型训练&#xff0c;细分场…