5

这是简短的...

我需要使用 jQuery 或 javascript 对大量文本进行分页。现在我考虑过进行字符计数等,但问题是我没有使用等宽文本,所以这是行不通的。

相反,我使用的是动态输入的文本(直接来自我最好的朋友 wordpress)。

这是设置:


我的设置


我已将文本放在一个名为bodytext的 div 中,它的溢出设置为 auto。这是它的样式:

.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;
}

无论如何...文本溢出。

我想要做的是创建一系列 div(带有bodytext类),所有这些都在彼此旁边,我可以将我的按钮连接起来以在它们之间切换。

不过,我发现了这一点很好的信息:每 18 行我需要创建一个新的 div。

我不知道如何解决这个问题。

我还需要它能够处理大量文本......一次可能最多 1000 个单词......导致说 10 页......


任何帮助都会很可爱!谢谢阅读!

4

2 回答 2

1

这个概念证明可以工作(也适用于 css3 列),但要注意,这会消耗 CPU;它是 DOM 密集型的,并且需要 jQuery。

这需要将文本分成更短的段落(或任何其他标记),但如果文本太大,甚至应该可以在客户端进行。

伪标记:

文章
  标题,介绍等
  部分
    带有(文本)内容的段落、div、span 等

代码:

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);
}
于 2015-12-28T20:27:34.377 回答
0

假设您的目标浏览器支持 CSS3 多列布局,它将完成这项工作:http: //caniuse.com/#feat=multicolumn

您需要裁剪文本的外部 div、具有固定宽度和高度的列的内部 div 以及可以修改内部 div 左侧边距的按钮。

许多功能只是部分支持(根据我的经验 Opera 表现最好),但对于许多情况来说应该足够了。请参阅http://www.w3.org/TR/css3-multicol/,您可以在其中找到许多很好的示例(尤其是示例 XXIII,“多列元素之外的分页和溢出”)。

于 2012-11-06T12:40:05.333 回答