Implementation of a Web-based E-book Reader

Introduction On various online or offline reading platforms such as Fanqienovel, Dedao, Biquge, Qidian, Kindle, etc., although the content they host differs, the reading modes primarily fall into two categories: pagination mode and scroll mode. This article introduces the implementation of these two modes on the web, with a focus on pagination mode. Implementing Pagination Using overflow+scroll A simple implementation of pagination mode is shown below. For brevity, placeholder content has been removed. You can download the full content by clicking here. use scroll api to implement paginator * { padding: 0; margin: 0; } .ebook { box-sizing: border-box; column-count: 2; column-gap: 20px; column-fill: auto; height: 100vh; overflow: hidden; overflow-wrap: break-word; padding-left: 20px; padding-right: 20px; padding-top: 20px; } prev next E-book Title Jin Daxin stopped speaking and just stared at Wan Miaomiao, as if she were talking about someone unrelated. While Wan Miaomiao was lost in her thoughts, he recovered from the earlier embarrassment of being questioned. He picked up the wine glass, brought it close to his nose, sniffed it, and then gently put it down. "Sorry, I said too much!" Wan Miaomiao stood up, picked up the remote control, and selected a song titled "The Shepherd of Cocoto Sea." The melancholic melody instantly filled the cabin. "The rain that night couldn't keep you / The valley's wind accompanied my tears..." Wan Miaomiao closed her eyes and swayed gently to the melody... Jin Daxin felt nothing for this song. He thought the singer was merely being melodramatic. With so many beautiful women in the world, was it necessary to be so dramatic? const ebook = document.querySelector('.ebook') const nextPage = document.querySelector('.nextPage') const prevPage = document.querySelector('.prevPage') const ebookWidth = ebook.getBoundingClientRect().width // !!! width + gap - padding const delta = ebookWidth + 20 - 20 - 20 const scrollWidth = ebook.scrollWidth let pageIndex = 0 const pageCount = Math.floor(scrollWidth / delta) console.log(pageCount) nextPage.addEventListener('click', () => { if (pageIndex { if (pageIndex > 0) { pageIndex-- ebook.scrollTo({ top: 0, left: pageIndex * delta, }) } }) The main container for the e-book content is div.ebook. Below .ebook, HTML text content is dynamically added. In the CSS, the three most important elements are column-count: 2;, height: 100vh;, and overflow: hidden;. Setting column-count changes the layout of the content area to a multi-column layout. Without a fixed height, the content would naturally extend downward. By setting a fixed height of 100vh, the content extends horizontally, achieving the pagination effect. However, this would display all content, and .ebook would become a horizontally scrollable container. To display only the intended two columns, overflow: hidden; is used to clip the excess content, which also disables manual scrolling. The scroll API is then used to implement page-turning functionality. Inside .ebook, there are two parts: the dynamically added book text wrapped in .outer, and a placeholder element that takes up 100% of the width and height. Without this placeholder, scrolling to the last page might not work correctly. Adding this placeholder ensures smooth scrolling without affecting the content display, as the browser handles the placeholder's area appropriately. To implement page-turning, the distance to scroll left or right is calculated. Each page turn scrolls by the same absolute distance. After setting column-gap, each column (except the last) will have a gap on its right. Therefore, the scroll distance delta is calculated as width + gap - padding, where padding includes both left and right margins. Here, delta is calculated as the width of .ebook plus the column-gap value, minus the padding-left and padding-right values (both 20 pixels). ebook.scrollWidth is the total scrollable distance, and dividing it by delta gives the total number of pages in the current chapter. In JavaScript, the precision of DOM element dimensions can be categorized into three levels. Element.clientWidth/clientHeight retrieves the element's width and height excluding the border, rounded down to the nearest integer, offering the lowest precision. window.getComputedStyle retrieves the width and height based on the box-sizing property, retaining up to two decimal places, offering better precision. Element.getBoundingClientRect offers the highest precision, sometimes retaining up to 10 decimal place

Mar 9, 2025 - 11:07
 0
Implementation of a Web-based E-book Reader

Introduction

On various online or offline reading platforms such as Fanqienovel, Dedao, Biquge, Qidian, Kindle, etc., although the content they host differs, the reading modes primarily fall into two categories: pagination mode and scroll mode. This article introduces the implementation of these two modes on the web, with a focus on pagination mode.

Implementing Pagination Using overflow+scroll

A simple implementation of pagination mode is shown below. For brevity, placeholder content has been removed. You can download the full content by clicking here.


 lang="zh">
  
     charset="UTF-8" />
     name="viewport" content="width=device-width, initial-scale=1.0" />
    </span>use scroll api to implement paginator<span class="nt">
    
  

  
     style="position: fixed; left: 0;" class="prevPage">prev
     style="position: fixed; left: 50px;" class="nextPage">next
     class="ebook">
       class="outer">
        

E-book Title

Jin Daxin stopped speaking and just stared at Wan Miaomiao, as if she were talking about someone unrelated. While Wan Miaomiao was lost in her thoughts, he recovered from the earlier embarrassment of being questioned. He picked up the wine glass, brought it close to his nose, sniffed it, and then gently put it down. "Sorry, I said too much!" Wan Miaomiao stood up, picked up the remote control, and selected a song titled "The Shepherd of Cocoto Sea." The melancholic melody instantly filled the cabin. "The rain that night couldn't keep you / The valley's wind accompanied my tears..." Wan Miaomiao closed her eyes and swayed gently to the melody... Jin Daxin felt nothing for this song. He thought the singer was merely being melodramatic. With so many beautiful women in the world, was it necessary to be so dramatic?

style="width: 100%; height: 100%; background-color: aqua;">