当前位置:首页 > JavaScript

js实现翻页

2026-03-01 00:23:13JavaScript

实现翻页的基本逻辑

翻页功能通常需要以下核心要素:当前页码、每页数据量、总数据量、翻页按钮(上一页/下一页/页码跳转)。通过计算总页数和监听用户操作触发数据更新。

// 示例:基础翻页逻辑
const currentPage = 1;
const itemsPerPage = 10;
let totalItems = 0;

function updatePagination(newPage) {
  currentPage = Math.max(1, Math.min(newPage, getTotalPages()));
  loadData();
}

function getTotalPages() {
  return Math.ceil(totalItems / itemsPerPage);
}

function loadData() {
  const startIndex = (currentPage - 1) * itemsPerPage;
  const endIndex = startIndex + itemsPerPage;
  // 实际数据加载逻辑...
}

分页器UI实现

创建可交互的分页控件需要DOM操作和事件绑定。以下示例展示动态生成页码按钮:

function renderPagination() {
  const paginationContainer = document.getElementById('pagination');
  paginationContainer.innerHTML = '';

  // 上一页按钮
  const prevBtn = document.createElement('button');
  prevBtn.textContent = '«';
  prevBtn.disabled = currentPage === 1;
  prevBtn.addEventListener('click', () => updatePagination(currentPage - 1));
  paginationContainer.appendChild(prevBtn);

  // 页码按钮
  const totalPages = getTotalPages();
  for (let i = 1; i <= totalPages; i++) {
    const pageBtn = document.createElement('button');
    pageBtn.textContent = i;
    pageBtn.className = i === currentPage ? 'active' : '';
    pageBtn.addEventListener('click', () => updatePagination(i));
    paginationContainer.appendChild(pageBtn);
  }

  // 下一页按钮
  const nextBtn = document.createElement('button');
  nextBtn.textContent = '»';
  nextBtn.disabled = currentPage === totalPages;
  nextBtn.addEventListener('click', () => updatePagination(currentPage + 1));
  paginationContainer.appendChild(nextBtn);
}

异步数据加载

实际项目中常通过API获取分页数据,需处理异步逻辑:

async function fetchPaginatedData() {
  try {
    const response = await fetch(`/api/data?page=${currentPage}&limit=${itemsPerPage}`);
    const result = await response.json();
    totalItems = result.totalCount;
    renderData(result.items);
    renderPagination();
  } catch (error) {
    console.error('数据加载失败:', error);
  }
}

优化用户体验

添加功能增强:

  • 输入框跳转页码
  • 显示当前页信息
  • 移动端适配
// 页码跳转输入
function addPageJump() {
  const container = document.getElementById('pagination');
  const input = document.createElement('input');
  input.type = 'number';
  input.min = 1;
  input.max = getTotalPages();
  input.placeholder = '跳转';

  const goBtn = document.createElement('button');
  goBtn.textContent = 'GO';
  goBtn.addEventListener('click', () => {
    const page = parseInt(input.value);
    if (page >= 1 && page <= getTotalPages()) {
      updatePagination(page);
    }
  });

  container.appendChild(input);
  container.appendChild(goBtn);
}

完整示例CSS

基础样式增强交互性:

#pagination {
  display: flex;
  gap: 5px;
  margin-top: 20px;
}

#pagination button {
  padding: 5px 10px;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
}

#pagination button.active {
  background: #007bff;
  color: white;
  border-color: #007bff;
}

#pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

#pagination input {
  width: 50px;
  text-align: center;
}

js实现翻页

标签: 翻页js
分享给朋友:

相关文章

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.define…

js实现继承

js实现继承

原型链继承 通过将子类的原型指向父类的实例实现继承。子类实例可以访问父类原型上的属性和方法。 function Parent() { this.name = 'parent'; } Parent…

js实现图片上传

js实现图片上传

图片上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现图片上传功能。HTML部分需要创建一个文件选择输入框和一个用于…

js实现图表

js实现图表

在JavaScript中实现图表通常使用流行的图表库,以下是几种常见的方法和工具: 使用Chart.js Chart.js是一个简单灵活的库,适合快速生成响应式图表。安装方式包括CDN引入或npm安…

vue实现翻页效果

vue实现翻页效果

使用Vue实现翻页效果 在Vue中实现翻页效果通常需要结合分页组件和数据处理逻辑。以下是几种常见方法: 基础分页组件实现 <template> <div>…

js实现点击显示和隐藏

js实现点击显示和隐藏

实现点击显示和隐藏的JavaScript方法 使用classList.toggle切换类名 通过添加/移除CSS类控制元素的显示与隐藏,需提前在CSS中定义隐藏样式(如display: none)。…