使用个人资料图片创建动态排行榜
Creating a dynamic LeaderBoard with profile pictures
我想找到一种创建 排行榜 的方法,如下所示,它使用 变量 来保存 用户积分 在 Javascript 上以响应用户排名变化时对这些点数的变化...
这是我要实现的目标:
我只想手动填写用户点数数据仅使用Javascript变量...所有数据都会来自 javascript 数组。
类似于:
user_1 = Nilesh S;
user_2 = Shristi_S;
user_1 points = 1710;
user_2 points = 1710;
etc...
显然,如果我将 Nilesh S (user_1) 点数更改为 1000,那么 Nilesh S 的排名将是第 10 位...
我现在所取得的成就只是创建了那些圈子头像:)
代码如下:
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);
}
任何解决方案将不胜感激。
这是一种在对象数组中创建一些虚拟数据、对其进行排序并将其添加到页面的快速而肮脏的方法。
// 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>
我想找到一种创建 排行榜 的方法,如下所示,它使用 变量 来保存 用户积分 在 Javascript 上以响应用户排名变化时对这些点数的变化...
这是我要实现的目标:
我只想手动填写用户点数数据仅使用Javascript变量...所有数据都会来自 javascript 数组。
类似于:
user_1 = Nilesh S;
user_2 = Shristi_S;
user_1 points = 1710;
user_2 points = 1710;
etc...
显然,如果我将 Nilesh S (user_1) 点数更改为 1000,那么 Nilesh S 的排名将是第 10 位...
我现在所取得的成就只是创建了那些圈子头像:)
代码如下:
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);
}
任何解决方案将不胜感激。
这是一种在对象数组中创建一些虚拟数据、对其进行排序并将其添加到页面的快速而肮脏的方法。
// 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>