Vue使用epubjs电子书的教程详解
npmjs: https://www.npmjs.com/package/epubjs
安装
npm i epubjs
简单封装
src/hooks/
import Epub from "epubjs";
import type { Book, Rendition } from 'epubjs'
import type { BookOptions } from 'epubjs/types/book'
import type { RenditionOptions } from 'epubjs/types/rendition'
export function useEpub() {
let book: Book
let rendition: Rendition
function createBook(urlOrData?: string | ArrayBuffer, options?: BookOptions) {
if(!urlOrData) {
book = Epub(options)
} else {
book = Epub(urlOrData, options)
}
return book
}
function render(element: Element | string, options?: RenditionOptions) {
if(!book) return
if(typeof element === 'string') {
rendition = book.renderTo(element, options)
} else {
rendition = book.renderTo(element, options)
}
return rendition
}
function display() {
if(!rendition) return
return rendition.display()
}
function getBook() {
return book
}
function getRendition() {
return rendition
}
return { createBook, render, display, getBook, getRendition }
}使用
<template>
<div class="main">
<div id="epub"></div>
<div class="btn">
<button @click="pre">pre</button>
<button @click="next">next</button>
</div>
</div>
</template>
<script setup lang="ts">
import { useEpub } from '../hooks'
import { onMounted } from 'vue'
const { createBook, render, display, getRendition } = useEpub()
onMounted(() => {
createBook('static/example.epub')
render('epub', { width: '100%', height: '100%' })
display()
})
// 上一页
const pre = async () => {
await getRendition().prev()
}
// 下一页
const next = async () => {
await getRendition().next()
}
</script>
<style scoped>
.main {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.main > #epub {
width: 500px;
height: 500px;
border: 1px dashed red;
box-sizing: border-box;
}
.main .btn {
display: flex;
justify-content: space-between;
margin-top: 5px;
}
.main .btn button {
padding: 7px 15px;
margin-left: 20px;
}
</style>这个示例电子书地址:https://example-files.online-convert.com/ebook/epub/example.epub
直接下载下来,放在public/static下。
这篇只是简单写一下使用,还没有正式用到
更多可以参考文章:vue+epubjs实现电子书阅读器的基本功能
写的相对更详细一些,也可以看着源码调用对应方法来实现对应功能。
到此这篇关于Vue使用epubjs电子书的教程详解的文章就介绍到这了,更多相关Vue epubjs电子书内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
vue利用vue meta info设置每个页面的title与meta信息
这篇文章主要给大家介绍了关于vue如何利用vue meta info设置每个页面的title与meta信息的相关资料,文中将实现的方法介绍的非常详细,对大家学习或者使用vue具有一定的参考学习价值,需要的朋友可以参考下2021-10-10
vue3+ts数组去重方及reactive/ref响应式显示流程分析
这篇文章主要介绍了vue3+ts数组去重方法-reactive/ref响应式显示,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-04-04


最新评论