为什么我的进度条显示浮点值?
本例中的“value”为 5,“maxValue”为 39。进度条上显示的值 (a wijmo 进度条)是 5.07。 {0} 应告诉进度条显示进度值而不是百分比。如果我将值更改为 8,保持最大值为 39,则显示的值将变为 8.19。我有点困惑为什么我看到的是浮点数而不是整数。我在进度条代码中看不到任何内容,除了 self.value() 以外的任何内容,它以整数形式记录到控制台。那么什么可能导致这种行为呢?
$("#proBarAdherence").wijprogressbar({
value: Gymloop.completedCount,
animationOptions: {
duration: 1000
},
maxValue: Gymloop.targetSessions,
indicatorImage: '/images/progbar_red.png',
labelAlign: 'running',
labelFormatString: '{0} sessions completed',
beforeProgressChanging: function(e,data) {
console.log('beforeprogresschanging',data);
},
progressChanging: function (e,data) {
console.log('progresschanging',data);
},
progressChanged : function (e,data) {
console.log('progresschanged',data);
}
});
beforeprogresschanging Object { oldValue="5", newValue=5}
progresschanging Object { oldValue="5", newValue=0}
progresschanging Object { oldValue="0", newValue=1.17}
progresschanging Object { oldValue="1.17", newValue=1.56}
progresschanging Object { oldValue="1.56", newValue=1.95}
progresschanging Object { oldValue="1.95", newValue=2.34}
progresschanging Object { oldValue="2.34", newValue=2.73}
progresschanging Object { oldValue="2.73", newValue=3.12}
progresschanging Object { oldValue="3.12", newValue=3.51}
progresschanging Object { oldValue="3.51", newValue=3.9}
progresschanging Object { oldValue="3.9", newValue=4.29}
progresschanging Object { oldValue="4.29", newValue=4.68}
progresschanging Object { oldValue="4.68", newValue=5.07}
progresschanged Object { oldValue="5", newValue=5}
更新
我可以在 ProgressChanging 事件期间看到该值,但不确定如何在动画完成后使标签显示 5 而不是 5.07? 这里有一个实例
'value' in this example is 5, 'maxValue' is 39. The value displayed on the progress bar (a wijmo progressbar) is 5.07. {0} should tell the progressbar to show a progress value rather than a percentage. If I change the value to 8, maintaining the max value as 39, the value displayed becomes 8.19. I'm a bit confused as to why I am seeing floating point numbers and not integers. I can't see anything in the progressbar code which would display anything other than self.value() which logs to the console as an integer. So what might be causing this behaviour?
$("#proBarAdherence").wijprogressbar({
value: Gymloop.completedCount,
animationOptions: {
duration: 1000
},
maxValue: Gymloop.targetSessions,
indicatorImage: '/images/progbar_red.png',
labelAlign: 'running',
labelFormatString: '{0} sessions completed',
beforeProgressChanging: function(e,data) {
console.log('beforeprogresschanging',data);
},
progressChanging: function (e,data) {
console.log('progresschanging',data);
},
progressChanged : function (e,data) {
console.log('progresschanged',data);
}
});
beforeprogresschanging Object { oldValue="5", newValue=5}
progresschanging Object { oldValue="5", newValue=0}
progresschanging Object { oldValue="0", newValue=1.17}
progresschanging Object { oldValue="1.17", newValue=1.56}
progresschanging Object { oldValue="1.56", newValue=1.95}
progresschanging Object { oldValue="1.95", newValue=2.34}
progresschanging Object { oldValue="2.34", newValue=2.73}
progresschanging Object { oldValue="2.73", newValue=3.12}
progresschanging Object { oldValue="3.12", newValue=3.51}
progresschanging Object { oldValue="3.51", newValue=3.9}
progresschanging Object { oldValue="3.9", newValue=4.29}
progresschanging Object { oldValue="4.29", newValue=4.68}
progresschanging Object { oldValue="4.68", newValue=5.07}
progresschanged Object { oldValue="5", newValue=5}
Update
I can see the value's during the progressChanging event but am not sure how to make the label display 5 instead of 5.07 once the the animation is complete? There is a live example here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想显示整数,你需要四舍五入 JavaScript 对所有内容都使用浮点数。
如何转换浮点数JavaScript 中的整数?
--- 对更新的响应 ---
我对那个确切的进度条不是特别熟悉,但它们都很相似。
发生的情况是,您在变量初始化时调用舍入一次,而不是在变量更新时调用舍入一次。
有一个与正在更新的加载栏关联的事件,该事件是您想要对已完成的舍入进行舍入的位置在传递到进度条之前进行计数。
我不确定我是否很好地解释了发生的事情
value = Math.round(myobj.completedCount), ---update event--> CompletedCount = newValue
---更新事件---> CompletedCount = newValue
您需要做的是捕获该更新事件并将变量舍入那里。
---更新事件--> CompletedCount = round(newValue)
------ 试试这个-----
问题线是这个
percent = (value - self.min) / (self.max - self.min) * 100;
将 wijmo 栏源代码中的第 328 行替换为
percent = Math.round((value - self.min) / (self.max - self.min)) * 100;
< br> 它应该按照您想要的方式运行。You need to round if you want to display integers JavaScript uses floats for everything.
How do I convert a float number to a whole number in JavaScript?
--- Response to the update ---
I am not particularly familiar with that exact progress bar but they are all similar.
Whats happening is you are calling round once when the variable is initialized and not when the variable is updated.
There is an event associated with the loading bar being updated that is where you want to round the completed Count before it is passed to the progress bar.
I am not sure if I am doing a good job explaining whats happening
value = Math.round(myobj.completedCount), ---update event--> completedCount = newValue
---update event---> completedCount = newValue
what you need to do is catch that update event and round the variable there.
---update event--> completedCount = round(newValue)
------ TRY THIS-------
The trouble line is this
percent = (value - self.min) / (self.max - self.min) * 100;
replace line 328 from the source code of the wijmo bar with
percent = Math.round((value - self.min) / (self.max - self.min)) * 100;
and it should act the way you want it to.
我最终选择了这个(编辑进度条js文件 - 恶心):
我还记录了 wijmo 开发者的 bug。
无需编辑源代码的另一个选项是:
I went with this in the end (editing the progressbar js file - yuck):
I also logged a bug with the wijmo developers.
Another option without editing the source is :