当前位置:首页 > JavaScript

js实现加载进度条

2026-03-01 07:29:37JavaScript

实现基础进度条

使用HTML和CSS创建一个简单的进度条结构,通过JavaScript动态更新宽度:

<div class="progress-container">
  <div class="progress-bar" id="progressBar"></div>
</div>
.progress-container {
  width: 100%;
  height: 5px;
  background-color: #f0f0f0;
}
.progress-bar {
  height: 100%;
  width: 0%;
  background-color: #4CAF50;
  transition: width 0.3s ease;
}
const progressBar = document.getElementById('progressBar');
let progress = 0;

function updateProgress() {
  progress += 5;
  progressBar.style.width = progress + '%';

  if(progress < 100) {
    setTimeout(updateProgress, 200);
  }
}

updateProgress();

模拟真实加载进度

对于文件加载或资源请求,可以使用XMLHttpRequest的progress事件:

function loadFile(url) {
  const xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);

  xhr.onprogress = function(e) {
    if(e.lengthComputable) {
      const percent = (e.loaded / e.total) * 100;
      progressBar.style.width = percent + '%';
    }
  };

  xhr.onload = function() {
    progressBar.style.width = '100%';
  };

  xhr.send();
}

loadFile('large-file.pdf');

使用Fetch API实现

现代JavaScript可以使用Fetch API配合ReadableStream实现进度跟踪:

async function fetchWithProgress(url) {
  const response = await fetch(url);
  const reader = response.body.getReader();
  const contentLength = +response.headers.get('Content-Length');
  let receivedLength = 0;

  while(true) {
    const {done, value} = await reader.read();
    if(done) break;

    receivedLength += value.length;
    const percent = (receivedLength / contentLength) * 100;
    progressBar.style.width = percent + '%';
  }
}

fetchWithProgress('large-data.json');

平滑动画效果

添加CSS过渡效果使进度变化更平滑:

.progress-bar {
  transition: width 0.5s cubic-bezier(0.4, 0.0, 0.2, 1);
}

完整页面加载进度

监听页面整体加载进度:

document.addEventListener('DOMContentLoaded', () => {
  progressBar.style.width = '50%';
});

window.addEventListener('load', () => {
  progressBar.style.width = '100%';
  setTimeout(() => {
    progressBar.style.opacity = '0';
  }, 500);
});

进度条颜色变化

根据进度百分比改变颜色:

js实现加载进度条

function updateColor(percent) {
  if(percent < 30) {
    progressBar.style.backgroundColor = '#ff5252';
  } else if(percent < 70) {
    progressBar.style.backgroundColor = '#ffab40';
  } else {
    progressBar.style.backgroundColor = '#4CAF50';
  }
}

这些方法可以根据实际需求组合使用,创建出符合项目要求的加载进度指示效果。

标签: 进度条加载
分享给朋友:

相关文章

vue实现懒加载的方法

vue实现懒加载的方法

vue实现懒加载的方法 使用Vue的<img>标签结合IntersectionObserver 在Vue组件中,通过IntersectionObserver监听图片是否进入视口,动态加载…

vue实现水波球进度条

vue实现水波球进度条

实现水波球进度条的方法 使用 SVG 和 CSS 动画 创建一个圆形 SVG 元素,通过 CSS 动画实现水波效果。定义一个波浪路径,使用 animateTransform 或 CSS keyfram…

jquery加载页面

jquery加载页面

jQuery 加载页面内容的方法 使用 .load() 方法 通过 AJAX 请求加载远程数据并插入到指定元素中。适用于加载部分页面片段。 $("#targetElement").load("ext…

jquery页面加载

jquery页面加载

jQuery 页面加载事件 在 jQuery 中,页面加载事件通常通过 $(document).ready() 或简写的 $() 来实现。这种方式确保代码在 DOM 完全加载后执行,但无需等待图片等资…

vue 实现加载更多

vue 实现加载更多

Vue 实现加载更多功能 实现加载更多功能通常结合分页数据与滚动事件监听,以下是几种常见方法: 滚动监听 + 分页加载 监听滚动事件,当滚动到底部时触发加载更多数据: <template&g…

vue实现滚动鼠标加载

vue实现滚动鼠标加载

vue实现滚动鼠标加载的实现方法 使用Intersection Observer API Intersection Observer API可以监听元素是否进入视口,适合实现滚动加载。在Vue中可以通…