Vue使用epubjs电子书

npmjs: https://www.npmjs.com/package/epubjs

在线电子书转换器

安装:

javascript 复制代码
npm i epubjs

简单封装:

src/hooks/

javascript 复制代码
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 }
}

使用:

javascript 复制代码
<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下。

这篇只是简单写一下使用,还没有正式用到

更多可以参考另一位博主的文章:https://blog.csdn.net/qq_42484429/article/details/93158645?ops_request_misc=&request_id=&biz_id=102&utm_term=vue%E4%BD%BF%E7%94%A8epubjs&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-2-93158645.142^v96^pc_search_result_base9&spm=1018.2226.3001.4187

写的相对更详细一些,也可以看着源码调用对应方法来实现对应功能。

相关推荐
申阳6 分钟前
Day 7:05. 基于Nuxt开发博客项目-首页开发
前端·后端·程序员
Crystal32813 分钟前
App端用户每日弹出签到弹窗如何实现?(uniapp+Vue)
前端·vue.js
摸着石头过河的石头14 分钟前
Service Worker 深度解析:让你的 Web 应用离线也能飞
前端·javascript·性能优化
用户40993225021215 分钟前
Vue 3中watch侦听器的正确使用姿势你掌握了吗?深度监听、与watchEffect的差异及常见报错解析
前端·ai编程·trae
1024小神18 分钟前
Xcode 常用使用技巧说明,总有一个帮助你
前端
molly cheung20 分钟前
Vue3:watch与watchEffect的异同
vue.js·watch·store·watcheffect
政采云技术1 小时前
音视频通用组件设计探索和应用
前端·音视频开发
不爱吃糖的程序媛1 小时前
Electron 如何判断运行平台是鸿蒙系统(OpenHarmony)
javascript·electron·harmonyos
Hilaku1 小时前
我用AI重构了一段500行的屎山代码,这是我的Prompt和思考过程
前端·javascript·架构
Cxiaomu1 小时前
React Native App 自动检测版本更新完整实现指南
javascript·react native·react.js