v-viewer
用于图片浏览的Vue组件,支持旋转、缩放、翻转等操作,基于viewer.js。
有任何问题请到github项目页提交issue,博客的留言我可能无法及时看到,谢谢各位支持👍。
v-viewer for vue3
在线演示
使用示例
从0.x迁移
你需要做的唯一改动就是手动引入样式文件:
import 'viewerjs/dist/viewer.css'
import 'viewerjs/dist/viewer.css'
安装
使用npm命令安装
npm install v-viewer@legacy viewerjs
npm install v-viewer@legacy viewerjs
使用
引入v-viewer
及必需的css样式,并使用Vue.use()
注册插件,之后即可使用。
<template>
<div>
<!-- directive -->
<div class="images" v-viewer>
<img v-for="src in images" :key="src" :src="src">
</div>
<!-- component -->
<viewer :images="images">
<img v-for="src in images" :key="src" :src="src">
</viewer>
<!-- api -->
<button type="button" @click="show">Click to show</button>
</div>
</template>
<script>
import 'viewerjs/dist/viewer.css'
import VueViewer from 'v-viewer'
import Vue from 'vue'
Vue.use(VueViewer)
export default {
data() {
return {
images: [
"https://picsum.photos/200/200",
"https://picsum.photos/300/200",
"https://picsum.photos/250/200"
]
};
},
methods: {
show() {
this.$viewerApi({
images: this.images,
})
},
},
}
</script>
<template>
<div>
<!-- directive -->
<div class="images" v-viewer>
<img v-for="src in images" :key="src" :src="src">
</div>
<!-- component -->
<viewer :images="images">
<img v-for="src in images" :key="src" :src="src">
</viewer>
<!-- api -->
<button type="button" @click="show">Click to show</button>
</div>
</template>
<script>
import 'viewerjs/dist/viewer.css'
import VueViewer from 'v-viewer'
import Vue from 'vue'
Vue.use(VueViewer)
export default {
data() {
return {
images: [
"https://picsum.photos/200/200",
"https://picsum.photos/300/200",
"https://picsum.photos/250/200"
]
};
},
methods: {
show() {
this.$viewerApi({
images: this.images,
})
},
},
}
</script>
支持UMD方式引入
Browser
<link href="//unpkg.com/viewerjs/dist/viewer.css" rel="stylesheet">
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/viewerjs/dist/viewer.js"></script>
<script src="//unpkg.com/v-viewer/dist/v-viewer.js"></script>
<script>
Vue.use(VueViewer.default)
</script>
<link href="//unpkg.com/viewerjs/dist/viewer.css" rel="stylesheet">
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/viewerjs/dist/viewer.js"></script>
<script src="//unpkg.com/v-viewer/dist/v-viewer.js"></script>
<script>
Vue.use(VueViewer.default)
</script>
CommonJS
var VueViewer = require('VueViewer')
var VueViewer = require('VueViewer')
AMD
require(['VueViewer'], function (VueViewer) {});
require(['VueViewer'], function (VueViewer) {});
以指令形式使用
只需要将v-viewer
指令添加到任意元素即可,该元素下的所有img
元素都会被viewer
自动处理。
你可以像这样传入配置项: v-viewer="{inline: true}"
如果有必要,可以先用选择器查找到目标元素,然后可以用el.$viewer
来获取viewer
实例。
<template>
<div>
<div class="images" v-viewer="{movable: false}">
<img v-for="src in images" :src="src" :key="src">
</div>
<button type="button" @click="show">Show</button>
</div>
</template>
<script>
import 'viewerjs/dist/viewer.css'
import { directive as viewer } from "v-viewer"
export default {
directives: {
viewer: viewer({
debug: true,
}),
},
data() {
return {
images: [
"https://picsum.photos/200/200",
"https://picsum.photos/300/200",
"https://picsum.photos/250/200"
]
};
},
methods: {
show () {
const viewer = this.$el.querySelector('.images').$viewer
viewer.show()
}
}
}
</script>
<template>
<div>
<div class="images" v-viewer="{movable: false}">
<img v-for="src in images" :src="src" :key="src">
</div>
<button type="button" @click="show">Show</button>
</div>
</template>
<script>
import 'viewerjs/dist/viewer.css'
import { directive as viewer } from "v-viewer"
export default {
directives: {
viewer: viewer({
debug: true,
}),
},
data() {
return {
images: [
"https://picsum.photos/200/200",
"https://picsum.photos/300/200",
"https://picsum.photos/250/200"
]
};
},
methods: {
show () {
const viewer = this.$el.querySelector('.images').$viewer
viewer.show()
}
}
}
</script>
指令修饰器
static
添加修饰器后,viewer
的创建只会在元素绑定指令时执行一次。 如果你确定元素内的图片不会再发生变化,使用它可以避免不必要的重建动作。
<div class="images" v-viewer.static="{inline: true}">
<img v-for="src in images" :src="src" :key="src">
</div>
<div class="images" v-viewer.static="{inline: true}">
<img v-for="src in images" :src="src" :key="src">
</div>
rebuild
默认情况下当图片发生变更时(添加、删除或排序),viewer
实例会使用update
方法更新内容。
如果你遇到任何显示问题,尝试使用重建来代替更新。
<div class="images" v-viewer.rebuild="{inline: true}">
<img v-for="src in images" :src="src" :key="src">
</div>
<div class="images" v-viewer.rebuild="{inline: true}">
<img v-for="src in images" :src="src" :key="src">
</div>
以组件形式使用
你也可以单独引入全屏组件并局部注册它。
使用作用域插槽来定制你的图片展示方式。
<template>
<div>
<viewer :options="options" :images="images"
@inited="inited"
class="viewer" ref="viewer"
>
<template #default="scope">
<img v-for="src in scope.images" :src="src" :key="src">
{{scope.options}}
</template>
</viewer>
<button type="button" @click="show">Show</button>
</div>
</template>
<script>
import 'viewerjs/dist/viewer.css'
import { component as Viewer } from "v-viewer"
export default {
components: {
Viewer
},
data() {
return {
images: [
"https://picsum.photos/200/200",
"https://picsum.photos/300/200",
"https://picsum.photos/250/200"
]
};
},
methods: {
inited (viewer) {
this.$viewer = viewer
},
show () {
this.$viewer.show()
}
}
}
</script>
<template>
<div>
<viewer :options="options" :images="images"
@inited="inited"
class="viewer" ref="viewer"
>
<template #default="scope">
<img v-for="src in scope.images" :src="src" :key="src">
{{scope.options}}
</template>
</viewer>
<button type="button" @click="show">Show</button>
</div>
</template>
<script>
import 'viewerjs/dist/viewer.css'
import { component as Viewer } from "v-viewer"
export default {
components: {
Viewer
},
data() {
return {
images: [
"https://picsum.photos/200/200",
"https://picsum.photos/300/200",
"https://picsum.photos/250/200"
]
};
},
methods: {
inited (viewer) {
this.$viewer = viewer
},
show () {
this.$viewer.show()
}
}
}
</script>
组件属性
images
- 类型:
Array
trigger
- 类型:
Object
你可以使用trigger
来代替images
, 从而传入任何类型的数据。
当trigger
绑定的数据发生变更,组件就会自动更新。
<viewer :trigger="externallyGeneratedHtmlWithImages">
<div v-html="externallyGeneratedHtmlWithImages"/>
</viewer>
<viewer :trigger="externallyGeneratedHtmlWithImages">
<div v-html="externallyGeneratedHtmlWithImages"/>
</viewer>
rebuild
- 类型:
Boolean
- 默认值:
false
默认情况下当图片发生变更时(添加、删除或排序),viewer
实例会使用update
方法更新内容。
如果你遇到任何显示问题,尝试使用重建来代替更新。
<viewer
ref="viewer"
:options="options"
:images="images"
rebuild
class="viewer"
@inited="inited"
>
<template #default="scope">
<img v-for="src in scope.images" :src="src" :key="src">
{{scope.options}}
</template>
</viewer>
<viewer
ref="viewer"
:options="options"
:images="images"
rebuild
class="viewer"
@inited="inited"
>
<template #default="scope">
<img v-for="src in scope.images" :src="src" :key="src">
{{scope.options}}
</template>
</viewer>
组件事件
inited
- viewer:
Viewer
监听inited
事件来获取viewer
实例,或者也可以用this.refs.xxx.$viewer
这种方法。
以api形式使用
api形式只能使用modal模式。
你可以直接执行函数: this.$viewerApi({options: {}, images: []})
来展现画廊, 而不需要自己来渲染这些img
元素.
函数会返回对应的viewer实例.
<template>
<div>
<button type="button" class="button" @click="previewURL">URL Array</button>
<button type="button" class="button" @click="previewImgObject">Img-Object Array</button>
</div>
</template>
<script>
import 'viewerjs/dist/viewer.css'
import { api as viewerApi } from "v-viewer"
export default {
data() {
sourceImageURLs: [
'https://picsum.photos/200/200?random=1',
'https://picsum.photos/200/200?random=2',
],
sourceImageObjects: [
{
'src':'https://picsum.photos/200/200?random=3',
'data-source':'https://picsum.photos/800/800?random=3'
},
{
'src':'https://picsum.photos/200/200?random=4',
'data-source':'https://picsum.photos/800/800?random=4'
}
]
},
methods: {
previewURL () {
// 如果使用`app.use`进行全局安装, 你就可以像这样直接调用`this.$viewerApi`
const $viewer = this.$viewerApi({
images: this.sourceImageURLs
})
},
previewImgObject () {
// 或者你可以单独引入api然后执行它
const $viewer = viewerApi({
options: {
toolbar: true,
url: 'data-source',
initialViewIndex: 1
},
images: this.sourceImageObjects
})
}
}
}
</script>
<template>
<div>
<button type="button" class="button" @click="previewURL">URL Array</button>
<button type="button" class="button" @click="previewImgObject">Img-Object Array</button>
</div>
</template>
<script>
import 'viewerjs/dist/viewer.css'
import { api as viewerApi } from "v-viewer"
export default {
data() {
sourceImageURLs: [
'https://picsum.photos/200/200?random=1',
'https://picsum.photos/200/200?random=2',
],
sourceImageObjects: [
{
'src':'https://picsum.photos/200/200?random=3',
'data-source':'https://picsum.photos/800/800?random=3'
},
{
'src':'https://picsum.photos/200/200?random=4',
'data-source':'https://picsum.photos/800/800?random=4'
}
]
},
methods: {
previewURL () {
// 如果使用`app.use`进行全局安装, 你就可以像这样直接调用`this.$viewerApi`
const $viewer = this.$viewerApi({
images: this.sourceImageURLs
})
},
previewImgObject () {
// 或者你可以单独引入api然后执行它
const $viewer = viewerApi({
options: {
toolbar: true,
url: 'data-source',
initialViewIndex: 1
},
images: this.sourceImageObjects
})
}
}
}
</script>
Viewer的配置项 & 方法
请参考viewer.js.
插件配置项
name
- 类型:
String
- 默认值:
viewer
如果你需要避免重名冲突,可以像这样引入:
<template>
<div>
<div class="images" v-vuer="{movable: false}">
<img v-for="src in images" :src="src" :key="src">
</div>
<button type="button" @click="show">Show</button>
</div>
</template>
<script>
import 'viewerjs/dist/viewer.css'
import Vuer from 'v-viewer'
import Vue from 'vue'
Vue.use(Vuer, {name: 'vuer'})
export default {
data() {
return {
images: [
"https://picsum.photos/200/200",
"https://picsum.photos/300/200",
"https://picsum.photos/250/200"
]
};
},
methods: {
show () {
// viewerjs实例名称
const vuer = this.$el.querySelector('.images').$vuer
vuer.show()
// api名称
this.$vuerApi({
images: this.images
})
}
}
}
</script>
<template>
<div>
<div class="images" v-vuer="{movable: false}">
<img v-for="src in images" :src="src" :key="src">
</div>
<button type="button" @click="show">Show</button>
</div>
</template>
<script>
import 'viewerjs/dist/viewer.css'
import Vuer from 'v-viewer'
import Vue from 'vue'
Vue.use(Vuer, {name: 'vuer'})
export default {
data() {
return {
images: [
"https://picsum.photos/200/200",
"https://picsum.photos/300/200",
"https://picsum.photos/250/200"
]
};
},
methods: {
show () {
// viewerjs实例名称
const vuer = this.$el.querySelector('.images').$vuer
vuer.show()
// api名称
this.$vuerApi({
images: this.images
})
}
}
}
</script>
defaultOptions
- 类型:
Object
- 默认值:
undefined
如果你需要修改viewer.js的全局默认配置项,可以像这样引入:
import VueViewer from 'v-viewer'
import Vue from 'vue'
Vue.use(VueViewer, {
defaultOptions: {
zIndex: 9999
}
})
import VueViewer from 'v-viewer'
import Vue from 'vue'
Vue.use(VueViewer, {
defaultOptions: {
zIndex: 9999
}
})
你还可以在任何时候像这样修改全局默认配置项:
import VueViewer from 'v-viewer'
import Vue from 'vue'
Vue.use(VueViewer)
Viewer.setDefaults({
zIndexInline: 2017
})
import VueViewer from 'v-viewer'
import Vue from 'vue'
Vue.use(VueViewer)
Viewer.setDefaults({
zIndexInline: 2017
})