当前位置:首页 > 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实现动画

使用 CSS 动画与 JavaScript 控制 通过 JavaScript 动态添加或移除 CSS 类来触发动画。CSS 定义关键帧(@keyframes),JavaScript 通过 classL…

js实现复制到剪贴板

js实现复制到剪贴板

使用document.execCommand方法(传统方式,已废弃但部分浏览器仍支持) 该方法适用于旧版浏览器,但已被标记为废弃。执行前需确保文本已被选中: function copyToClipb…

js实现百叶窗

js实现百叶窗

使用CSS和JavaScript实现百叶窗效果 通过CSS动画和JavaScript事件监听可以实现百叶窗效果。核心思路是将内容区域分割为多个条状元素,通过控制它们的展开/折叠状态来模拟百叶窗。 &…

js实现目录

js实现目录

实现目录的基本思路 在JavaScript中实现目录功能通常涉及以下几个核心步骤:解析文档中的标题元素(如h1-h6),动态生成目录结构,并添加交互逻辑(如点击跳转)。以下是具体实现方法: 解析标题…

js钟表实现

js钟表实现

实现JavaScript钟表的基本方法 创建一个简单的JavaScript钟表可以通过以下步骤完成,涵盖数字和模拟两种形式。 数字钟表实现 HTML结构只需一个显示时间的容器: <div i…

js实现路由

js实现路由

js实现路由的方法 在JavaScript中实现路由功能可以通过多种方式完成,以下是几种常见的方法: 使用原生JavaScript实现路由 通过监听window.onhashchange事件来实现基…