要使用JavaScript在10秒内使进度条从0加载到100%,您可以使用setTimeout
或setInterval
函数来逐步更新进度条的宽度。以下是使用setTimeout
实现的一个示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Progress Bar Loading</title>
<style>
#progressBarContainer {
width: 100%;
background-color: #f3f3f3;
}
#progressBar {
width: 0%;
height: 30px;
background-color: #4caf50;
text-align: center;
line-height: 30px;
color: white;
}
</style>
</head>
<body>
<div id="progressBarContainer">
<div id="progressBar">0%</div>
</div>
<script src="script.js"></script>
</body>
</html>
const progressBar = document.getElementById('progressBar');
let width = 0;
function progress() {
if (width < 100) {
width++;
progressBar.style.width = width + '%';
progressBar.textContent = width + '%';
setTimeout(progress, 100); // 每100毫秒更新一次进度
}
}
// 开始加载进度条
setTimeout(progress, 100);
div
元素,并为其设置样式。div
元素,并为其设置初始宽度和样式。div
元素的引用。progress
的函数,该函数会逐渐增加进度条的宽度,并更新其显示的百分比。setTimeout
函数每100毫秒调用一次progress
函数,直到进度达到100%。setTimeout
函数在页面加载后立即开始进度条的加载过程。这样,进度条将在10秒内从0%加载到100%。您可以根据需要调整setTimeout
的延迟时间来改变加载速度。
领取专属 10元无门槛券
手把手带您无忧上云