StPageFlip: Realistic Book Page Flip Animation In JavaScript

Category: Animation , Javascript , Recommended | June 8, 2026
AuthorNodlik
Last UpdateJune 8, 2026
LicenseMIT
Tags
Views34,997 views
StPageFlip: Realistic Book Page Flip Animation In JavaScript

StPageFlip is a vanilla JavaScript page flip library that creates realistic book and magazine page-turning animations from HTML elements or image files.

It works for digital catalogs, storybooks, portfolios, brochures, magazines, and document-style interfaces that need a familiar page-turning interaction. The library supports mobile touch gestures, portrait and landscape layouts, configurable shadows, soft page turns, and hard cover pages in HTML mode.

You can install it with npm or load the browser bundle from a CDN. The API includes methods for loading pages, turning pages programmatically, updating book content, reading the current page index, and listening for flip, orientation, state, init, and update events.

For another card-style interaction pattern, compare this stacked card carousel.

Basic usage:

Install StPageFlip

Install the page-flip package and import PageFlip in your JavaScript.

# NPM
$ npm install page-flip --save
import {PageFlip} from 'page-flip';

For a browser build, load page-flip.browser.min.js from a CDN or a local file.

<script src="https://cdn.jsdelivr.net/npm/page-flip/dist/js/page-flip.browser.min.js"></script>

Add Flipbook Pages

Create the page elements inside the flipbook container. In HTML mode, use data-density="hard" for hard covers or other rigid pages.

<div class="flip-book" id="example">
  <div class="page page-cover page-cover-top" data-density="hard">
    <div class="page-content">
      <h2>BOOK TITLE</h2>
    </div>
  </div>
  <div class="page">
    <div class="page-content">
      <h2 class="page-header">Page Header 1</h2>
      <div class="page-image" style="background-image: url(1.jpg)"></div>
      <div class="page-text">Page Content 1</div>
      <div class="page-footer">2</div>
    </div>
  </div>
  <div class="page">
    <div class="page-content">
      <h2 class="page-header">Page Header 2</h2>
      <div class="page-image" style="background-image: url(2.jpg)"></div>
      <div class="page-text">Page Content 2</div>
      <div class="page-footer">3</div>
    </div>
  </div>
  ... more pages here ...
  <div class="page page-cover page-cover-bottom" data-density="hard">
    <div class="page-content">
      <h2>THE END</h2>
    </div>
  </div>
</div>

Initialize StPageFlip

Create a PageFlip instance with the flipbook container and configuration object.

// ES Module
const pageFlip = new PageFlip(
      document.getElementById("example"),
        {
          // options here
        }
);
// Browser
const pageFlip = new St.PageFlip(
      document.getElementById("example"),
        {
          // options here
        }
);

Load Pages

Load existing HTML page elements with loadFromHTML().

pageFlip.loadFromHTML(document.querySelectorAll(".page"));

For image-based books in Canvas mode, pass an array of image URLs to loadFromImages().

pageFlip.loadFromImages(['1.jpg', '2.jpg' ... ]);

All Configuration Options

The width and height values are required and must be greater than 0. Use "fixed" or "stretch" for size.

// ES Module
const pageFlip = new PageFlip(
  document.getElementById("example"),
  {
    startPage: 0,
    size: "fixed",
    // Required base page size
    width: 400,
    height: 600,
    // Used with size: "stretch"
    minWidth: 315,
    maxWidth: 1000,
    minHeight: 420,
    maxHeight: 1350,
    drawShadow: true,
    flippingTime: 1000,
    usePortrait: true,
    startZIndex: 0,
    autoSize: true,
    maxShadowOpacity: 1,
    showCover: false,
    mobileScrollSupport: true,
    swipeDistance: 30,
    clickEventForward: true,
    useMouseEvents: true,
    showPageCorners: true,
    disableFlipByClick: false
  }
);
OptionDefaultDescription
widthRequiredSets the base page width in pixels. The value must be greater than 0.
heightRequiredSets the base page height in pixels. The value must be greater than 0.
size"fixed"Uses fixed page dimensions or stretches the book within the configured minimum and maximum dimensions. Accepted values are "fixed" and "stretch".
minWidth0Sets the minimum page width for stretch mode. Values less than or equal to 0 fall back to 100.
maxWidth0Sets the maximum page width for stretch mode. A value below minWidth falls back to 2000.
minHeight0Sets the minimum page height for stretch mode. Values less than or equal to 0 fall back to 100.
maxHeight0Sets the maximum page height for stretch mode. A value below minHeight falls back to 2000.
startPage0Sets the page index displayed when the book loads.
drawShadowtrueDraws page shadows during the flip animation.
flippingTime1000Sets the page flip animation duration in milliseconds. The value must be greater than 0.
usePortraittrueLets the book switch to portrait mode when the available area becomes narrow.
startZIndex0Sets the initial z-index used by the book.
autoSizetrueSizes the parent element from the calculated book dimensions.
maxShadowOpacity1Sets maximum shadow opacity from 0 to 1.
showCoverfalseMarks the first and last pages as hard covers and displays them in single-page mode.
mobileScrollSupporttrueKeeps normal page scrolling available on touch devices until a book flip interaction begins.
swipeDistance30Sets the minimum horizontal distance in pixels used to detect a swipe.
clickEventForwardtrueLeaves clicks on child <a> and <button> elements available to those elements.
useMouseEventstrueEnables mouse and touch input for interactive page turning.
showPageCornerstrueFolds a page corner when the pointer moves over an active corner.
disableFlipByClickfalseRestricts click-triggered page turns to the page corners.

API Methods

// Get the total number of pages.
pageFlip.getPageCount();
// Get the current orientation.
pageFlip.getOrientation();
// Get the current book size and position.
pageFlip.getBoundsRect();
// Get the current page index. The index starts at 0.
pageFlip.getCurrentPageIndex();
// Move to a specific page without animation.
pageFlip.turnToPage(4);
// Flip to a specific page with animation.
pageFlip.flip(6, 'bottom');
// Update pages from existing HTML elements.
pageFlip.updateFromHtml(document.querySelectorAll('.page'));
// Update pages from image URLs.
pageFlip.updateFromImages([
  '/images/catalog-1.jpg',
  '/images/catalog-2.jpg'
]);
MethodDescription
loadFromHTML(items)Loads pages from a NodeListOf<HTMLElement> or an array of HTML elements.
loadFromImages(imagesHref)Loads pages from an array of image URLs in Canvas mode.
updateFromHtml(items)Replaces the current HTML pages and keeps the current page index.
updateFromImages(imagesHref)Replaces the current image pages and keeps the current page index.
clear()Clears the current HTML page collection and resets the HTML UI.
update()Recalculates the render area and displays the current page again.
destroy()Destroys the UI, removes the root book element, and removes registered UI event handlers.
turnToPage(page)Moves to a page index without animation.
turnToNextPage()Moves to the next page without animation.
turnToPrevPage()Moves to the previous page without animation.
flip(page, corner = "top")Flips to a page index with animation. The corner can be "top" or "bottom".
flipNext(corner = "top")Flips to the next page with animation.
flipPrev(corner = "top")Flips to the previous page with animation.
getPageCount()Returns the total number of pages.
getCurrentPageIndex()Returns the current zero-based page index.
getPage(pageIndex)Returns the Page object at a page index.
getOrientation()Returns the current "portrait" or "landscape" orientation.
getBoundsRect()Returns the current book dimensions and position as a PageRect.
getSettings()Returns the processed FlipSetting configuration object.
getState()Returns the current flipping state.
getRender()Returns the active Render object.
getFlipController()Returns the active Flip controller.
getUI()Returns the active UI object.
getPageCollection()Returns the active PageCollection.
on(eventName, callback)Registers a callback for a StPageFlip event.
off(event)Removes all registered handlers for an event name.
updateState(newState)Updates the flip state and triggers changeState.
updatePageIndex(newPage)Updates the page index event state and triggers flip.
updateOrientation(newOrientation)Updates orientation styles and triggers changeOrientation.
startUserTouch(pos)Starts a pointer-driven page turn from book-relative coordinates.
userMove(pos, isTouch)Passes pointer movement to the page-turn controller.
userStop(pos, isSwipe = false)Ends the current pointer-driven page-turn interaction.

Events

Register an init listener before calling loadFromHTML() or loadFromImages() if you need the initialization event.

pageFlip.on('init', function (event) {
  console.log('Initial page:', event.data.page);
  console.log('Initial mode:', event.data.mode);
});
pageFlip.on('flip', function (event) {
  console.log('Current page:', event.data);
});
pageFlip.on('changeOrientation', function (event) {
  console.log('Orientation:', event.data);
});
pageFlip.on('changeState', function (event) {
  console.log('State:', event.data);
});
pageFlip.on('update', function (event) {
  console.log('Updated page:', event.data.page);
  console.log('Updated mode:', event.data.mode);
});
// Remove all handlers registered for an event.
pageFlip.off('flip');
EventEvent dataFires when
flipnumberThe current page index changes.
changeOrientation"portrait" or "landscape"The book switches orientation.
changeState"user_fold", "fold_corner", "flipping", or "read"The page-turning state changes.
init{ page, mode }The book initializes and loads its start page.
update{ page, mode }updateFromHtml() or updateFromImages() replaces the page collection.

FAQs:

Q: Can I use StPageFlip without npm?
A: Yes. Load page-flip.browser.min.js from a CDN or local bundle, then create the book with new St.PageFlip(...).

Q: Does StPageFlip create CSS-only page flip animations?
A: No. StPageFlip uses JavaScript to control page loading, page turning, orientation changes, and events. Use a CSS-only flip book snippet when you only need a static CSS effect.

Q: Why do hard pages only work in some examples?
A: Hard and soft page types apply to HTML mode. Add data-density="hard" to cover pages loaded with loadFromHTML().

Q: Can StPageFlip load image-based books?
A: Yes. Use loadFromImages() with an array of image URLs.

Alternatives:

Changelog:

v2.0.7 (04/18/2021)

  • Fixed a bug with resizing the window after updating book state

v2.0.6 (04/17/2021)

  • fix shadow after update

v2.0.5 (02/27/2021)

  • Fix var name

v2.0.2 (02/15/2021)

  • Fix constructor, fix td

v2.0.1 (02/14/2021)

  • Refactor

v2.0.0 (08/17/2020)

  • Refactor

v1.2.1 (08/16/2020)

  • Added “init” and”update” events

v1.1.0 (07/11/2020)

  • Append event forwarding to child elements

v1.0.3 (07/11/2020)

  • Bugfix

v1.0.1 (07/08/2020)

  • Refactoring

v1.0.1 (07/02/2020)

  • Fix: Swipe distance and mobile scroll

You Might Be Interested In:


Leave a Reply