简体   繁体   English

如何制作以 px 为宽度的 ProgressBar?

[英]How can I make a ProgressBar with px as width?

I am having a hard time thinking about the process of a Progress Bar that the px width is the current value and the max width is 100%.我很难考虑 px 宽度是当前值且最大宽度为 100% 的进度条的过程。

My increment value should change and not fixed to 1. like for example the current width on the UI was 1000px and I want to increment until I got 1000px then I resize and the progress bar max width change to 117px.我的增量值应该改变而不是固定为 1。例如 UI 上的当前宽度是 1000 像素,我想增加直到我得到 1000 像素,然后我调整大小并将进度条最大宽度更改为 117 像素。 then the increment value should change to.002 something like that.然后增量值应该更改为.002 类似的东西。

 <div class="progress-line-body" style="width: 1309.8px;"> <div class="progress-value" id="sampleid" style="width: 50px;"> </div> </div>

 <script> var $sample = $('#sampleid'); var incrementVal = 1; var value = currentWidth //progress-line-body + incrementVal; $sample.css("width", increment + "px"); </script>

You really should use percentages to keep it responsive.你真的应该使用百分比来保持响应。 a 1px incrementing will be unresponsive as hell. 1px 的增量将无法响应。 Also, use the HTML progressbar:此外,使用 HTML 进度条:

 window.addEventListener('DOMContentLoaded', function() { let x = 0.1; setInterval(function() { document.querySelector('#progress-bar').value = (x >= 100)? 100: x; x += 0.1; }, 25); })
 <label for="progress-bar">Progressbar:</label> <progress id="progress-bar" max="100" value="0"></progress>

I think you would be better off using percentages also for the progress value.我认为您最好将百分比也用于进度值。

I have created an example for you.我为您创建了一个示例。 You could easily adapt it if you really want to use pixels instead of percentages如果您真的想使用像素而不是百分比,您可以轻松调整它

 const progressValueEl = document.querySelector(".progress-value"); const startingPercentage = 0; const maxPercentage = 100; let currentPercentage = startingPercentage; const updateValueBar = () => { progressValueEl.style.width = `${currentPercentage}%`; } setInterval(() => { if (currentPercentage < maxPercentage) { currentPercentage++; updateValueBar(); } }, 100);
 .progress-line-body { height: 20px; width: 400px; background-color: lightgreen; position: relative; }.progress-value { position: absolute; top: 0; left: 0; height: 100%; background-color: purple; }
 <div class="progress-line-body"> <div class="progress-value"></div> </div>

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

相关问题 如何制作可滚动div内固定有940px宽度的框? - How can i make a box with 940px width fixed inside a scrollable div? 如何在PixiJS中创建进度条? - How I can make a progressbar in PixiJS? 如何使目标宽度和高度大于640像素的高质量PhoneGap摄像机图像? - How can I make PhoneGap Camera images with target width and height bigger than 640px of high quality? 如何使此JavaScript仅适用于最小宽度为1036px的浏览器上的用户? - How do I make this JavaScript only work for users on browsers with a min-width of 1036px? 如何计算行中三个元素的每一个可以有300px宽度? - How can I get calculated of each of three elements in row can have 300px width? 如何增加进度条的宽度 - How to increase the width of a progressbar 如何在1200px断点处停止自举面板的全宽? - how can I stop bootstrap panels going full width at the 1200px breakpoint? 如何在包含Bootstrap的情况下将文本输入的宽度设置为0px? - How can I set a text input's width to 0px with Bootstrap included? 如何在最大宽度 768px 上禁用 jquery 功能? - How can i disable jquery function on max-width 768px? 如何使用jQuery在同一个父元素内动态将“ left” px应用于div的宽度 - How can I dynamically apply the “left” px to a div's width, within the same parent element, using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM