day28-Github Profiles(获取Github用户概要)

这篇具有很好参考价值的文章主要介绍了day28-Github Profiles(获取Github用户概要)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

day28-Github Profiles(获取Github用户概要)

效果

day28-Github Profiles(获取Github用户概要),50天50个小demo前端,github,html5,css3,javascript,前端文章来源地址https://www.toymoban.com/news/detail-601438.html

index.html

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

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

<body>
    <!-- 搜索框 -->
    <form class="user-form" id="form">
        <input type="text" id="search" placeholder="搜索Github用户-输入用户名">
    </form>
    <!-- 展示用户页面 -->
    <main id="main"></main>

    <!-- 引入了axios库,使你可以在JavaScript代码中使用axios来进行HTTP请求,而integrity和crossorigin属性则是为了增强安全性和隐私保护。 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.0/axios.min.js"
        integrity="sha512-DZqqY3PiOvTP9HkjIWgjO6ouCbq+dxqWoJZ/Q+zPYNHmlnI2dQnbJ5bxAHpAMw+LXRm4D72EIRXzvcHQtE8/VQ=="
        crossorigin="anonymous"></script>
    <script src="script.js"></script>
</body>

</html>

style.css

/* 引入字体 */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;400&display=swap');

* {
    box-sizing: border-box;
}

body {
    background: url('https://source.unsplash.com/random') no-repeat center/cover;
    /* color: #fff; */
    font-family: 'Poppins', sans-serif;
    /* 竖直排列 */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    overflow: hidden;
    margin: 0;
}

/* 搜索表单 */
.user-form {
    width: 100%;
    max-width: 700px;
}

/* 搜索框 */
.user-form input {
    width: 100%;
    display: block;
    background-color: #fff;
    border: none;
    border-radius: 10px;
    /* color: #fff; */
    padding: 1rem;
    margin-bottom: 2rem;
    font-family: inherit;
    font-size: 1rem;
    box-shadow: 0 5px 10px rgba(154, 160, 185, 0.05),
        0 15px 40px rgba(0, 0, 0, 0.1);
    /* 去除点击时搜索框的效果 */
    outline: none;
}

/* 搜索框内的字体 */
.user-form input::placeholder {
    color: #bbb;
}

/* 用户卡片 */
.card {
    max-width: 800px;
    background-color: #bbb;
    border-radius: 20px;
    box-shadow: 0 5px 10px rgba(154, 160, 185, 0.05),
        0 15px 40px rgba(0, 0, 0, 0.1);
    display: flex;
    padding: 3rem;
    margin: 0 1.5rem;
}

/* 用户头像 */
.avatar {
    border-radius: 50%;
    border: 10px solid #2a2a72;
    height: 150px;
    width: 150px;
}

/* 用户信息 */
.user-info {
    color: #eee;
    margin-left: 2rem;
}

.user-info h2 {
    margin-top: 0;
}

.user-info ul {
    list-style-type: none;
    display: flex;
    justify-content: space-between;
    padding: 0;
    max-width: 400px;
}

.user-info ul li {
    display: flex;
    align-items: center;
}

.user-info ul li strong {
    font-size: 0.9rem;
    margin-left: 0.5rem;
}

.repo {
    text-decoration: none;
    color: #fff;
    background-color: #729cdf;
    font-size: 0.7rem;
    padding: 0.25rem 0.5rem;
    margin-right: 0.5rem;
    margin-bottom: 0.5rem;
    display: inline-block;
}

@media (max-width: 500px) {
    .card {
        flex-direction: column;
        align-items: center;
    }

    .user-form {
        max-width: 400px;
    }
}

script.js

// 重点 flex  background  async/await
// 1.获取元素节点
const APIURL = 'https://api.github.com/users/'   //github用户信息apiurl

const main = document.getElementById('main')//放置卡片的主体
const form = document.getElementById('form')//搜索表单
const search = document.getElementById('search')//搜索框
// 异步 语法糖获取用户信息
async function getUser(username) {
    try {
        // 获取数据
        const { data } = await axios(APIURL + username)
        // 创建用户卡片
        createUserCard(data)
        // 获取用户的Repos信息
        getRepos(username)
    } catch (err) {
        if (err.response.status == 404) {
            createErrorCard('没有此用户名的信息文件')
        }
    }
}
// 异步 语法糖获取用户的Repos信息
async function getRepos(username) {
    try {
        const { data } = await axios(APIURL + username + '/repos?sort=created')
        // 创建用户卡片的Repos
        addReposToCard(data)
    } catch (err) {
        createErrorCard('获取Repos有问题')
    }
}
// 创建用户卡片
function createUserCard(user) {
    const cardHTML = `
    <div class="card">
    <div>
      <img src="${user.avatar_url}" alt="${user.name}" class="avatar">
    </div>
    <div class="user-info">
      <h2>${user.name}</h2>
      <p>${user.bio}</p>
      <ul>
        <li>${user.followers} <strong>Followers</strong></li>
        <li>${user.following} <strong>Following</strong></li>
        <li>${user.public_repos} <strong>Repos</strong></li>
      </ul>

      <div id="repos"></div>
    </div>
  </div>
    `
    main.innerHTML = cardHTML

}

