【发布时间】:2020-04-06 12:55:19
【问题描述】:
如标题所述,如何为以下项目添加计数动画: 检测呈阳性 人口 死亡人数 检测呈阳性的人的死亡百分比 最后更新日期
这些数字是动态的而不是静态的,这就是我迷路的地方。
感谢您的帮助。抱歉,我不熟悉使用 Javascript、Json 和 API 进行编码。
window.addEventListener("load", function() {
document.getElementById("countrySel").addEventListener("change", getCovidStats);
document.getElementById("countrySel").value = "169";
getCovidStats()
})
function getCovidStats() {
const cc = document.getElementById("countrySel").value;
if (cc === "") return;
fetch('https://coronavirus-tracker-api.herokuapp.com/v2/locations/' + cc)
.then(function(resp) {
return resp.json()
})
.then(function(data) {
let population = data.location.country_population;
let update = data.location.last_updated;
let confirmedCases = data.location.latest.confirmed;
let deaths = data.location.latest.deaths;
document.getElementById('inwoners').innerHTML = population.toLocaleString('en');
document.getElementById('update').innerHTML = update.substr(0, 10);
document.getElementById('patienten').innerHTML = confirmedCases.toLocaleString('en');
document.getElementById('doden').innerHTML = deaths.toLocaleString('en');
document.getElementById('procent').innerHTML = ((Number(deaths) / Number(confirmedCases)) * 100).toLocaleString("en", {
minimumFractionDigits: 2,
maximumFractionDigits: 2
}) + "%";
})
.catch(function() {
console.log("error");
})
setInterval(getCovidStats, 43200000) // update every 12 hours
}
* {
margin: 0;
padding: 0;
}
html {
height: 100%;
width: 100%;
}
h1,
h2 {
font-family: 'Roboto', sans-serif;
font-weight: 300;
text-align: center;
padding-bottom: 20px;
font-size: 250%;
}
.subtitle,
.over {
padding: 20px;
font-size: 150%;
}
body {
background-color: #FFDC56;
}
div {
padding: 20px;
}
/* Add a black background color to the top navigation */
.topnav {
background-color: #005A9C;
overflow: hidden;
font-family: 'Roboto', sans-serif;
font-size: 75%;
}
.logo {
float: left;
}
/* Style the links inside the navigation bar */
.topnav a {
float: right;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
/* Change the color of links on hover */
.topnav a:hover {
background-color: #FFDC56;
color: black;
}
/* Add a color to the active/current link */
.topnav a.active {
background-color: #4CAF50;
color: white;
}
.stats-container {
text-align: center;
float: right;
display: inline-block;
}
.location-container {
display: inline-block;
}
.data-container {
border: 2px solid #005A9C;
margin-right: 30%;
margin-left: 30%;
}
h4,
{
font-size: 85%;
color: gray;
font-family: 'Roboto', sans-serif;
font-weight: 300;
text-align: center;
padding-top: 20px;
padding-left: 20px;
padding-right: 20px;
padding-bottom: 5px;
}
.over {
font-family: 'Roboto', sans-serif;
font-size: 100%;
text-align: center;
}
.footer {
font-family: 'Roboto', sans-serif;
bottom: 0;
font-size: 75%;
padding: 5px;
}
.maatregelen {
width: 35%;
display: block;
margin-left: auto;
margin-right: auto;
text-align: center;
}
.maatregelen-caption {
text-align: center;
font-family: 'Roboto', sans-serif;
font-size: 80%;
}
<!DOCTYPE html>
<html>
<head>
<title>Coronavirus Statistieken</title>
<meta charset="UTF-8">
<link rel="shortcut icon" href="masker-emoji.png">
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="styles.css">
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript" src="loader.js"></script>
</head>
<body>
<div class="topnav">
<h1 class="logo">Coronavirus</h1>
<a href="over.html">about</a>
<a class="active" href="index.html">stats</a>
</div>
<h2 class="subtitle">Title</h2>
<div class="data-container">
<div class="stats-container">
<h4>Tested positive</h4>
<h1 id="patienten"></h1>
<h4>Deaths</h4>
<h1 id="doden"></h1>
<h4>Percentage of deaths of positive tested people</h4>
<h1 id="procent"></h1>
</div>
<div class="location-container">
<h4>country</h4>
<h1 id="country"><label for="countrySel">Country:</label>
<select id="countrySel">
<option value="169">???????? </option>
<option value="120">???????? </option>
<option value="116">???????? </option>
<option value="201">???????? </option>
<option value="137">???????? </option>
<option value="187">???????? </option>
<option value="143">???????? </option>
<option value="225">???????? </option>
</select>
</h1>
<h4>population</h4>
<h1 id="inwoners"></h1>
<h4>updated on</h4>
<h1 id="update"></h1>
</div>
</div>
<h1 class="footer">Footer</h1>
</body>
</html>
【问题讨论】:
-
为此有现成的库,例如countUp.js。此外,您每次调用该函数时都会设置一个新的时间间隔。间隔会堆积起来。你应该只使用
setTimeout()
标签: javascript html json animation css-animations