简体   繁体   English

如何将HTML内容拆分为适合屏幕的页面?

[英]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. 然而,有一些分页方法,你可以采取,实际上它们都不涉及渲染整个文本和进行测量。

Scroll Override 滚动覆盖

One approach I'd try would be to simply 我试过的一种方法就是简单

  • render all text into a container, 将所有文本渲染到容器中,
  • style the container in such a way that the scrollbar is not visible (eg by pushing it off the screen), 以这样一种方式设置容器的样式,使滚动条不可见(例如,将其从屏幕上推下),
  • disable manual scrolling, and 禁用手动滚动,和
  • provide "paging" buttons that programatically scroll by exactly the height of your "page". 提供“分页”按钮,以编程方式精确滚动“页面”的高度。

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. 图像可以在整个休息时间呈现,用户将无法完整地看到它们。

Columns

The only other approach I could think of at short notice is using columns . 我能在短时间内想到的另一种方法是使用 You'd have to 你必须这样做

  • render all text into a container, 将所有文本渲染到容器中,
  • style the container to be "infinitely" wide and have "infinite" number of columns, each the width of the page, 将容器设置为“无限”宽,并且具有“无限”列数,每列都是页面的宽度,
  • absolutely position the container in its parent, 绝对将容器放在其父级中,
  • make your "paging" buttons move the container horizontally by the width of the page. 使“分页”按钮水平移动容器的页面宽度。

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.

相关问题 如何自动适应/调整 html5 textarea 元素以适应初始内容? - How can I auto fit/resize a html5 textarea element to fit the initial content? 如何将html拆分为全屏高度页面? - How to split html to full-screen height pages? 当我页面上的内容不适合屏幕时,它会移动页面的 rest。 我怎样才能解决这个问题? - When the content on my page doesn't fit the screen, it moves the rest of the page. How can I fix this? 如何获取 HTML 元素内容的图片,包括屏幕上不可见的内容? - How can I get a picture of an HTML element's content, including content not visible on the screen? 动态加载时,如何从 a.txt 文件中获取动态内容以显示在多个 html 页面中 - How can I get dynamic content from a .txt file to display in multiple html pages when they are loaded dynamically 如何将html页面中的div内容加载到主页上的div中? - How can I load div content from html pages into a div on main page? 如何缩小画布上的绘制以适合屏幕尺寸 - How can I scale down a draw on canvas to fit screen size 如何扩展图像高度以适应我的整个屏幕? - How can I extend image height to fit my whole screen? 无法在iframe中容纳整个HTML内容 - Can't fit entire HTML content in iframe 如何将 ejs 中的数组结果拆分为多个页面? - How can I split the result of an array in an ejs into multiple pages?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM