v-viewer
is a Vue component used for image browsing. It supports operations such as rotation, scaling, and flipping. It is based on viewer.js.
Chinese document: Vue image browsing component v-viewer, supports rotation, scaling, flipping and other operations | Mirari's Blog
Code example: https://mirari.cc/v-viewer/
It is relatively simple to use the v-viewer plug-in to implement the image preview function in Vue.js 2. v-viewer is a Vue.js image preview plug-in that can easily realize functions such as zooming in, zooming out, and sliding preview of images. Here are the implementation steps:
Install the v-viewer plug-in:
Use npm or yarn to install the v-viewer plug-in in the project directory.
npm install v-viewer --save
npm i -S viewerjs
# 或
yarn add v-viewer
yarn add viewerjs
Introduce and configure the v-viewer plug-in in the main.js file:
这行放在:import App from './App.vue'; 之前
import Viewer from 'v-viewer';
import 'viewerjs/dist/viewer.css';
Vue.use(Viewer);
或者
Vue.use(Viewer, {
defaultOptions: {
zIndex: 9999, // 设置图片预览组件的层级,确保能在其他组件之上
},
});
Use the v-viewer directive in components that need to preview images:
<template>
<div>
<!-- 点击图片触发预览 -->
<img v-for="(image, index) in imageList" :key="index" :src="image" v-viewer />
</div>
</template>
<script>
export default {
data() {
return {
imageList: [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
// 添加更多图片链接
],
};
},
};
</script
You can also use the following method
<template>
<div>
<button type="button" class="button" @click="previewURL">URL Array</button>
</div>
</template>
<script>
export default {
data() {
sourceImageURLs: [
'https://picsum.photos/200/200?random=1',
'https://picsum.photos/200/200?random=2',
],
},
methods: {
previewURL () {
// 如果使用`app.use`进行全局安装, 你就可以像这样直接调用`this.$viewerApi`
const $viewer = this.$viewerApi({
images: this.sourceImageURLs
});
},
},
};
</script>
In the above code, we apply the v-viewer directive to the img tag, so that the preview effect will be triggered when the image is clicked.