javascript - 尝试按百分比增加进度条

标签 javascript jquery html css algorithm

我正在制作一个类似于 jackbox.tv 的问答游戏,我正在尝试找出我需要的逻辑,以便在玩家正确回答问题时按百分比增加进度条(我想说 +10每个正确回答的问题的百分比)。我发布了一些示例逻辑,但我认为它既不实用也不是编写它的最佳方式。

<script>
  $(document).ready(function () {
    users= [{}];

    // This is the example logic for the animation:

    for(let i = 0; i < users.length; i++) {
      let wid = (users[i].score)*1%
      $('.player1').animate({ width: wid }, 2000)
    }

    $('.player1').animate({ width: '90%' }, 2000);
    $('.player2').animate({ width: '75%' }, 2000);
    $('.player3').animate({ width: '50%' }, 2000);
    $('.player4').animate({ width: '70%' }, 2000);
    $('.player5').animate({ width: '45%' }, 2000);
  });
</script>

最佳答案

您正在寻找这样的东西吗?

enter image description here

$(document).ready(function () {
  
  var players = [
    {
      "id": 1,
      "score": 0.2
    },
    {
      "id": 2,
      "score": 0.5
    }
  ];
  
  for(var index in players){
    var id = players[index].id;
    var score = players[index].score;
    
    console.log(id);
    var selector = ".bar.player" + id;
    var bar = $(selector);
    bar.css("width", 100 * score + "px");
    
  }
}); 
.container {
  display: block;
  margin-top: 40px;
  clear: both;
  
  height: 20px;
  width: 100px;
  background-color: white;
  border: 1px black solid;
}

.bar {
  height: 18px;
  width: 10px;
  background-color: green;
  margin-top: 1px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="container">
  <div class="bar player1">&nbsp;<div>
<div>

<div class="container">
  <div class="bar player2">&nbsp;<div>
<div>

关于javascript - 尝试按百分比增加进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55818248/

相关文章:

javascript - 创建一个JS购物车。请告知添加到购物车

jquery - 如何在弹出窗口或工具提示中显示自定义 HTML

javascript - Ajax 自动刷新 - 表格不刷新

javascript - 当有超过 1 个时,如何从表中的下拉列表中获取选定值?

javascript - 如何将默认内容添加到div

html - 如何在 CSS 中实现这样的标题图片?

html - Css 图像根据高度调整大小

javascript - 当另一个 div 到达页面顶部时简单的 CSS 更改

javascript - 函数未定义 - jQuery/Javascript

php - Laravel 4:Input::all() 使用 $.ajax POST 不返回任何数据