// 错误信息
function createErrorCard(msg) {
    const cardHTML = `
        <div class="card">
            <h1>${msg}</h1>
        </div>
    `

    main.innerHTML = cardHTML
}
// 添加Repos
function addReposToCard(repos) {
    const reposEl = document.getElementById('repos')
    // console.log(repos);// [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
    repos
        .slice(0, 5)
        .forEach(repo => {
            const repoEl = document.createElement('a')
            repoEl.classList.add('repo')
            repoEl.href = repo.html_url
            repoEl.target = '_blank'
            repoEl.innerText = repo.name

            reposEl.appendChild(repoEl)
        })
}

// 监听事件 表单提交事件
form.addEventListener('submit', (e) => {
    // 阻止表单提交的默认行为
    // 在大多数情况下,当表单提交时,浏览器会刷新页面并处理表单提交的数据。通过调用e.preventDefault(),阻止了这一默认行为,使得我们可以自行处理表单提交的逻辑,而不刷新页面。
    e.preventDefault()
    // 获取表单值
    const user = search.value
    // 存在发请求,并置空
    if (user) {
        getUser(user)

        search.value = ''
    }
})


到了这里,关于day28-Github Profiles(获取Github用户概要)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 代码随想录Day50

    昨天因为准备面试所以咕咕了一天。今天继续学习动规算法,尽管背包问题已经结束但其中的各类思想仍需要进一步理解。 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两

    2023年04月14日
    浏览(18)
  • 【算法刷题】Day28

    原题链接 第 i 个元素是一支给定的股票在第 i 天的价格 最多可以完成 两笔 交易 注意:你不能同时参与多笔交易 1. 状态表示: dp[i] 表示:第 i 天结束之后,所能获得的最大利润 f[i][j] 表示:第 i 天结束之后,完成了 j 次交易,此时处于“买入”状态下的,最大利润 g[i][j]

    2024年02月02日
    浏览(14)
  • day28 异常

     创建异常类  重载所有构造方法 创建异常类对象  抛出异常

    2024年02月11日
    浏览(15)
  • day28打卡

    8. 复原IP地址 9. 子集 10. 子集II

    2024年01月23日
    浏览(12)
  • day28-JSP

    1.为什么使用JSP 2.B/S和C/S的区别 3.URL 4.Tomcat 5.JSP实战综合项目 1.为什么使用JSP 1.1  JSP定义: (1)是一种动态网页技术 (2)Java Server Pages(Java服务器端页面技术) 1.2  JSP缘由: (1)JSP可以实现交互功能(客户端和服务器端产生请求和响应) (2)可以解决HTML静态页面无法实

    2024年02月12日
    浏览(12)
  • day50-springboot+ajax分页

    分页依赖: dependency     groupIdcom.github.pagehelper/groupId     artifactIdpagehelper-spring-boot-starter/artifactId     version1.0.0/version /dependency 配置: 前后端分离:前段页面中url需填写全路径,同时在controller层添加@CrossOrigin注解 拓展:Date类型需要转换,实体类中添加注解解决

    2024年02月14日
    浏览(17)
  • 面试经典150题——Day28

    11. Container With Most Water You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. N

    2024年02月06日
    浏览(17)
  • 面试题打卡30天-day28

    回答一: 在 Git 中, fork 命令是指将其他用户的代码仓库完全复制一份到当前用户自己的账户下,成为一个新的独立代码仓库。与此相对, clone 命令是指在本地将某个远程代码仓库中的代码克隆到本地,成为本地工作区的一个副本。 具体来说, fork 命令会在远程服务端(如

    2024年02月05日
    浏览(13)
  • Day28- 贪心算法part02

    题目一:122. 买卖股票的最佳时机II 122. 买卖股票的最佳时机 II 给你一个整数数组  prices  ,其中  prices[i]  表示某支股票第  i  天的价格。 在每一天,你可以决定是否购买和/或出售股票。你在任何时候  最多  只能持有  一股  股票。你也可以先购买,然后在  同一天  

    2024年01月15日
    浏览(23)
  • 每日一练 | 华为认证真题练习Day50

    1、SWA和SWB的MAC地址表中,MAC地址、VLAN、端口对应关系正确的有?(多选) 2、PPP帧格式中的Flag字段的取值为? A. 0xFF B. 0x7E C. 0xEF D. 0x8E 3、ICMP报文不包含端口号,所以无法使用NAPT。 A. 对 B. 错 4、如下图所示的网络,要求主机A所在的网络通过Easy IP的方式访问Internet,则在路由

    2024年02月07日
    浏览(14)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包