当前位置:首页 > 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
分享给朋友:

相关文章

vue.js实现轮播

vue.js实现轮播

Vue.js 实现轮播功能 使用第三方库(推荐) Vue.js 生态中有许多成熟的轮播组件库,例如 vue-awesome-swiper 或 swiper,它们功能丰富且易于集成。 安装 swip…

js实现拖拽

js实现拖拽

实现拖拽的基本步骤 通过监听鼠标事件(mousedown、mousemove、mouseup)实现拖拽功能。以下是核心代码逻辑: const draggableElement = document…

js实现复制

js实现复制

使用document.execCommand方法 在较旧的浏览器中,可以使用document.execCommand('copy')实现复制功能。创建一个临时的textarea或input元素,将需要…

js 实现倒计时

js 实现倒计时

实现倒计时的基本方法 使用 JavaScript 实现倒计时可以通过 setInterval 或 setTimeout 结合日期计算来完成。以下是两种常见实现方式。 使用 setInterval 实…

原生js实现轮播图

原生js实现轮播图

基本结构搭建 HTML部分需要包含轮播图容器、图片列表及导航按钮。结构示例如下: <div class="slider-container"> <div class="slid…

js实现复制到剪贴板

js实现复制到剪贴板

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