使用js对文章内容进行分页

来源:互联网 发布:网络星期一是什么时候 编辑:程序博客网 时间:2024/05/02 02:12

Thinkphp中文章显示代码:

<div id="showContent">{$article.content|htmlspecialchars_decode}</div>

<div id="articlePages"></div>


js实现代码:

<script type="text/javascript">
    var obj = document.getElementById("showContent");
    var pages= document.getElementById("articlePages");
    //alert(obj.scrollHeight);
    window.onload= function()
    {
     var all=Math.ceil(parseInt(obj.scrollHeight)/ parseInt(obj.offsetHeight));
     //获取总页数,主要是应用scrollHeight
     pages.innerHTML="共"+ all +"页";
     for(var i=1; i<=all;i++) 
     {
      pages.innerHTML +=" <a href=\javascript:showPage('"+i+"');> "+i+"</a>&nbsp;"; 
         //输出所有页码
     }
    }
    function showPage(pageIndex)
    {
     obj.scrollTop = (pageIndex-1)* parseInt(obj.offsetHeight);
    }
 </script>


css代码:

#showContent {
color:black;
font-size: 16px;
height: 700px;
overflow: hidden;
}
#articlePages {
text-align: right;
}

0 0
原创粉丝点击