当前位置:首页 > JavaScript

js实现加载更多内容

2026-03-01 06:30:23JavaScript

实现滚动加载更多内容

使用Intersection Observer API监听元素进入视口,触发加载更多数据。这种方法性能较好,避免频繁计算滚动位置。

const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      loadMoreData();
    }
  });
});

observer.observe(document.querySelector('.load-more-trigger'));

async function loadMoreData() {
  const response = await fetch('/api/data?page=' + currentPage);
  const newData = await response.json();
  appendDataToDOM(newData);
  currentPage++;
}

实现点击加载更多按钮

创建按钮点击事件处理程序,每次点击时获取新数据并追加到页面。

document.getElementById('load-more-btn').addEventListener('click', async () => {
  const response = await fetch('/api/data?page=' + currentPage);
  const newData = await response.json();

  newData.forEach(item => {
    const element = document.createElement('div');
    element.textContent = item.content;
    document.getElementById('content-container').appendChild(element);
  });

  currentPage++;

  if (currentPage >= totalPages) {
    document.getElementById('load-more-btn').disabled = true;
  }
});

实现无限滚动加载

监听窗口滚动事件,当接近底部时加载更多内容。需要添加防抖优化性能。

let isLoading = false;

window.addEventListener('scroll', debounce(() => {
  const { scrollTop, scrollHeight, clientHeight } = document.documentElement;

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

function debounce(fn, delay) {
  let timer;
  return function() {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, arguments), delay);
  };
}

实现分页加载

维护当前页码状态,每次请求时递增页码参数。

let currentPage = 1;
const pageSize = 10;

async function loadPaginatedData() {
  const response = await fetch(`/api/data?page=${currentPage}&size=${pageSize}`);
  const data = await response.json();

  if (data.length > 0) {
    renderData(data);
    currentPage++;
  } else {
    showNoMoreData();
  }
}

实现虚拟滚动优化

对于大量数据,使用虚拟滚动技术只渲染可见区域内容。

const container = document.getElementById('virtual-scroll');
const contentHeight = 100000; // 总内容高度
const visibleItems = 10;
const itemHeight = 50;

container.style.height = `${visibleItems * itemHeight}px`;
container.addEventListener('scroll', () => {
  const scrollTop = container.scrollTop;
  const startIndex = Math.floor(scrollTop / itemHeight);

  renderItems(startIndex, startIndex + visibleItems);
});

function renderItems(start, end) {
  // 只渲染start到end范围内的项目
}

注意事项

确保添加加载状态提示和错误处理 合理设置节流/防抖时间避免频繁请求 移动端需要考虑触摸事件兼容性 服务端应支持分页参数 加载完成后可以禁用按钮或移除观察者

js实现加载更多内容

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

相关文章

react如何重新加载

react如何重新加载

重新加载当前页面 在React中重新加载当前页面可以通过window.location.reload()方法实现。该方法会强制浏览器重新加载当前URL,类似于按下F5刷新页面。 const rel…

jquery页面加载

jquery页面加载

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

vue实现页面懒加载

vue实现页面懒加载

Vue 实现页面懒加载的方法 在 Vue 中实现页面懒加载可以通过路由懒加载和组件懒加载两种方式实现,以下是具体方法: 路由懒加载 路由懒加载是通过动态导入组件的方式实现,仅在访问对应路由时才加载…

vue实现图片显示更多

vue实现图片显示更多

实现图片显示更多的功能 在Vue中实现图片显示更多功能,可以通过动态渲染图片列表并结合“显示更多”按钮来控制展示数量。以下是具体实现方法: 1. 数据准备 定义一个包含所有图片URL的数组,并设置初…

vue实现无限滚动加载

vue实现无限滚动加载

实现无限滚动加载的方法 无限滚动加载是一种常见的优化长列表渲染的技术,通过监听滚动事件动态加载数据,避免一次性渲染大量DOM节点。以下是Vue中的几种实现方式: 使用IntersectionObs…

vue watch实现懒加载

vue watch实现懒加载

使用 Vue 的 watch 实现懒加载 在 Vue 中,可以通过 watch 监听数据变化,结合 Intersection Observer API 或滚动事件实现懒加载。以下是具体实现方法: 监…