当前位置:首页 > JavaScript

js 加载更多实现

2026-02-28 23:53:16JavaScript

滚动加载实现

滚动加载是一种常见的分页加载方式,通过监听滚动事件触发数据加载。核心逻辑是当用户滚动到接近页面底部时,自动加载下一页数据。

window.addEventListener('scroll', function() {
  const scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  const windowHeight = window.innerHeight;
  const scrollHeight = document.documentElement.scrollHeight;

  if (scrollTop + windowHeight >= scrollHeight - 100) {
    loadMoreData();
  }
});

function loadMoreData() {
  // 防止重复加载
  if (isLoading) return;

  isLoading = true;
  fetch('/api/data?page=' + currentPage)
    .then(response => response.json())
    .then(data => {
      renderData(data);
      currentPage++;
      isLoading = false;
    });
}

按钮点击加载实现

通过点击"加载更多"按钮手动触发数据加载,适合需要用户主动控制的场景。

js 加载更多实现

document.getElementById('load-more-btn').addEventListener('click', function() {
  fetch('/api/data?page=' + currentPage)
    .then(response => response.json())
    .then(data => {
      renderData(data);
      currentPage++;

      // 如果已是最后一页,隐藏按钮
      if (data.isLastPage) {
        this.style.display = 'none';
      }
    });
});

无限滚动优化

对于性能敏感的无限滚动场景,需要添加防抖和加载状态控制。

js 加载更多实现

let isLoading = false;
let currentPage = 1;

// 防抖函数
function debounce(func, delay) {
  let timer;
  return function() {
    clearTimeout(timer);
    timer = setTimeout(func, delay);
  };
}

const checkScroll = debounce(function() {
  const { scrollTop, clientHeight, scrollHeight } = document.documentElement;

  if (scrollTop + clientHeight >= scrollHeight - 100 && !isLoading) {
    loadMoreData();
  }
}, 200);

window.addEventListener('scroll', checkScroll);

虚拟滚动技术

对于超长列表,虚拟滚动可以大幅提升性能,只渲染可视区域内的元素。

class VirtualScroll {
  constructor(container, itemHeight, renderItem) {
    this.container = container;
    this.itemHeight = itemHeight;
    this.renderItem = renderItem;
    this.data = [];
    this.visibleItems = [];

    container.addEventListener('scroll', this.handleScroll.bind(this));
  }

  setData(data) {
    this.data = data;
    this.render();
  }

  handleScroll() {
    const scrollTop = this.container.scrollTop;
    const startIdx = Math.floor(scrollTop / this.itemHeight);
    const endIdx = Math.min(
      startIdx + Math.ceil(this.container.clientHeight / this.itemHeight),
      this.data.length
    );

    this.renderVisibleItems(startIdx, endIdx);
  }

  renderVisibleItems(start, end) {
    // 实现可视区域渲染逻辑
  }
}

注意事项

实现加载更多功能时需要考虑以下关键点:

  • 节流或防抖滚动事件避免频繁触发
  • 加载状态管理防止重复请求
  • 错误处理机制保证健壮性
  • 加载指示器提升用户体验
  • 无更多数据时的状态提示
  • 列表项key的管理确保渲染效率

根据具体场景选择合适的实现方式,移动端通常使用滚动加载,管理后台可能更适合按钮点击加载。

标签: 加载更多
分享给朋友:

相关文章

vue实现加载条

vue实现加载条

Vue 实现加载条的方法 使用第三方库(如 NProgress) 安装 NProgress 库: npm install nprogress 在 Vue 项目中引入并使用: import NPro…

vue实现分页加载

vue实现分页加载

Vue 分页加载的实现方法 基础分页实现(后端分页) 安装依赖(如使用axios): npm install axios 示例代码: <template> <div>…

vue加载更多实现

vue加载更多实现

滚动监听实现加载更多 通过监听滚动事件判断是否到达底部触发加载。在组件的mounted钩子中添加滚动事件监听器,计算滚动位置与文档高度的关系。 mounted() { window.addEve…

vue实现异步加载组件

vue实现异步加载组件

异步组件的基本实现 在 Vue 中可以通过动态 import() 语法实现组件的异步加载。这种方式会返回一个 Promise,Vue 会在需要时自动处理加载逻辑。 const AsyncCompon…

vue实现页面加载更多

vue实现页面加载更多

Vue 实现页面加载更多功能 使用滚动事件监听 在 Vue 中实现加载更多功能,可以通过监听滚动事件来判断是否滚动到底部。当用户滚动到页面底部时,触发加载更多数据的逻辑。 <templ…

vue图片实现懒加载

vue图片实现懒加载

vue图片实现懒加载的方法 使用Intersection Observer API Intersection Observer API是现代浏览器提供的原生API,可以高效检测元素是否进入视口。 /…