


Native js implements page turning function that can turn pages up and down (code)
The content of this article is about the native js implementation of the page turning function (code) that can turn pages up and down. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .
The page turning function is often used when rendering data. The following is a page turning function implemented using native JS, with the up and down page turning function. The rendering is as follows:
Main steps/ideas:
Realize the page effect;
When you click on the page number, judge according to the situation and control the page turning changes , there are the following situations:
(a).Total number of pages>Limited number of pages =》There are 10 pages, and only 5 pages are displayed at a time
(a).Total number of pages =Limited number of pages =》Only 1 page will be returned, and only 5 pages will be displayed at a time
(a).Total number of pages 3. The up and down page turning function, according to the page turning situation, the page turning function should be removed on the first page and the last page; html css Related recommendations: How to implement page turning function in native JS js Drag and drop page turning implementation code_javascript skills Js to implement web page keyboard control How to turn pages_javascript skills The above is the detailed content of Native js implements page turning function that can turn pages up and down (code). For more information, please follow other related articles on the PHP Chinese website!Page effect implementation:
<div class="pagination">
<a href="javascript:;" class="page-pre">上一页</a>
<ul></ul>
<a href="javascript:;" class="page-next">下一页</a>
</div>
/** 分页 */
.pagination{
display: inline-block;
}
.pagination>ul{
padding: 0;
margin: 0;
display: inline-block;
border: 1px solid #e2e2e2;
}
.pagination>ul>li{
float: left;
border-right-width: 1px;
border-right-style: solid;
border-right-color: #e2e2e2;
}
.pagination>ul>li:first-child{
background-color: rgb(30, 159, 255);
}
.pagination>ul>li:last-child{
border-right-width: 0px;
}
.pagination>ul>li>a{
display: inline-block;
width: 38px;
height: 30px;
line-height: 30px;
text-align: center;
color: #333;
}
.pagination>ul>li>a:hover{
opacity: 0.7;
}
.pagination>a{
display: inline-block;
width: 50px;
height: 30px;
line-height: 30px;
text-align: center;
color: #333;
border: 1px solid #e2e2e2;
}
.pagination a.page-pre{
margin-right: -1px;
float: left;
color: #d2d2d2;
cursor: not-allowed;
border-right-width: 0px;
}
.pagination a.page-next{
float: right;
border-left-width: 0px;
}
/* 分页 end **/
Realization of page turning effect
var pagination = function (o){
/**
* 翻页会出现的情况:
* 总页数 > 限制页数 30 > 10
* 总页数 < 限制页数 5 < 10
* 总页数 = 限制页数 10 = 10
* @var pnCount - 显示多少页码 => 限制显示页码 < 页码总数 (按限制显示) : 限制显示页码 > 页码总数 (按页码总数显示)
* @var midN - 翻页的中间页码位置
*
*/
var config = {
count: o.count || 10,
limit: o.limit || 5, //每页显示的条数
};
var count = config.count,
limit = config.limit,
eUl = dorea(".pagination ul")[0],
ePre = dorea(".pagination .page-pre")[0],
eNext = dorea(".pagination .page-next")[0],
eUlChild = eUl.children,
pnCount = limit < count ? pnCount = limit : pnCount = count,
midN = Math.ceil( pnCount / 2 );
/* 初始化上下翻页的页码 */
ePre.setAttribute("data-page","1");
eNext.setAttribute("data-page","1");
/*
* @func renderPag
* @desc 渲染分页
* @param { string } startN - 页码起始数
* @param { string } currP - 当前页数 ,初始化该函数时可不传
* @var childLen - 所有的子元素(页码)的长度
*/
var renderPag = function (startN,currP){
var childLen = eUlChild.length;
/* 渲染前先清空所有页码 */
for ( var d = childLen-1; d>=0; d-- ) {
eUlChild[d].parentNode.removeChild(eUlChild[d]);
}
/* 渲染页码 */
for ( var i = startN; i <pnCount+startN; i++ ) {
var eLi = creatEle("li"),
eA = creatEle("a");
eA.innerHTML = i;
eA.setAttribute("href","javascript:;");
eLi.setAttribute("data-page",i);
eLi.appendChild(eA);
eUl.appendChild(eLi);
/* 添加页码的点击事件,获取当前页码currPage */
eLi.addEventListener("click",function (){
var currPage = this.getAttribute("data-page");
turnPag(currPage);
ePre.setAttribute("data-page",currPage);
eNext.setAttribute("data-page",currPage);
});
}
/* 每次重新渲染翻页时,判断当前页情况(是否属于首页和尾页) */
if (currP!==undefined) {
if (currP=="1") {
ePre.style.color = "#d2d2d2";
ePre.style.cursor = "not-allowed";
ePre.removeEventListener("click",preFn,false);
}else if(currP==count){
eNext.style.color = "#d2d2d2";
eNext.style.cursor = "not-allowed";
eNext.removeEventListener("click",nextFn,false);
}else{
ePre.style.color = "#333";
ePre.style.cursor = "pointer";
eNext.style.color = "#333";
eNext.style.cursor = "pointer";
ePre.addEventListener("click",preFn,false);
eNext.addEventListener("click",nextFn,false);
}
}
};
/**
* @func turnPag
* @desc 翻页事件判断,主要用于点击事件发生后,进行页码渲染前的判断
* @param { string } cp - 传入一个点击所获得的当前页数
* 情况:1) count > limit
* a). limit的前半部分页码,例如 10,5 ,前半部分是 1,2 => 起始页为 1
* b). limit的后半部分页码,例如 10,5 ,后半部分是 9,10 => 起始页为 (count-limit)+1
* b). limit的中间部分,例如 10,5 ,中间部分是 4-7 => 起始页为 (当前页 - (limit/2))+1
* 情况:2) count = limit => 起始页为 1
* 情况:3) count < limit => 限制10条,但真实数据确只有5条
* a). 发生这类情况,限制条数应以总数据条数为准则
*
*/
var turnPag = function (cp){
console.log("当前第 "+cp+" 页");
if (count>limit) {
if ( cp<=midN ) { //判断是否属于前部分
renderPag(1,cp);
}else if( cp<=count && cp>count - midN ){ //判断是否属于后部分
renderPag( (count - limit)+1 ,cp) ;
}else{
renderPag( (cp-midN)+1 ,cp);
}
}else if (count===limit || count<limit) {
renderPag(1);
}else{
renderPag( (count - midN)-1 ,cp);
}
for (var i = 0; i<eUlChild.length; i++) {
eUlChild[i].style.backgroundColor = "#fff";
if (eUlChild[i].getAttribute("data-page") == cp) {
eUlChild[i].style.backgroundColor = "#1E9FFF"; /* 选中状态 */
}
}
};
/**
* @func preFn
* @desc 上翻页
* @func nextFn
* @desc 下翻页
*/
var preFn = function (){
var currPage = this.getAttribute("data-page");
currPage--;
turnPag(currPage);
ePre.setAttribute("data-page",currPage);
eNext.setAttribute("data-page",currPage);
};
var nextFn = function (){
var currPage = this.getAttribute("data-page");
currPage++;
turnPag(currPage);
ePre.setAttribute("data-page",currPage);
eNext.setAttribute("data-page",currPage);
};
renderPag(1);
/*
* 初次渲染翻页时,判断当前的总页数情况,初始化翻页功能
* 情况: 1) count > limit 上翻页:暗色,删除事件 - 下翻页:亮色,点击事件
* 情况: 2) count = limit 上下翻页:暗色,删除事件
* 情况: 3) count < limit 上下翻页:暗色,删除事件
*/
if (count>limit) {
ePre.style.color = "#d2d2d2";
ePre.style.cursor = "not-allowed";
ePre.removeEventListener("click",preFn,false);
eNext.addEventListener("click",nextFn,false);
}else{
ePre.style.color = "#d2d2d2";
ePre.style.cursor = "not-allowed";
ePre.removeEventListener("click",preFn,false);
eNext.style.color = "#d2d2d2";
eNext.style.cursor = "not-allowed";
eNext.removeEventListener("click",nextFn,false);
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
