接到一个需求,实现多张图片和PDF展示,并且有放大效果
首先把tab切换封装成组件
<template>
<div class="bos">
<div class="left">
<div class="li" v-for="(item,index) in list" :class="value == item.value? 'active' : ''" :key="index" @click="getValue(item.value)">{{item.label}}</div>
</div>
</div>
</template>
<script>
export default {
props: {
list: {
type: Array,
default: () => []
},
value: {
default: ''
}
},
model: {
prop: "value",
event: 'getValue'
},
methods: {
getValue (val) {
this.$emit('getValue', val)
}
}
}
</script>
<style lang="less" scoped>
.bos {
display: flex;
height: 60px;
border-bottom: 1px solid #ccc;
margin-bottom: 20px;
align-items: flex-end;
}
.left {
display: flex;
padding-left: 20px;
.li {
margin-right: 50px;
height: 40px;
line-height: 40px;
cursor: pointer;
list-style: none;
}
}
.active {
position: relative;
&::after {
width: 100%;
height: 3px;
background: skyblue;
position: absolute;
bottom: 0;
left: 0;
content: '';
}
}
</style>
父组件中引入tab切换组件
<template>
<div class="box">
<GlobalTableTabs v-model="num" :list='list' />
<div class="imagereview" v-if="num == 1">
<div v-if="img.length>0">
<el-image class="Item" v-for="(item,index) in img" :key="index" :src="item.filePath" lazy @click="showImgViewer(index)" />
</div>
<el-image-viewer v-if="imgViewerVisible" :on-close="closeviewer" :url-list="imgList" class="el-image-viewer" />
</div>
<div class="imagereview" v-if="num == 2">
<div v-if="pdf.length>0">
<el-image class="Item" v-for="(item,index) in pdf" :key="index" :src="item.thumbnailPath" lazy @click="imagClick(index)" />
</div>
</div>
<el-dialog title="PDF预览" :visible.sync="dialogVisible" width="420px" style="margin-top: 1px;margin-left: 0px;" center :modal="false" :close-on-click-modal="false">
<iframe :src="pdfSrc" frameborder="0" scrolling="auto" class="iframe"></iframe>
</el-dialog>
</div>
</template>
<script>
import img0 from '../assets/0.png'
import img1 from '../assets/111.png'
import img2 from '../assets/2.png'
import elImageViewer from 'element-ui/packages/image/src/image-viewer'
export default {
data () {
return {
list,
num: '1',
img: [
{ filePath: img0 },
{ filePath: img1 },
{ filePath: img2 }
],
imgList: [],
imgViewerVisible: false,
pdf: [
{ thumbnailPath: img1, filePath: 'https://cancer-research.oss-cn-beijing.aliyuncs.com/yuance-platform-oss/e05b2f02d73b43ecbaa54e5d00c09d14.pdf' },
{ thumbnailPath: img2, filePath: 'https://cancer-research.oss-cn-beijing.aliyuncs.com/yuance-platform-oss/e05b2f02d73b43ecbaa54e5d00c09d14.pdf' },
{ thumbnailPath: img0, filePath: 'https://cancer-research.oss-cn-beijing.aliyuncs.com/yuance-platform-oss/e05b2f02d73b43ecbaa54e5d00c09d14.pdf' }
],
pdfSrc: [],
dialogVisible: false
}
},
methods: {
showImgViewer (index) {
this.imgViewerVisible = true;
this.imgList = []
this.imgList.push(this.img[index].filePath)
const m = (e) => { e.preventDefault() };
document.body.style.overflow = 'hidden';
document.addEventListener("touchmove", m, false); // 禁止页面滑动
},
// 关闭查看器
closeviewer () {
this.imgViewerVisible = false;
const m = (e) => { e.preventDefault() };
document.body.style.overflow = 'auto';
document.removeEventListener("touchmove", m, true);
},
imagClick (index) {
this.pdfSrc = this.pdf[index].filePath
this.dialogVisible = true
},
},
components: {
elImageViewer
}
}
let list = [
{ label: '上', value: '1' },
{ label: '下', value: '2' }
]
</script>
<style lang="less" scoped>
.box {
width: 420px;
border-radius: 6px;
background: #fff;
margin-left: 20px;
overflow: auto;
}
.imagereview {
height: 600px;
overflow: auto;
}
.Item {
width: 380px;
background-size: 100% 100%;
}
.el-image-viewer {
width: 420px;
margin-left: 28px;
margin-top: 90px;
height: calc(100% - 280px);
overflow: hidden;
}
.iframe {
width: 100%;
height: 600px;
}
/deep/.el-dialog {
margin-left: 28px;
top: -60px;
}
</style>