dongzhi4470 2013-01-05 07:20
浏览 98
已采纳

分页:如何将长文本显示为书页?

I have a very long text and I want to display it as a book in a web page. The user will use arrow keys to move forward and backward in same way like flipping pages of the book.

Leaving apart the transition of pages, how can this be achieved using jQuery?

What I thought was calculating the amount of text that will occupy the space on one page and then breaking the whole text into such pages and then displaying them. But it seems to be a bad idea for the space occupied will be platform dependent even if we fix the font.

One more problem that I was facing while using the space calculation method was due to the css justified display of text.

Has anyone done such thing before for a web page?

  • 写回答

1条回答 默认 最新

  • dongxiaoyan4388 2013-01-05 07:43
    关注

    To layout a long string in a beautiful book page format. You need to get the exact string portion. You can use this function.

    function get_page($text, $page_index, $line_length=76, $page_length=40){
        $lines = explode("
    ", wordwrap($texxt, $line_length, "
    "));
        $page_lines = array_slice($lines, $page_index*$page_length, $page_length);
        return implode("
    ", $page_lines);
    }
    
    $line_length = 70;
    $lines_per_page=50;
    $page = 3;
    $longtext= "...";
    
    $page_text = get_page($longtext, $page-1, $line_length, $page_length);
    

    See Demonstration.

    Example

    PHP

    $longtext = "..."; // it can be retrieved from sql as well.
    $index=is_int($_GET['page'])? intval($_GET['page']): 1;
    $line_length = 70;
    $lines_per_page=50;
    $longtext= "...";
    
    $page_text = get_page($longtext, $index-1, $line_length, $page_length);
    echo json_encode(array('text'=>$page_text));
    

    JQuery

    var nextPage=2;
    $.get("getpage.php", { page: nextPage }, function(data){
       alert("text is "+data.text;
       // show the text data.text
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?