<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.father {
width: 300px;
height: 20px;
background: #784C2D;
border-radius: 15px;
margin: 100px auto;
padding: 5px;
}
#Son {
width: 100%;
height: 100%;
background: #F2CE28;
border-radius: 10px;
}
</style>
<body>
<div class="father">
<div id="Son"></div>
</div>
</body>
</html>
<script>
var Son = document.getElementById("Son")
Son.style.width = Percentage(40, 100) + "%"
function Percentage(current, total) {
// 数量/总数量*100 公式
return current / total * 100
}
</script>
效果