Vue使用epubjs電子書的教程詳解
npmjs: https://www.npmjs.com/package/epubjs
安裝
npm i epubjs
簡(jiǎn)單封裝
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>這個(gè)示例電子書地址:https://example-files.online-convert.com/ebook/epub/example.epub
直接下載下來,放在public/static下。
這篇只是簡(jiǎn)單寫一下使用,還沒有正式用到
更多可以參考文章:vue+epubjs實(shí)現(xiàn)電子書閱讀器的基本功能
寫的相對(duì)更詳細(xì)一些,也可以看著源碼調(diào)用對(duì)應(yīng)方法來實(shí)現(xiàn)對(duì)應(yīng)功能。
到此這篇關(guān)于Vue使用epubjs電子書的教程詳解的文章就介紹到這了,更多相關(guān)Vue epubjs電子書內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue項(xiàng)目webpack中Npm傳遞參數(shù)配置不同域名接口
這篇文章主要介紹了vue項(xiàng)目webpack中Npm傳遞參數(shù)配置不同域名接口,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
vue項(xiàng)目中按需引入element-ui的正確實(shí)現(xiàn)方法
這篇文章主要介紹了vue項(xiàng)目中按需引入element-ui的正確實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
vue?element表格某一列內(nèi)容過多,超出省略號(hào)顯示的實(shí)現(xiàn)
這篇文章主要介紹了vue?element表格某一列內(nèi)容過多,超出省略號(hào)顯示的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Vue3+TS實(shí)現(xiàn)語音播放組件的示例代碼
這篇文章主要介紹了如何利用Vue+TS實(shí)現(xiàn)一個(gè)簡(jiǎn)易的語音播放組件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定的幫助,需要的可以參考一下2022-03-03
Vue.extend實(shí)現(xiàn)組件庫message組件示例詳解
這篇文章主要為大家介紹了Vue.extend實(shí)現(xiàn)組件庫message組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07

