Sure! Below is a comprehensive implementation of an interactive online book with a realistic page-flipping effect using HTML, CSS, and JavaScript. This solution leverages the Turn.js library, which simplifies creating a flipbook effect.
Note: Turn.js might require a license for commercial use. For this example, we'll use the free version suitable for non-commercial purposes.
Step 1: Project Structure
Ensure your project directory is structured as follows:
your-project/
│
├── index.html
├── css/
│ └── styles.css
├── js/
│ └── script.js
├── image/
│ ├── page1.jpg
│ ├── page2.jpg
│ ├── page3.jpg
│ └── ... (add as many pages as you like)
└── lib/
├── turn.min.js
└── jquery.min.js
- index.html: The main HTML file.
- css/styles.css: Custom CSS styles.
- js/script.js: Custom JavaScript code.
- image/: Directory containing all your page images (
page1.jpg, page2.jpg, etc.).
- lib/: Directory containing Turn.js and jQuery libraries.
Step 2: Including Libraries
First, download the necessary libraries:
- jQuery: Download from https://code.jquery.com/jquery-3.6.0.min.js.
- Turn.js: Download from http://www.turnjs.com/ or use a CDN if available.
Place both jquery.min.js and turn.min.js inside the lib/ directory.
Step 3: HTML Structure (index.html)
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<title>Quyển Sách Trực Tuyến</title>
<link rel="stylesheet" href="css/styles.css">
<!-- Include jQuery -->
<script src="lib/jquery.min.js"></script>
<!-- Include Turn.js -->
<script src="lib/turn.min.js"></script>
</head>
<body>
<div class="book-container">
<div id="flipbook">
<!-- Pages -->
<div class="hard">Trang Bìa</div>
<div><img src="image/page1.jpg" alt="Page 1"></div>
<div><img src="image/page2.jpg" alt="Page 2"></div>
<div><img src="image/page3.jpg" alt="Page 3"></div>
<!-- Add more pages as needed -->
<div class="hard">Trang Cuối</div>
</div>
<!-- Navigation Buttons -->
<button id="prevBtn" class="nav-btn">Trước</button>
<button id="nextBtn" class="nav-btn">Tiếp</button>
</div>
<script src="js/script.js"></script>
</body>
</html>
Step 4: CSS Styling (css/styles.css)
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
}
.book-container {
position: relative;
width: 800px; /* Adjust as needed */
height: 600px; /* Adjust as needed */
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
background-color: #fff;
}
#flipbook {
width: 100%;
height: 100%;
}
#flipbook .hard {
background-color: #8e44ad;
color: white;
font-size: 24px;
display: flex;
justify-content: center;
align-items: center;
}
#flipbook .page img {
width: 100%;
height: 100%;
object-fit: cover;
}
/* Navigation Buttons */
.nav-btn {
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(142, 68, 173, 0.8);
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
z-index: 1000;
font-size: 16px;
border-radius: 4px;
}
#prevBtn {
left: 10px;
}
#nextBtn {
right: 10px;
}
.nav-btn:hover {
background-color: rgba(142, 68, 173, 1);
}
/* Responsive Design for Mobile */
@media (max-width: 900px) {
.book-container {
width: 100%;
height: auto;
}
#flipbook {
height: auto;
}
.nav-btn {
display: none; /* Hide buttons on mobile */
}
}
Step 5: JavaScript Functionality (js/script.js)
$(document).ready(function() {
// Initialize the flipbook
$("#flipbook").turn({
width: 800,
height: 600,
autoCenter: true,
duration: 1000,
gradients: true,
elevation: 50,
when: {
turning: function(event, page, view) {
// Optional: Add any custom behavior before turning pages
},
turned: function(event, page, view) {
// Optional: Add any custom behavior after turning pages
}
}
});
// Navigation Buttons
$("#nextBtn").click(function() {
$("#flipbook").turn("next");
});
$("#prevBtn").click(function() {
$("#flipbook").turn("previous");
});
// Keyboard Navigation
$(document).keydown(function(e) {
if (e.which == 37) { // Left arrow
$("#flipbook").turn("previous");
} else if (e.which == 39) { // Right arrow
$("#flipbook").turn("next");
}
});
// Responsive: Adjust flipbook size on window resize
function resizeFlipbook() {
var containerWidth = $(".book-container").width();
var aspectRatio = 800 / 600; // Original aspect ratio
var newWidth = containerWidth;
var newHeight = containerWidth / aspectRatio;
$("#flipbook").turn("size", newWidth, newHeight);
}
$(window).on('resize', function(){
resizeFlipbook();
});
// Initial resize
resizeFlipbook();
});
Step 6: Adding Swipe Functionality for Mobile
Turn.js inherently supports touch gestures for swiping on mobile devices. However, to enhance the experience or ensure compatibility, you can add additional touch event listeners if needed. For this example, Turn.js should handle swipe gestures out of the box, allowing users to swipe left or right to turn pages on touch-enabled devices.
Step 7: Adding Your Images
Place all your page images inside the image/ directory. Ensure they are named sequentially (e.g., page1.jpg, page2.jpg, etc.) and referenced correctly in the HTML.
Final Notes
- Page Sizes: Adjust the
width and height in both the CSS and JavaScript to match the aspect ratio of your images. The example uses 800x600 pixels.
- Hard Pages: The first and last pages (
Trang Bìa and Trang Cuối) have the class hard to differentiate them as covers. You can style them differently if desired.
- Responsive Design: The CSS includes basic responsiveness. You can enhance it further based on your requirements.
- Additional Pages: To add more pages, simply duplicate the
<div><img src="image/pageX.jpg" alt="Page X"></div> block within the #flipbook container.
- Libraries via CDN (Optional): If you prefer not to host the libraries locally, you can use CDN links. Here's how you can modify the
<head> section:
<!-- Include jQuery via CDN -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Include Turn.js via CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/turn.js/4.1.0/turn.min.js"></script>
Replace the local script references with the CDN links accordingly.
Complete Example
For convenience, here's the complete index.html with embedded CSS and JS. However, it's recommended to separate them as shown above for better organization.
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<title>Quyển Sách Trực Tuyến</title>
<style>
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
}
.book-container {
position: relative;
width: 800px; /* Adjust as needed */
height: 600px; /* Adjust as needed */
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
background-color: #fff;
}
#flipbook {
width: 100%;
height: 100%;
}
#flipbook .hard {
background-color: #8e44ad;
color: white;
font-size: 24px;
display: flex;
justify-content: center;
align-items: center;
}
#flipbook .page img {
width: 100%;
height: 100%;
object-fit: cover;
}
/* Navigation Buttons */
.nav-btn {
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(142, 68, 173, 0.8);
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
z-index: 1000;
font-size: 16px;
border-radius: 4px;
}
#prevBtn {
left: 10px;
}
#nextBtn {
right: 10px;
}
.nav-btn:hover {
background-color: rgba(142, 68, 173, 1);
}
/* Responsive Design for Mobile */
@media (max-width: 900px) {
.book-container {
width: 100%;
height: auto;
}
#flipbook {
height: auto;
}
.nav-btn {
display: none; /* Hide buttons on mobile */
}
}
</style>
<!-- Include jQuery via CDN -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<!-- Include Turn.js via CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/turn.js/4.1.0/turn.min.js"></script>
</head>
<body>
<div class="book-container">
<div id="flipbook">
<!-- Pages -->
<div class="hard">Trang Bìa</div>
<div><img src="image/page1.jpg" alt="Page 1"></div>
<div><img src="image/page2.jpg" alt="Page 2"></div>
<div><img src="image/page3.jpg" alt="Page 3"></div>
<!-- Add more pages as needed -->
<div class="hard">Trang Cuối</div>
</div>
<!-- Navigation Buttons -->
<button id="prevBtn" class="nav-btn">Trước</button>
<button id="nextBtn" class="nav-btn">Tiếp</button>
</div>
<script>
$(document).ready(function() {
// Initialize the flipbook
$("#flipbook").turn({
width: 800,
height: 600,
autoCenter: true,
duration: 1000,
gradients: true,
elevation: 50,
when: {
turning: function(event, page, view) {
// Optional: Add any custom behavior before turning pages
},
turned: function(event, page, view) {
// Optional: Add any custom behavior after turning pages
}
}
});
// Navigation Buttons
$("#nextBtn").click(function() {
$("#flipbook").turn("next");
});
$("#prevBtn").click(function() {
$("#flipbook").turn("previous");
});
// Keyboard Navigation
$(document).keydown(function(e) {
if (e.which == 37) { // Left arrow
$("#flipbook").turn("previous");
} else if (e.which == 39) { // Right arrow
$("#flipbook").turn("next");
}
});
// Responsive: Adjust flipbook size on window resize
function resizeFlipbook() {
var containerWidth = $(".book-container").width();
var aspectRatio = 800 / 600; // Original aspect ratio
var newWidth = containerWidth;
var newHeight = containerWidth / aspectRatio;
$("#flipbook").turn("size", newWidth, newHeight);
}
$(window).on('resize', function(){
resizeFlipbook();
});
// Initial resize
resizeFlipbook();
});
</script>
</body>
</html>
Conclusion
This implementation provides a user-friendly online flipbook with page-turning effects suitable for both desktop and mobile devices. Users can navigate through the book using navigation buttons, keyboard arrows, or swipe gestures on touch-enabled devices. Customize the styles and functionality further to match your specific requirements.
Feel free to expand upon this foundation by adding features such as dynamic loading of images, adding table of contents, or integrating annotations.