[英]How can I split HTML content into pages that fit the screen?
I'm making a book reader web application. 我正在制作一本书籍阅读器网页应用程序。 From the server I get a long string with all of the book text in HTML format.
从服务器我得到一个长字符串,其中包含HTML格式的所有书籍文本。 It looks something like this:
它看起来像这样:
<div><p>Alice was beginning....</p></div></div>to get very ... </div>
What I want is to split this text into pages. 我想要的是将这个文本分成几页。 I want to get something like this:
我想得到这样的东西:
<li id='page1'>... text ...</li>
<li id='page2'>... text ...</li>
...
A single page should fill the viewport. 单个页面应填充视口。 Users shouldn't be able to scroll the book text.
用户不应该滚动书籍文本。 Instead, they should be able to move between the pages using buttons.
相反,他们应该能够使用按钮在页面之间移动。
I need some function to measure all content, split it into pieces of same maximum height, and tag the pieces with page numbers. 我需要一些功能来测量所有内容,将其分成相同最大高度的片段,并用页码标记片段。 It must also take into account pictures and long paragraphs (which may not fit on a single page).
它还必须考虑图片和长段落(可能不适合单页)。
How can I accomplish all of this? 我怎样才能完成所有这些?
To find out the dimensions of each bit of content, you'd have to let the browser actually render it and then measure it. 要找出每个内容的维度,您必须让浏览器实际渲染它然后进行测量。 Additionally, if you want to break in the middle of flow (eg in the middle of a paragraph), things get pretty difficult.
另外,如果你想在流的中间打破(例如在段落的中间),事情变得相当困难。 (You cannot measure individual characters/words without wrapping them in elements.) You'd also have to disable zoom, etc., so that your measurements have some lasting meaning.
(如果不将它们包含在元素中,则无法测量单个字符/单词。)您还必须禁用缩放等,以便您的测量具有一定的持久意义。 All in all, HTML rendering engines are specifically tailored to render flow content, which is pretty much the opposite to paged content.
总而言之,HTML呈现引擎专门用于呈现流内容,这与分页内容完全相反。
However, there are some approaches to paging, you could take, neither of which actually deals with rendering the whole text and taking measurements. 然而,有一些分页方法,你可以采取,实际上它们都不涉及渲染整个文本和进行测量。
One approach I'd try would be to simply 我试过的一种方法就是简单
This solution has the advantage of being simple to implement, but it's not perfect. 该解决方案具有易于实现的优点,但并不完美。 Images can be rendered across the break and the user wouldn't be able to see them whole.
图像可以在整个休息时间呈现,用户将无法完整地看到它们。
The only other approach I could think of at short notice is using columns . 我能在短时间内想到的另一种方法是使用列 。 You'd have to
你必须这样做
This solution needs modern browsers with support for columns
, but using columns
solves the problem of properly breaking flow content into pages. 此解决方案需要支持
columns
现代浏览器,但使用columns
可以解决将流内容正确分解为页面的问题。 I'd recommend trying this first, if at all possible. 如果可能的话,我建议先尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.