当前位置: 首页 > news >正文

实现分页功能【jQuery】

效果:
在这里插入图片描述
在这里插入图片描述

分页功能:
页码分屏展示,当前示例一屏为5页,左右箭头为可以切换上一屏、下一屏

<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1.0"><title>分页</title><script src="./js/jquery-1.7.2.min.js"></script>
</head>
<style>#pagination {width: 210px;padding-top: 10px;display: flex;justify-content: center;position: absolute;bottom: 10px;//width: calc(100% - 20px); //外层有父元素固定长度}#pagination .page_list_box {flex: 1;overflow: hidden;}#pagination .page_list {display: flex;transform: translateX(0px);}#pagination span {width: 24px;height: 24px;flex-shrink: 0;font-size: 12px;text-align: center;line-height: 24px;border: 1px solid #ddd;border-radius: 4px;cursor: pointer;margin-right: 4px;position: relative;}#pagination span.disabled {background: #eee;cursor: not-allowed;}#pagination span.active {border-color: #3b84ec;color: #3b84ec;font-weight: 600;}#pagination span:not(.disabled):hover {border-color: #3b84ec;}#pagination .page_left::before {content: '';position: absolute;top: 0;left: 0;right: 0;bottom: 0;background: url(./images/arrow_left.png) no-repeat center;background-size: 70%;}#pagination .page_right::before {content: '';position: absolute;top: 0;left: 0;right: 0;bottom: 0;background: url(./images/arrow_right.png) no-repeat center;background-size: 70%;}
</style>
<body><!-- 分页 --><div id="pagination"><span class="page_left disabled" onclick="pagetrans(this,'pre')"></span><div class="page_list_box"><div class="page_list"></div></div><span class="page_right" onclick="pagetrans(this,'next')"></span></div>
</body>
<script>//当前页码
var currentPage = 1;
//页数
var pageLength = 10;
//滑动距左的页数
var currentleft = 0
renderPage()
function renderPage(){$(".page_list").empty()var pageStr = ''for (let i = 0; i < pageLength; i++) {pageStr += '<span onClick="pageClick(this)">'+ (i * 1 + 1)+'</span>'}$(".page_list").append(pageStr)$(".page_list span").eq(currentPage - 1).addClass("active")if(pageLength == 1) {$(".page_right").addClass('disabled')}
}
function pageClick(object){if(object.innerText == currentPage) returncurrentPage = object.innerText$(object).addClass("active").siblings().removeClass("active")renderHistorys()
}
function pagetrans(object,type) {if($(object).hasClass('disabled')) returnleftLength = Math.ceil(pageLength / 5)var currentNum = $(".page_list")[0].style.transform ? $(".page_list")[0].style.transform.split('px')[0].split("(")[1] : '0'if(type == 'pre') {if(currentleft > 0) {var num = (currentNum * 1 + 150) + 'px'$(".page_list").css("transform","translateX("+num+")")currentleft--if(currentleft == 0) {$(".page_left").addClass('disabled')}if(currentleft < leftLength - 1) {$(".page_right").removeClass('disabled')}}}else {if(currentleft < leftLength - 1) {var num = (currentNum * 1 - 150) + 'px'$(".page_list").css("transform","translateX("+num+")")currentleft++if(currentleft > 0) {$(".page_left").removeClass('disabled')}if(currentleft == leftLength - 1) {$(".page_right").addClass('disabled')}}}
}
</script>
</html>
http://www.dtcms.com/a/325794.html

相关文章:

  • GDB调试 core dump 文件与栈溢出分析
  • 《Python入门:从零到Hello World的极简指南》
  • 板子 7.20--8.11
  • Spring Boot 参数校验 Validation 入门
  • 华为云计算的行业趋势:迈向智能、融合与绿色的未来
  • 【工控】线扫相机小结 第六篇
  • 用vscode 里docker显示不出有容器和镜像 ?
  • 通用 maven 私服 settings.xml 多源配置文件(多个仓库优先级配置)
  • SQL179 每个6/7级用户活跃情况
  • 十一、Linux Shell脚本:函数与模块化
  • 逃离城市与喧嚣,拥抱新的生活方式
  • 开博尔雷电5数据线:120Gbps“闪电传输”,以Intel硬核基因从容优化数字生活
  • 【SpringBoot】持久层 sql 注入问题
  • C/C++练习面试题
  • PyTorch基础(使用Numpy实现机器学习)
  • PyTorch基础(使用Tensor及Antograd实现机器学习)
  • OCSSA-VMD-Transformer轴承故障诊断,特征提取+编码器!
  • cs的搭建和使用
  • 力扣-153.寻找旋转排序数组中的最小值
  • Kubernetes-核心概念
  • 2438. 二的幂数组中查询范围内的乘积
  • flutter入门
  • 从 Kotlin ViewModel 到 Flutter:完整迁移指南与状态管理实践
  • Flutter Dialog、BottomSheet
  • Python调用C/C++函数库的多种方法与实践指南
  • LCR 120. 寻找文件副本
  • LLM 残差链接是什么
  • TRL - Transformer Reinforcement Learning SFTTrainer 和 SFTConfig
  • docker是什么以及镜像命令详解
  • ROS2学习(1)—基础概念及环境搭建