当前位置:首页 > JavaScript

js实现页码

2026-02-01 21:30:01JavaScript

实现页码功能的方法

使用原生JavaScript实现分页

创建一个简单的分页组件需要计算总页数、当前页以及生成页码按钮。以下是一个基础实现示例:

js实现页码

function createPagination(totalItems, itemsPerPage, currentPage, containerId) {
    const totalPages = Math.ceil(totalItems / itemsPerPage);
    const container = document.getElementById(containerId);
    container.innerHTML = '';

    // 上一页按钮
    if (currentPage > 1) {
        const prevBtn = document.createElement('button');
        prevBtn.textContent = '上一页';
        prevBtn.addEventListener('click', () => updatePage(currentPage - 1));
        container.appendChild(prevBtn);
    }

    // 页码按钮
    for (let i = 1; i <= totalPages; i++) {
        const pageBtn = document.createElement('button');
        pageBtn.textContent = i;
        if (i === currentPage) {
            pageBtn.classList.add('active');
        }
        pageBtn.addEventListener('click', () => updatePage(i));
        container.appendChild(pageBtn);
    }

    // 下一页按钮
    if (currentPage < totalPages) {
        const nextBtn = document.createElement('button');
        nextBtn.textContent = '下一页';
        nextBtn.addEventListener('click', () => updatePage(currentPage + 1));
        container.appendChild(nextBtn);
    }

    function updatePage(newPage) {
        // 这里可以添加页面跳转逻辑或回调函数
        console.log('跳转到页面:', newPage);
        createPagination(totalItems, itemsPerPage, newPage, containerId);
    }
}

// 使用示例
createPagination(100, 10, 1, 'pagination-container');

使用CSS样式美化页码

为页码添加基本样式:

js实现页码

#pagination-container button {
    margin: 0 5px;
    padding: 5px 10px;
    border: 1px solid #ddd;
    background: #fff;
    cursor: pointer;
}

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

#pagination-container button:hover:not(.active) {
    background: #f0f0f0;
}

高级分页实现(显示部分页码)

对于大量页码,可以限制显示的页码数量:

function createAdvancedPagination(totalItems, itemsPerPage, currentPage, containerId, maxVisiblePages = 5) {
    const totalPages = Math.ceil(totalItems / itemsPerPage);
    const container = document.getElementById(containerId);
    container.innerHTML = '';

    // 计算显示的页码范围
    let startPage = Math.max(1, currentPage - Math.floor(maxVisiblePages / 2));
    let endPage = Math.min(totalPages, startPage + maxVisiblePages - 1);

    if (endPage - startPage + 1 < maxVisiblePages) {
        startPage = Math.max(1, endPage - maxVisiblePages + 1);
    }

    // 上一页按钮
    if (currentPage > 1) {
        const prevBtn = document.createElement('button');
        prevBtn.textContent = '上一页';
        prevBtn.addEventListener('click', () => updatePage(currentPage - 1));
        container.appendChild(prevBtn);
    }

    // 第一页和省略号
    if (startPage > 1) {
        const firstBtn = document.createElement('button');
        firstBtn.textContent = '1';
        firstBtn.addEventListener('click', () => updatePage(1));
        container.appendChild(firstBtn);

        if (startPage > 2) {
            const ellipsis = document.createElement('span');
            ellipsis.textContent = '...';
            container.appendChild(ellipsis);
        }
    }

    // 页码按钮
    for (let i = startPage; i <= endPage; i++) {
        const pageBtn = document.createElement('button');
        pageBtn.textContent = i;
        if (i === currentPage) {
            pageBtn.classList.add('active');
        }
        pageBtn.addEventListener('click', () => updatePage(i));
        container.appendChild(pageBtn);
    }

    // 最后一页和省略号
    if (endPage < totalPages) {
        if (endPage < totalPages - 1) {
            const ellipsis = document.createElement('span');
            ellipsis.textContent = '...';
            container.appendChild(ellipsis);
        }

        const lastBtn = document.createElement('button');
        lastBtn.textContent = totalPages;
        lastBtn.addEventListener('click', () => updatePage(totalPages));
        container.appendChild(lastBtn);
    }

    // 下一页按钮
    if (currentPage < totalPages) {
        const nextBtn = document.createElement('button');
        nextBtn.textContent = '下一页';
        nextBtn.addEventListener('click', () => updatePage(currentPage + 1));
        container.appendChild(nextBtn);
    }

    function updatePage(newPage) {
        console.log('跳转到页面:', newPage);
        createAdvancedPagination(totalItems, itemsPerPage, newPage, containerId, maxVisiblePages);
    }
}

使用现有库实现分页

如果需要更复杂的功能,可以考虑使用现有库如:

  • Pagination.js
  • jQuery Pagination
  • Bootstrap Pagination组件

这些库通常提供更多定制选项和更好的浏览器兼容性。

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

相关文章

js实现选项卡

js实现选项卡

实现选项卡的基本思路 选项卡通常由一组标签和对应的内容面板组成。点击标签时,显示对应的内容面板,隐藏其他面板。实现这一效果需要结合HTML结构、CSS样式和JavaScript交互逻辑。 HTML结…

js实现tab选项卡切换

js实现tab选项卡切换

实现Tab选项卡切换的JavaScript方法 使用纯JavaScript实现Tab切换功能,可以通过监听点击事件动态切换内容显示状态。以下是两种常见实现方式: 基础DOM操作实现 /…

js手势实现

js手势实现

手势识别实现方法 在JavaScript中实现手势识别通常涉及监听触摸事件(touchstart、touchmove、touchend)或鼠标事件(mousedown、mousemove、mouseu…

js树实现

js树实现

树的基本概念 树是一种非线性的数据结构,由节点和边组成。每个节点包含一个值和指向子节点的引用。树的顶部节点称为根节点,没有子节点的节点称为叶节点。 树的实现方式 在JavaScript中,树可以通过…

js实现授权

js实现授权

授权流程设计 授权流程通常涉及前端与后端的交互,常见方案包括OAuth2.0、JWT等。以JWT为例的典型流程: 用户提交凭证(如用户名密码)到认证服务 服务端验证通过后生成包含用户信息的JWT令牌…

js实现跑马灯

js实现跑马灯

实现跑马灯效果 使用HTML和JavaScript可以轻松实现跑马灯效果。以下是两种常见的实现方式: HTML结构 <div id="marquee"> <span>…