[英]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.