码迷,mamicode.com
首页 > Web开发 > 详细

自己改造的Jquery版的分页JS

时间:2014-09-25 11:35:58      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   java   ar   for   div   

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>分页JS代码</title>
<style type="text/css">
.page{margin:2em;}
.page a{text-decoration:none;display:inline-block;line-height:14px;padding:2px 5px;color:#333;border:1px solid #ccc;margin:0 2px;}
.page a:hover,.page a.on{background:#999;color:#fff;border:1px solid #333;}
.page a.unclick,.page a.unclick:hover{background:none;border:1px solid #eee;color:#999;cursor:default;}
</style>
<script	src="/js/jquery-1.7.1.js" type="text/javascript"></script>
</head>
<body>
<div class="page"></div>
<div class="page" id="div2"></div>
</body>
<script type="text/javascript">
//container 容器,count 总页数 pageindex 当前页数
function setPage(container, count, pageindex) {
var container = container;
var count = count;
var pageindex = pageindex;
var a = [];
  //总页数少于10 全部显示,大于10 显示前3 后3 中间3 其余....
  if (pageindex == 1) {
    a[a.length] = "<a href=\"#\" class=\"prev unclick\">首页</a>";
	a[a.length] = "<a href=\"#\" class=\"prev unclick\"><<</a>";
  } else {
  	a[a.length] = "<a href=\"#\" class=\"prev\">首页</a>";
    a[a.length] = "<a href=\"#\" class=\"prev\"><<</a>";
  }

  //总页数小于10
  if (count <= 10) {
    for (var i = 1; i <= count; i++) {
      setPageList(pageindex,i,a);
    }
  }else {
  	//总页数大于10页
    if (pageindex <= 4) {
      for (var i = 1; i <= 5; i++) {
        setPageList(pageindex,i,a);
      }
      a[a.length] = "...<a href=\"#\">" + count + "</a>";
    }else if (pageindex >= count - 3) {
      a[a.length] = "<a href=\"#\">1</a>...";
      for (var i = count - 4; i <= count; i++) {
        setPageList(pageindex,i,a);
      }
    }
    else { //当前页在中间部分
      a[a.length] = "<a href=\"#\">1</a>...";
      for (var i = pageindex - 2; i <= pageindex + 2; i++) {
        setPageList(pageindex,i,a);
      }
      a[a.length] = "...<a href=\"#\">" + count + "</a>";
    }
  }
  if (pageindex == count) {
    a[a.length] = "<a href=\"#\" class=\"next unclick\">>></a>";
	a[a.length] = "<a href=\"#\" class=\"next unclick\">尾页</a>";
  } else {
    	 a[a.length] = "<a href=\"#\" class=\"next\">>></a>";
	  	 a[a.length] = "<a href=\"#\" class=\"next\">尾页</a>";
  }
  container.html(a.join(""));
  	//事件点击
 	 var oAlink = container.find("a");
     var inx = pageindex; //初始的页码
     
	 //点击首页
	 $(oAlink[0]).click(function(){
	 	if (inx == 1) {
        return false;
      }else{
	  	setPage(container, count, 1);
	  }
	 });
	 
	//点击上一页
  $(oAlink[1]).click(function(){
 
      if (inx == 1) {
        return false;
      }
      inx--;
	  setPage(container, count, inx);
      return false;
  });
 	for(var i=2;i<oAlink.length-2;i++){
		$(oAlink[i]).click(function(){
			inx = parseInt($(this).html());
       	 	setPage(container, count, inx);
       		 return false;
		});
	}
	//点击下一页
	$(oAlink[oAlink.length - 2]).click(function(){
		if (inx == count) {
        return false;
      }
	  inx++;
      setPage(container, count, inx);
      return false;
	});
	
	//点击尾页
	$(oAlink[oAlink.length - 1]).click(function(){
		if(inx == count){
			return false;
		}else{
			setPage(container, count, count);
		}
	});
}

/**
 * 设置分页的初始值
 * @param {Object} pageindex  当前页码
 * @param {Object} i
 * @param {Object} a  存放分页html的数组
 */
  function setPageList(pageindex,i,a) {
    if (pageindex == i) {
      a[a.length] = "<a href=\"#\" class=\"on\">" + i + "</a>";
    } else {
      a[a.length] = "<a href=\"#\">" + i + "</a>";
    }
  }

//	setPage(document.getElementsByTagName("div")[0],10,1);
	setPage($("#div2"),12,5);
</script>
</html>

效果图如下:

bubuko.com,布布扣

自己改造的Jquery版的分页JS

标签:style   blog   http   color   io   java   ar   for   div   

原文地址:http://blog.csdn.net/jihaixiao8/article/details/39546233

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!