


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 skillsPage 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);
}
}
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!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


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

Dreamweaver Mac version
Visual web development tools

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

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
