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>這個(gè)示例電子書地址:https://example-files.online-convert.com/ebook/epub/example.epub
直接下載下來,放在public/static下。
這篇只是簡單寫一下使用,還沒有正式用到
更多可以參考文章: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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue自定義組件實(shí)現(xiàn)?v-model?的幾種方式
在?Vue?中,v-model?是一個(gè)常用的指令,用于實(shí)現(xiàn)表單元素和組件之間的雙向綁定,當(dāng)我們使用原生的表單元素時(shí),直接使用?v-model?是很方便的,本文給大家介紹了Vue自定義組件實(shí)現(xiàn)?v-model?的幾種方式,需要的朋友可以參考下2024-02-02
vue利用vue meta info設(shè)置每個(gè)頁面的title與meta信息
這篇文章主要給大家介紹了關(guān)于vue如何利用vue meta info設(shè)置每個(gè)頁面的title與meta信息的相關(guān)資料,文中將實(shí)現(xiàn)的方法介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-10-10
vue3+ts數(shù)組去重方及reactive/ref響應(yīng)式顯示流程分析
這篇文章主要介紹了vue3+ts數(shù)組去重方法-reactive/ref響應(yīng)式顯示,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
vue項(xiàng)目中頁面底部出現(xiàn)白邊及空白區(qū)域錯(cuò)誤的問題
這篇文章主要介紹了vue項(xiàng)目中頁面底部出現(xiàn)白邊及空白區(qū)域錯(cuò)誤的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue-cli基礎(chǔ)配置及webpack配置修改的完整步驟
這篇文章主要給大家介紹了關(guān)于vue-cli基礎(chǔ)配置及webpack配置修改的完整步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用vue-cli具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
vue通過element樹形控件實(shí)現(xiàn)樹形表格
這篇文章主要為大家介紹了vue?element樹形控件實(shí)現(xiàn)樹形表格,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11

