简体   繁体   English

如何用jQuery / javascript分页文本?

[英]how do I paginate text with jQuery / javascript?

Here's the brief... 这是简短的......

I need to paginate very long amounts of text using jQuery or javascript. 我需要使用jQuery或javascript对非常长的文本进行分页。 Now I have thought about doing character counts etc but the problem is that I'm not using monospace text so that's won't work. 现在我已经考虑过进行角色计数等问题,但问题是我没有使用等宽文本,所以它不会起作用。

Instead I'm using dynamically entered text (straight from my best friend wordpress). 相反,我使用动态输入的文本(直接来自我最好的朋友wordpress)。

Here's the setup: 这是设置:


我的设置


I have placed the text in a div called bodytext which has overflow set to auto. 我已将文本放在一个名为bodytext的div中,该div将溢出设置为auto。 Here's it's styling: 这是它的造型:

.bodytext {
width: 465px;
height: 454px;
display: block;
position: absolute;
display: block;
margin: 136px 25px;
font-family: 'Gentium Basic', serif;
color: #141414;
line-height: 1.5;
line-height: 1.5;
font-size: 17px;
overflow: hidden;
}

Anyway... the text overflows. 无论如何......文字溢出。

What I want to do is create a series of div's (with the bodytext class) all beside eachother that I can hook my buttons up to to toggle between. 我想要做的是在彼此旁边创建一系列div(与bodytext类),我可以将我的按钮挂钩到其间切换。

I have found this good bit of information out though: every 18 lines I need to create a new div. 我已经发现了这方面的一些信息:每18条线我需要创建一个新的div。

I have no idea how to work this out though. 我不知道如何解决这个问题。

I also need it to be able to handle large quantities of text... perhaps up to 1000 words at a time... resulting in say 10 pages... 我还需要它能够处理大量文本...一次最多可达1000个单词...结果说10页......


any help would be lovely! 任何帮助都会很可爱! Thanks for reading! 谢谢阅读!

This proof-of-concept will work (also with css3 columns), but be warned, there is a CPU cost; 这个概念验证将起作用(也可以使用css3列),但要注意,有一个CPU成本; it's DOM-intensive and jQuery is required. 它是DOM密集型的,需要jQuery。

This requires the text to be divided into shorter paragraps (or any other tag), but it should be possible to even do that clientside if the text is in too big chunks. 这需要将文本划分为更短的段落(或任何其他标记),但如果文本太大,则应该可以在客户端进行操作。

Pseudo-markup: 伪标记:

ARTICLE
  header, intro etc
  SECTION
    paragraphs, divs, spans etc with (text) content

Code: 码:

function paginate() {
  var screen_height = $(window).height();
  var max_page_height = screen_height * 0.8;

  var articles = $('article').toArray().map(function(node) {
    node = $(node);
    var sections = node.find('section');

    return {
      node: node,
      sections: sections,
      sectionChildren: sections.children(),
    };
  });

  function appendNewSection(article) {
    var section = $('<section class="columns page">')
    article.append(section);
    return section;
  }

  function re_paginate(article) {
    article.sections.detach(); // Important magic
    var section = appendNewSection(article.node);

    // Clone `sectionChildren` and append to DOM
    article.sectionChildren.clone().each(function(_, child) {
      child = $(child);

      if (section.height() > max_page_height) {
        section = appendNewSection(article.node);
      }

      if (child.is('ul') || child.is('ol')) {
        // Split list into shorter lists
        // NOTE! This approach can also be used to split long paragraphs...
        var list_children = child.children();
        list_children.detach(); // Important magic
        var blueprint = child.clone(); // Empty list blueprint
        var list = child;

        section.append(list);

        list_children.each(function(_, list_child) {
          if (section.height() > max_page_height) {
            // Append a new section with a new list and continue appending `list_children` therein
            section = appendNewSection(article.node);
            list = blueprint.clone();
            section.append(list);
          }
          list.append(list_child);
        });
      }
      else {
        section.append(child);
      }
    });
  }

  // Re-paginate articles
  confirm('Paginate articles to screen height?') && articles.filter(re_paginate);
}

CSS3 Multiple column layout would do the job assuming that your target browsers support it: http://caniuse.com/#feat=multicolumn CSS3假设您的目标浏览器支持它,多列布局可以完成这项工作: http//caniuse.com/#feat=multicolumn

You need outer div which crops text, inner div with columns having fixed width and height and buttons which would modify margin left of inner div. 您需要使用外部div来裁剪文本,内部div使用具有固定宽度和高度的列以及可以修改内部div左边距的按钮。

Many features are only partially supported (according to my experience Opera performs the best), but it should be enough for many cases. 许多功能仅部分支持(根据我的经验,Opera表现最佳),但对于许多情况应该足够了。 See http://www.w3.org/TR/css3-multicol/ where you can find many good examples (esp. example XXIII, "Pagination and overflow outside multicol elements"). 请参阅http://www.w3.org/TR/css3-multicol/ ,您可以在其中找到许多好的示例(特别是示例XXIII,“多元素外部的分页和溢出”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM