JavaScript

How to Implement Page Flipping Like a Book Using Pageflip.js

Adding interactive user experiences, like page flipping, to a website can attract visitors’ attention and increase their time spent on your site. In this article, we will introduce how to implement a page-flipping effect, similar to flipping through a real book, on your website using a JavaScript library called pageflip.js. If you are looking to incorporate the latest technologies in web design, this article will be helpful for you.

Introduction

The design of a website’s interface should not only be visually appealing but also enhance the user experience. By adding page-flipping animations, visitors can browse information more intuitively, which is especially effective for digital magazines or catalogs. In this article, we will explain how to achieve simple yet effective page flipping using the pageflip.js library.

What is pageflip.js?

Pageflip.js is a lightweight JavaScript library that allows you to easily implement animations that mimic flipping through a real book on a webpage. This library uses the HTML5 Canvas API, combining high performance and flexibility. By using it, you can add dynamic elements to static web pages and enhance the user experience.

Writing CSS

First, set up the basic CSS styles needed to achieve page flipping. Here, we define styles for the page background, text styles, and layout. Load the `main.css` file.

<link type="text/css" href="main.css" rel="stylesheet" media="screen" />

The content of the `main.css` file is as follows. It describes the areas for page flipping (`#book`, `#pages section`, `#pageflip-canvas`). Modify as needed.

body, h2, p {
	margin: 0;
	padding: 0;
}
body {
background-color: #444;
color: #333;
font-family: Helvetica, sans-serif;
}

#book {
background: url("book.png") no-repeat;
position: absolute;
width: 830px;
height: 260px;
left: 50%;
top: 50%;
margin-left: -400px;
margin-top: -125px;
}

#pages section {
background: url("paper.png") no-repeat;
display: block;
width: 400px;
height: 250px;
position: absolute;
left: 415px;
top: 5px;
overflow: hidden;
}
#pages section>div {
display: block;
width: 400px;
height: 250px;
font-size: 12px;
}
#pages section p,
#pages section h1 {
padding: 3px 25px;
line-height: 1.4em;
text-align: justify;
}
#pages section h2 {
padding: 3px 25px;
line-height: 1.4em;
text-align: justify;
}
#pages section h2{
margin: 15px 0 10px;
}

#pageflip-canvas {
position: absolute;
z-index: 100;
}

This CSS file controls the layout and style of the entire page. #book sets the background image for the entire book, and #pages section specifies the style for each page.

Writing HTML

Next, create the HTML structure. In this example, we define one page per `section` tag and implement the page-flipping animation using the `canvas` element.
Each `section` tag represents one page.

		<div id="book">
			<canvas id="pageflip-canvas"></canvas>
			<div id="pages">
				<section>
					<div>
						<h1>You can flip left and right, with the center of the page as the boundary.</h1>
                        <h2>Page 1</h2>
						<p>Try flipping (moving) your mouse from right to left and left to right near the edges.</p>
					</div>
				</section>
				<section>
					<div>
                        <h2>Page 2</h2>
						<p>Try flipping (moving) your mouse from right to left and left to right near the edges.</p>
					</div>
				</section>
				<section>
					<div>
                        <h2>Page 3</h2>
						<p>Try flipping (moving) your mouse from right to left and left to right near the edges.</p>
					</div>
				</section>
				<section>
					<div>
                        <h2>Page 4</h2>
						<p>Try flipping (moving) your mouse from right to left and left to right near the edges.</p>
					</div>
				</section>
			</div>
		</div>

This HTML code defines the layout and content of the pages. The `section` tags separate each page, and the content for each page is placed within a `div`.

Writing JavaScript

Next, use the pageflip.js library to implement the page-flipping animation.

Loading pageflip.js

Add the following code to your HTML file to load the pageflip.js library.

<script type="text/javascript" src="pageflip.js"></script>

Content of the pageflip.js File

Next, here is the basic structure of the pageflip.js file. The content of the pageflip.js file is as follows. The script below defines the settings and event listeners needed to achieve the page-flipping effect.
(You can adjust the whole book, the size of one page, spacing, etc.)

(function() {
	
	// Dimensions of the whole book
	var BOOK_WIDTH = 830;
	var BOOK_HEIGHT = 260;
	
	// Dimensions of one page in the book
	var PAGE_WIDTH = 400;
	var PAGE_HEIGHT = 250;
	
	// Vertical spacing between the top edge of the book and the papers
	var PAGE_Y = ( BOOK_HEIGHT - PAGE_HEIGHT ) / 2;
	
	// The canvas size equals to the book dimensions + this padding
	var CANVAS_PADDING = 60;
	
	var page = 0;
	
	var canvas = document.getElementById( "pageflip-canvas" );
	var context = canvas.getContext( "2d" );
	
	var mouse = { x: 0, y: 0 };

	~

 
This JavaScript code provides the basic settings and event handling to achieve the page-flipping effect using the canvas element.

Pageflip: Demo Page with Page Flipping

You can view the actual demo page from the link below.
pageflip: Demo Page with Page Flipping

On this demo page, you can experience the page-flipping effect using pageflip.js.

Source: Implementation Example: 20thingsilearned.com’s Page Flipping Effect – HTML5 Rocks

Implementation Example: 20thingsilearned.com’s Page Flipping Effect – HTML5 Rocks

Conclusion

Using pageflip.js, you can easily implement a book-like page-flipping effect on your website. This library offers an interactive user experience and is particularly effective for viewing digital content. Refer to the actual demo page and try incorporating it into your projects.
By adding such interactive elements, you can attract visitors’ interest and increase their time on your site. Use pageflip.js to create an engaging website.

 
※ If you reuse this, please do so at your own risk.
 Please do not reuse the Google Analytics tag within the demo page tags.