-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathepub.html
95 lines (88 loc) · 2.81 KB
/
epub.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>EPUB阅读器</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/epubjs/dist/epub.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
}
#viewer {
width: 80%;
height: 600px;
margin: 20px auto;
border: 1px solid #ccc;
}
#controls {
text-align: center;
margin-top: 20px;
}
button {
margin: 0 10px;
padding: 10px 20px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>EPUB阅读器</h1>
<input type="file" id="file-input" accept=".epub">
<div id="viewer"></div>
<div id="controls">
<button id="prev">上一页</button>
<button id="next">下一页</button>
</div>
<script>
const fileInput = document.getElementById('file-input');
const viewer = document.getElementById('viewer');
const prevButton = document.getElementById('prev');
const nextButton = document.getElementById('next');
let currentRendition = null;
fileInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file.type !== 'application/epub+zip') {
alert('请选择EPUB文件');
return;
}
const reader = new FileReader();
reader.onload = (e) => {
const bookData = e.target.result;
const book = ePub(bookData);
currentRendition = book.renderTo('viewer', {
width: '100%',
height: '100%',
spread: 'always'
});
currentRendition.display();
// 添加键盘事件监听
document.addEventListener('keyup', handleKeyPress);
};
reader.readAsArrayBuffer(file);
});
prevButton.addEventListener('click', () => {
if (currentRendition) {
currentRendition.prev();
}
});
nextButton.addEventListener('click', () => {
if (currentRendition) {
currentRendition.next();
}
});
function handleKeyPress(e) {
if (currentRendition) {
if (e.key === 'ArrowLeft') {
currentRendition.prev();
} else if (e.key === 'ArrowRight') {
currentRendition.next();
}
}
}
</script>
</body>
</html>