当前位置:首页 > 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;
    });
}

按钮点击加载实现

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

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';
      }
    });
});

无限滚动优化

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

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的管理确保渲染效率

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

js 加载更多实现

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

相关文章

vue实现懒加载的方法

vue实现懒加载的方法

Vue 实现懒加载的方法 路由懒加载 通过动态导入语法拆分路由组件,减少初始加载时间。适用于 Vue Router 配置: const routes = [ { path: '/hom…

vue实现加载

vue实现加载

Vue 实现加载功能的方法 使用 v-if 或 v-show 控制加载状态 在模板中通过 v-if 或 v-show 控制加载动画的显示与隐藏。v-if 会完全移除 DOM 元素,而 v-show 仅…

jquery加载页面

jquery加载页面

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

vue实现图片加载

vue实现图片加载

Vue 实现图片加载的方法 在 Vue 中实现图片加载可以通过多种方式,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind 动态绑定图片路径,可以灵活地加载本地或远程图片。…

vue实现加载图片

vue实现加载图片

Vue 实现图片加载的方法 在 Vue 中加载图片可以通过多种方式实现,以下是一些常见的方法: 使用静态资源路径 将图片放在 public 或 assets 目录下,通过相对路径或绝对路径引用。…

vue动态加载实现

vue动态加载实现

Vue动态加载实现方法 动态加载在Vue中通常指按需加载组件或资源,以下是几种常见实现方式: 使用异步组件 通过defineAsyncComponent或动态import()语法实现组件按需加载:…