简体   繁体   English

我究竟做错了什么? 使用JS和CSS在HTML中为StreamLabs / Twitch警报创建进度栏

[英]What am I doing wrong? Creating a Progress bar in HTML using JS and CSS for StreamLabs/Twitch Alerts

Let me start by saying thank you in advance! 首先让我先说谢谢!

I'm on this site called StreamLabs for twitch streamers and they've introduced a way to use custom HTML, CSS, & JS to customize your look. 我在一个名为StreamLabs的网站上,用于抽搐彩带,他们介绍了一种使用自定义HTML,CSS和JS定制外观的方法。

They have a thing called "Stream boss" that reacts to users following, sub, and/or donating. 他们有一个叫做“ Stream boss”的东西,可以对用户的关注,订阅和/或捐赠做出反应。

In comes this progress bar. 进入此进度栏。 Here's the code: 这是代码:

HTML: HTML:

      <!-- All html objects will be wrapped in the #wrap div -->
      <div class='boss_cont'>
        <div id='username'></div>
        <div class='user_pic_cont'>
          <img id='user_pic' src=''\>
        </div>
        <div id='user_hp_cont'>
          <span id='current_health'>0</span>/<span id='total_health'>0</span>
        </div>
        <div id='message'>
        </div>
      </div>

CSS: "kinda irrelevant at this moment" CSS:“暂时与此无关”

 /* All html objects will be wrapped in the #wrap div */
      .boss_cont {
          color:white;
          background:black;
      }

JS: "the important thing here: all values are predetermined with twitch and streamlabs api" JS:“这里很重要:所有值都是通过twitch和streamlabs api预先确定的”

          // Events will be sent when the boss is damaged or killed.
          // Please use event listeners to run functions.
          document.addEventListener('bossLoad', function(obj) {
          // obj.detail will contain information about the current boss
          // this will fire only once when the widget loads
          console.log(obj.detail);
          $('#user_pic').attr('src', obj.detail.boss_img);
          $('#current_health').text(obj.detail.current_health);
          $('#total_health').text(obj.detail.total_health);
          $('#username').text(obj.detail.boss_name);
          });
          document.addEventListener('bossDamaged', function(obj) {
          // obj.detail will contain information about the boss and a
          // custom message
          console.log(obj.detail);
          $('#current_health').text(obj.detail.boss.current_health);
          });
          // Similarly for for when a boss is killed
          document.addEventListener('bossKilled', function(obj) {
          console.log(obj.detail);
          $('#username').text(obj.detail.boss.boss_name);
          $('#user_pic').attr('src', obj.detail.boss.boss_img);
          $('#current_health').text(obj.detail.boss.current_health);
          $('#total_health').text(obj.detail.boss.total_health);
      });

Now, let's get to what I'm trying to do. 现在,让我们开始尝试做的事情。

This basic code DOES NOT have a progress bar at all, but you have the values and tools to create one. 这个基本代码根本没有进度条,但是您拥有创建代码的价值和工具。

What I'm trying to do is create a progress bar that uses the values of the "current_health and total_health" 我想做的是创建一个进度条,该进度条使用“ current_health和total_health”的值

Also, i don't know how to set the max value of an html progress bar in JS. 另外,我不知道如何在JS中设置html进度条的最大值。

My code: 我的代码:

HTML: NOTE: The current HP of the boss is 295/300. HTML: 注意:老板的当前HP是295/300。 Predetermined. 预定的。

<!-- All html objects will be wrapped in the #wrap div -->
      <div class='boss_cont'>
        <div id='username'></div>
        <div class='user_pic_cont'>
          <img id='user_pic' src=''>
        </div>
        <div id='user_hp_cont'>
          <span id='current_health'>0</span>/<span id='total_health'>0</span>
          <progress id='health' value="100" max="100"></progress>
        </div>
        <div id='message'>
        </div>
      </div>

JS: JS:

// Events will be sent when the boss is damaged or killed.
          // Please use event listeners to run functions.
          document.addEventListener('bossLoad', function(obj) {
          // obj.detail will contain information about the current boss
          // this will fire only once when the widget loads
          console.log(obj.detail);
          $('#user_pic').attr('src', obj.detail.boss_img);
          $('#current_health').text(obj.detail.current_health);
          $('#total_health').text(obj.detail.total_health);
          $('#username').text(obj.detail.boss_name);
          });
          document.addEventListener('bossDamaged', function(obj) {
          // obj.detail will contain information about the boss and a
          // custom message
          console.log(obj.detail);
          $('#current_health').text(obj.detail.boss.current_health);
          $('#user_hp_cont progress').val(obj.detail.boss.current_health);
          });
          // Similarly for for when a boss is killed
          document.addEventListener('bossKilled', function(obj) {
          console.log(obj.detail);
          $('#username').text(obj.detail.boss.boss_name);
          $('#user_pic').attr('src', obj.detail.boss.boss_img);
          $('#current_health').text(obj.detail.boss.current_health);
          $('#total_health').text(obj.detail.boss.total_health);
      });

I've also tried setting 我也尝试过设定

// Sets a percentage value as a whole integer from 0 - 100
var health = val.(obj.detail.boss.current_health/obj.detail.total_health) * 100;
$('.progress').val(health);

after the boss is damaged. 老板损坏后。

All of this can be tested with activating test donations, follows, and/or subs. 所有这些都可以通过激活测试捐赠,后续和/或订阅进行测试。

Another question is quick yes or no. 另一个问题是快速的是或否。 Is there a way to set that percentage value into a css style for the width? 有没有一种方法可以将该百分比值设置为CSS样式的宽度?

Hopefully I understand the first question here. 希望我理解这里的第一个问题。

The max value for your progress bar should be always be 100. Then you use that CurrentHealth/TotalHealth*100 to determine how much of that bar to fill in. You'll set your width of the filled in portion to CurrentHealth/TotalHealth*100 as a percentage and that will give you a progress bar. 进度栏的最大值应始终为100。然后使用CurrentHealth / TotalHealth * 100确定要填充的进度条的多少。将填充部分的宽度设置为CurrentHealth / TotalHealth * 100百分比,这将为您提供进度条。 Ill make a fiddle. 生病了。

var totalHealth = 300;
var currentHealth = 295;
var healthProgress = currentHealth/totalHealth*100;

Now we need to set healthProgress as a width percentage on our element. 现在,我们需要将healthProgress设置为元素上的宽度百分比。

Here's a prototype https://jsfiddle.net/2Lsvxegs/5/ 这是一个原型https://jsfiddle.net/2Lsvxegs/5/

Sorry I didn't directly use your html/js, thought it would be a little clearer like this. 抱歉,我没有直接使用您的html / js,以为这样会更清晰一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM