当前位置: 首页 > news >正文

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下。

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

更多可以参考另一位博主的文章: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

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

相关文章:

  • docker部署elk
  • Node.js 中解析 HTML 的方法介绍
  • 月报总结|Moonbeam 10月份大事一览
  • 网际控制报文协议ICMP
  • Spring Boot整合Swagger
  • 【SOC基础】单片机学习案例汇总 Part2:蜂鸣器、数码管显示
  • 【C++心愿便利店】No.11---C++之string语法指南
  • Vscode LinuxC++环境配置
  • ElasticSearch集群架构实战及其原理剖析
  • 好物周刊#29:项目管理软件
  • 概念解析 | Retinex理论
  • 玩了一下 Jenkins,最新版本 + JDK11
  • 项目实战:分页功能实战
  • Java 正则表达式数字篇
  • 1047. 删除字符串中的所有相邻重复项
  • 线性代数 第二章 矩阵
  • MyBatis 动态 SQL
  • 数据结构——基于顺序表实现通讯录
  • Kafka基本原理、生产问题总结及性能优化实践 | 京东云技术团队
  • YOLO算法改进6【中阶改进篇】:depthwise separable convolution轻量化C3
  • 哪都“差一点”的《歌手2025》,还能爆吗?
  • 保利、北京建工联合体45.45亿元竞得北京海淀区宅地,溢价率11.95%
  • 东南亚五大经济体一季度增长放缓,美国关税大棒或阻全年增长
  • 印尼总统20年来首次访泰:建立战略伙伴关系,加强打击网络诈骗等合作
  • 广州某科技公司遭网络攻击,境外“黑手”被锁定
  • 牛市早报|年内首次存款利率下调启动,5月LPR今公布