简体   繁体   English

javascript 数学和 css 百分比

[英]javascript Math and css percentage

I have a progress div container and an xhr request posting base64 images to the server.我有一个进度 div 容器和一个将 base64 图像发布到服务器的 xhr 请求。

The progress indicator div looks as follows:进度指示器 div 如下所示:

<style>
.progresscover {
 background: #eee;
 width:100%;
 height: 3px;
 position: relative;
 text-align: left;
 }
 .progress{
 width:0%;
 height:100%;
 background: purple;
 position:absolute:
 left:0px;
 top:0px;
 bottom:0px;
  }
</style>
<div class="progresscover"><div class="progress"></div></div>

from the XHR request i get following values loaded: 102533, total: 703227 The loaded obviously changes till its equal to the total..从 XHR 请求中我得到以下加载值:102533,总计:703227 加载明显变化直到它等于总数..

what is the math to increase the width of the "progress" bar incrementally in percentages?以百分比递增地增加“进度”栏宽度的数学是什么?

its like this -- parseInt( (loaded / total * 100), 10) + '%');就像这样—— parseInt( (loaded / total * 100), 10) + '%');

Are you trying to achieve something like this?你想实现这样的目标吗?

 function percentage(num1,num2){ return (num1/num2) * 100 } get = () =>{ let value1 = document.getElementById('first').value; let value2 = document.getElementById('second').value; document.getElementById('res').innerHTML = `<h2>the percentage is:` + percentage(value1,value2) + `% </h2>`; document.getElementById('file').value = percentage(value1,value2) }
 .a{ margin-bottom: 30px; padding: 20px; }.one{ margin-bottom: 20px; }.two{ margin-bottom: 20px; }.res{ margin-bottom: 20px; } button { margin-bottom: 30px; background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 10px; margin: 4px 2px; cursor: pointer; }
 <div> <div class="a"> <div class="one">first Number:<input type="number" id="first"></div> <div class="two">second number:<input type="number" id="second"></div> <button onclick="get()">Get Percentage</button> </div> <div id="res"></div> <label for="file">Percentage:</label> <progress id="file" max="100" value=""> </progress> </div>

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

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