我很想找到一种创建排行榜的方法,如下所示,该方法使用变量来按顺序保留Javascript上的用户点数用不断变化的用户排名来响应这些点的变化...
这是我要实现的目标:
我只想手动仅使用Javascript变量填写用户的点数据 ...假设所有数据都来自包含这些数据的JavaScript数组。 / p>
类似:
user_1 = Nilesh S;
user_2 = Shristi_S;
user_1 points = 1710;
user_2 points = 1710;
etc...
很明显,如果我将Nilesh S(user_1)的点数更改为1000,那么Nilesh S的排名将是第十...
我现在所要做的只是创建那些圆形的个人资料图片:)
以下是代码:
Javascript:
var img = document.createElement("IMG");
img.setAttribute("src", "img_pulpit.jpg");
img.setAttribute("width", "300");
img.setAttribute("height", "300");
img.setAttribute("alt", "The Pulpit Rock");
document.body.appendChild(img);
HTML:
<div id="IMG">
<script src="Script.js"></script>
<link rel="stylesheet" type="text/css" href="Style.css">
[1]: https://i.stack.imgur.com/zXm4N.png
CSS:
img {
background: #2f293d;
border: 1px solid #2f293d;
padding: 6px;
border-radius: 50%;
box-shadow: .6rem .5rem 1rem rgba(0, 0, 0, .5);
}
任何解决方案将不胜感激。
答案 0 :(得分:0)
这是在对象数组中创建一些虚拟数据,对其进行排序并将其添加到页面的一种快速而肮脏的方法。
// this is the array that will hold all the profile objects
let profiles = [];
let profile1 = {};
profile1.name = "Jim Bob";
profile1.points = 1500;
profiles.push(profile1);
let profile2 = {};
profile2.name = "Jane Smith";
profile2.points = 1600;
profiles.push(profile2);
let profile3 = {};
profile3.name = "Mike Jones";
profile3.points = 400;
profiles.push(profile3);
let profile4 = {};
profile4.name = "Sally Peterson";
profile4.points = 1900;
profiles.push(profile4);
// sort the array by points
// b - a will make highest first, swap them so a - b to make lowest first
profiles.sort(function(a, b) {
return b.points-a.points;
})
let profilesDiv = document.getElementsByClassName('profiles')[0];
profiles.forEach(function(entry) {
let profile = document.createElement('div');
profile.className = "profile";
profile.textContent = entry.name + " -- " + entry.points;
profilesDiv.appendChild(profile);
});
.profile {
border: 2px solid #222222;
padding: 5px;
margin: 5px;
width: 50%;
}
<div class="profiles">
</div>