gpt4 book ai didi

javascript - 如何使用 jQuery/javascript 对文本进行分页?

转载 作者:搜寻专家 更新时间:2023-11-01 04:38:02 26 4
gpt4 key购买 nike

这是简介...

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

相反,我使用动态输入的文本(直接来 self 最好的 friend wordpress)。

这是设置:


my setup


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

.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 页……


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

最佳答案

这个概念验证会起作用(也适用于 css3 列),但请注意,这会产生 CPU 成本;它是 DOM 密集型的,需要 jQuery。

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

伪标记:

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

关于javascript - 如何使用 jQuery/javascript 对文本进行分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13249719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com