当前位置:首页 > 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的管理确保渲染效率

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

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

相关文章

jquery加载

jquery加载

jQuery 加载方法 使用 CDN 加载 jQuery 推荐通过公共 CDN(如 Google、Microsoft 或 jQuery 官方)加载,速度快且可能已被浏览器缓存。 <…

vue实现加载图片

vue实现加载图片

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

vue实现点击更多

vue实现点击更多

Vue 实现点击更多功能 在 Vue 中实现点击更多功能通常涉及动态显示和隐藏内容,或者加载更多数据。以下是几种常见的实现方式: 使用 v-show 或 v-if 控制显示 通过 Vue 的指令 v…

vue实现流加载

vue实现流加载

Vue 实现流加载(无限滚动)的方法 流加载(无限滚动)是一种常见的网页优化技术,通过动态加载数据提升用户体验。以下是几种 Vue 实现流加载的方式: 使用 v-infinite-scroll 指令…

vue实现滚动加载更多

vue实现滚动加载更多

vue实现滚动加载更多的方法 使用IntersectionObserver API 在Vue中可以通过IntersectionObserver API监听元素是否进入可视区域,触发加载更多数据。 &…

vue实现通用页面加载

vue实现通用页面加载

Vue 实现通用页面加载的方法 在 Vue 中实现通用页面加载功能,可以通过多种方式实现,包括使用全局加载状态、自定义指令或封装组件。以下是几种常见的方法: 使用全局状态管理 通过 Vuex 或 P…