第一步,安装v-viewer
npm install v-viewer --save
第二步,在main.js中配置全局变量:
import Viewer from 'v-viewer' import 'viewerjs/dist/viewer.css' Vue.use(Viewer) Viewer.setDefaults({ Options: { 'inline': true, 'button': true, 'navbar': true, 'title': true, 'toolbar': true, 'tooltip': true, 'movable': true, 'zoomable': true, 'rotatable': true, 'scalable': true, 'transition': true, 'fullscreen': true, 'keyboard': true, 'url': 'data-source' } })
/*
Viewer.setDefaults({
'inline':true,
'button':true, //右上角按钮
"navbar": true, //底部缩略图
"title": true, //当前图片标题
"toolbar": true, //底部工具栏
"tooltip": true, //显示缩放百分比
"movable": true, //是否可以移动
"zoomable": true, //是否可以缩放
"rotatable": true, //是否可旋转
"scalable": true, //是否可翻转
"transition": true, //使用 CSS3 过度
"fullscreen": true, //播放时是否全屏
"keyboard": true, //是否支持键盘
"url": "data-source",
ready: function (e) {
console.log(e.type,'组件以初始化');
},
show: function (e) {
console.log(e.type,'图片显示开始');
},
shown: function (e) {
console.log(e.type,'图片显示结束');
},
hide: function (e) {
console.log(e.type,'图片隐藏完成');
},
hidden: function (e) {
console.log(e.type,'图片隐藏结束');
},
view: function (e) {
console.log(e.type,'视图开始');
},
viewed: function (e) {
console.log(e.type,'视图结束');
// 索引为 1 的图片旋转20度
if(e.detail.index === 1){
this.viewer.rotate(20);
}
},
zoom: function (e) {
console.log(e.type,'图片缩放开始');
},
zoomed: function (e) {
console.log(e.type,'图片缩放结束');
}
})
*/
第三步,在vue文件中调用:
<template> <el-carousel height="150px"> <el-carousel-item v-for="item in imgList" :key="item" :autoplay="false"> <viewer style="height: 150px;"> <img :key="index" :src="item" class="item-card"> </viewer> </el-carousel-item> </el-carousel> </template> <script> export default { name: 'AttributeTable', data() { return { imgList: [ 'http://localhost:3000/static/img/login-background.bbdd7d12.png', 'http://localhost:3000/static/img/1.d9e9852f.jpg', ] } } } </script> <style lang="scss" scoped> .item-card { height: 150px; 100%; } </style>