一泡嘴、簡單藐唠、方便
<template>
<div>
// 第一種方式
<a href="https://mp-6b242e24-5f84-4668-95da-2c7804a288cd.cdn.bspapp.com/jl.pdf">第一種方法:預覽</a>
<hr>
// 第二種方式
<button @click="btn">點擊顯示</button>
<iframe v-if="isShow" src="https://mp-6b242e24-5f84-4668-95da-2c7804a288cd.cdn.bspapp.com/jl.pdf" frameborder="0" style="width: 100%; height: 1200px;"></iframe>
</div>
</template>
<script>
export default {
data() {
return {
isShow: false
}
},
methods: {
btn() {
this.isShow = true
}
}
}
</script>
<style lang="scss" scoped></style>
二、插件方式
父組件
<template>
<div class="container">
<excelView />
</div>
</template>
<script>
import excelView from "./components/pdf"
export default {
components: {
excelView
}
}
</script>
<style scoped>
.container {
font-family: PingFang SC;
width: 100%;
height: 500px;
}
</style>
子組件
<template>
<div id="container">
<!-- 上一頁豌研、下一頁 -->
<div class="right-btn">
<!-- 輸入頁碼 -->
<div class="pageNum">
<input
v-model.number="currentPage"
type="number"
class="inputNumber"
@input="inputEvent()"> / {{pageCount}}
</div>
<div
@click="changePdfPage('first')"
class="turn">
首頁
</div>
<!-- 在按鈕不符合條件時禁用 -->
<div @click="changePdfPage('pre')"
class="turn-btn"
:style="currentPage===1?'cursor: not-allowed;':''">
上一頁
</div>
<div @click="changePdfPage('next')"
class="turn-btn"
:style="currentPage===pageCount?'cursor: not-allowed;':''">
下一頁
</div>
<div @click="changePdfPage('last')"
class="turn">
尾頁
</div>
</div>
<div class="pdfArea">
<pdf :src="src"
ref="pdf"
v-show="loadedRatio===1"
:page="currentPage"
@num-pages="pageCount=$event"
@progress="loadedRatio = $event"
@page-loaded="currentPage=$event"
@loaded="loadPdfHandler"
@link-clicked="currentPage = $event"
style="display: inline-block;width:100%"
id="pdfID"></pdf>
</div>
<!-- 加載未完成時哮幢,展示進度條組件并計算進度 -->
<div class="progress"
v-show="loadedRatio!==1">
<el-progress type="circle"
:width="70"
color="#53a7ff"
:percentage="Math.floor(loadedRatio * 100)"></el-progress>
<br>
<!-- 加載提示語 -->
<span>{{remindShow}}</span>
</div>
</div>
</template>
<script>
import pdf from 'vue-pdf'
export default {
components: {
pdf
},
computed: {
},
created () {
this.prohibit()
},
destroyed () {
// 在頁面銷毀時記得清空 setInterval
clearInterval(this.intervalID)
},
mounted () {
// 更改 loading 文字
this.intervalID = setInterval(() => {
this.remindShow === this.remindText.refresh
? this.remindShow = this.remindText.loading
: this.remindShow = this.remindText.refresh
}, 4000)
// 監(jiān)聽滾動條事件
this.listenerFunction()
},
data () {
return {
// ----- loading -----
remindText: {
loading: '加載文件中,文件較大請耐心等待...',
refresh: '若卡住不動扇救,可刷新頁面重新加載...'
},
remindShow: '加載文件中刑枝,文件較大請耐心等待...',
intervalID: '',
// ----- vuepdf -----
// src靜態(tài)路徑: /static/xxx.pdf
// src服務器路徑: 'http://.../xxx.pdf'
src: 'https://xiaoyong.fun/luota.pdf',
// 當前頁數(shù)
currentPage: 0,
// 總頁數(shù)
pageCount: 0,
// 加載進度
loadedRatio: 0
}
},
methods: {
// 監(jiān)聽滾動條事件
listenerFunction (e) {
document.getElementById('container').addEventListener('scroll', true)
},
// 頁面回到頂部
toTop () {
document.getElementById('container').scrollTop = 0
},
// 輸入頁碼時校驗
inputEvent () {
if (this.currentPage > this.pageCount) {
// 1. 大于max
this.currentPage = this.pageCount
} else if (this.currentPage < 1) {
// 2. 小于min
this.currentPage = 1
}
},
// 切換頁數(shù)
changePdfPage (val) {
if (val === 'pre' && this.currentPage > 1) {
// 切換后頁面回到頂部
this.currentPage--
this.toTop()
} else if (val === 'next' && this.currentPage < this.pageCount) {
this.currentPage++
this.toTop()
} else if (val === 'first') {
this.currentPage = 1
this.toTop()
} else if (val === 'last' && this.currentPage < this.pageCount) {
this.currentPage = this.pageCount
this.toTop()
}
},
// pdf加載時
loadPdfHandler (e) {
// 加載的時候先加載第一頁
this.currentPage = 1
},
// 禁用鼠標右擊、F12 來禁止打印和打開調試工具
prohibit () {
// console.log(document)
document.oncontextmenu = function () {
return false
}
document.onkeydown = function (e) {
if (e.ctrlKey && (e.keyCode === 65 || e.keyCode === 67 || e.keyCode === 73 || e.keyCode === 74 || e.keyCode === 80 || e.keyCode === 83 || e.keyCode === 85 || e.keyCode === 86 || e.keyCode === 117)) {
return false
}
if (e.keyCode === 18 || e.keyCode === 123) {
return false
}
}
}
}
}
</script>
<style scoped>
#container {
overflow: auto;
height: 800px;
font-family: PingFang SC;
width: 100%;
display: flex;
/* justify-content: center; */
position: relative;
}
/* 右側功能按鈕區(qū) */
.right-btn {
position: fixed;
right: 5%;
bottom: 15%;
width: 120px;
display: flex;
flex-wrap: wrap;
justify-content: center;
z-index: 99;
}
.pdfArea {
width: 80%;
}
/* ------------------- 輸入頁碼 ------------------- */
.pageNum {
margin: 10px 0;
font-size: 18px;
}
/*在谷歌下移除input[number]的上下箭頭*/
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none !important;
margin: 0;
}
/*在firefox下移除input[number]的上下箭頭*/
input[type='number'] {
-moz-appearance: textfield;
}
.inputNumber {
border-radius: 8px;
border: 1px solid #999999;
height: 35px;
font-size: 18px;
width: 60px;
text-align: center;
}
.inputNumber:focus {
border: 1px solid #00aeff;
background-color: rgba(18, 163, 230, 0.096);
outline: none;
transition: 0.2s;
}
/* ------------------- 切換頁碼 ------------------- */
.turn {
background-color: #888888;
opacity: 0.7;
color: #ffffff;
height: 70px;
width: 70px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 5px 0;
}
.turn-btn {
background-color: #000000;
opacity: 0.6;
color: #ffffff;
height: 70px;
width: 70px;
border-radius: 50%;
margin: 5px 0;
display: flex;
align-items: center;
justify-content: center;
}
.turn-btn:hover,
.turn:hover {
transition: 0.3s;
opacity: 0.5;
cursor: pointer;
}
/* ------------------- 進度條 ------------------- */
.progress {
position: absolute;
right: 50%;
top: 50%;
text-align: center;
}
.progress > span {
color: #199edb;
font-size: 14px;
}
</style>
簡單方便的標簽展示
c33c022144ba477dff3bcc0fc767b2e.png
插件展示圖片
df788c0ca3ec0a256bbc1e20c66260e.png