“Where am I in this book?” has several meaningful answers at once: a byte offset, a chapter name, a page number, a percentage. foliate-js exposes all of these through a singleDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/johnfactotum/foliate-js/llms.txt
Use this file to discover all available pages before exploring further.
relocate event that fires whenever the visible content changes, plus a pair of helper methods for building chapter-level progress bars and context-aware running heads.
The relocate event
<foliate-view> emits relocate whenever the reader’s position changes — on navigation, page turns, or scroll. Listen to it to update your UI:
Event detail properties
| Property | Type | Description |
|---|---|---|
cfi | string | EPUB CFI identifying the current position. Save this to restore the reader’s place. |
fraction | number | Total book progress as a value from 0 to 1, weighted by section byte size. |
index | number | Index of the currently displayed section in book.sections. |
tocItem | object | null | The deepest TOC entry that precedes the current position. Has .label and .href. |
pageItem | object | null | The deepest page list entry that precedes the current position. |
location | { current, next, total } | Approximate location numbers derived from byte size (1500 bytes per location by default). |
time | { section, total } | Estimated remaining reading time in seconds (1600 bytes per second by default). |
section | { current, total } | The current section index and the total section count. |
Saving and restoring position
Save thecfi string from the relocate event and pass it back as lastLocation when you reopen the book:
view.init() navigates to lastLocation if it resolves successfully. If lastLocation is null or fails to resolve and showTextStart is true, it navigates to the first landmark marked as bodymatter or the first linear section.
CFIs are tied to the structure of the book file. A CFI from one edition will not correctly identify a position in a different edition of the same text.
Displaying a book-level progress bar
Usefraction from the relocate event directly:
Displaying a chapter-level progress bar
view.getSectionFractions() returns an array of cumulative fractions, one per section. Use these values to show how far the reader has progressed within the current chapter relative to the entire book.
getSectionFractions() has one extra entry at the end (value 1) so that sectionFractions[index + 1] always gives the section’s end fraction.
Getting TOC and page context for an arbitrary position
view.getProgressOf(index, range) looks up the TOC item and page list item for a given section index and DOM Range. Use this when you need progress information for a position that is not the current view — for example, when generating a preview or building a table of search results.
Resolve the target to a section index and anchor
Use
view.resolveNavigation(cfi) or book.resolveHref(href) to get { index, anchor }.Create a document for the section
Call
await book.sections[index].createDocument() to get the section’s Document.Running chapter heads
The paginator renderer exposes.heads and .feet properties — arrays of DOM elements, one per column, that sit in the header and footer regions of each page. Use these to display the current chapter title as a running head.
heads and feet properties are only available in paginated mode (flow="paginated"). In scrolled mode they are absent.
Style the head region with the ::part(head) CSS pseudo-element:
There will be one element in
renderer.heads for each visible column. In a two-column layout, update all elements in the array.