Vue2 中使用圖片查看器 v-viewer的方法
先看效果:

插件簡(jiǎn)介
基于 viewer.js 插件,用于 Vue 的圖像查看器組件
官網(wǎng)地址:v-viewer
下載安裝
通過(guò) npm 安裝:
npm install v-viewer
GitHub 下載地址:https://github.com/mirari/v-viewer
UMD 用法
Browser:
<link href="viewerjs/viewer.css" rel="external nofollow" rel="stylesheet"> <script src="vue/vue.js"></script> <script src="viewerjs/viewer.js"></script> <script src="viewerjs/v-viewer.js"></script> <!-- 指令形式 --> <div class="images" v-viewer> <img src="1.jpg"> <img src="2.jpg"> </div> <!-- 組件形式 --> <viewer :images="images"> <img v-for="src in images" :src="src" :key="src"> </viewer> <script> Vue.use(VueViewer.default) </script>
CommonJS:
var VueViewer = require('VueViewer');AMD:
require(['VueViewer'], function (VueViewer) {});Vue 指令形式用法
只需要將 v-viewer 指令添加到任意元素即可,該元素下的所有 img 元素都會(huì)被 viewer 自動(dòng)處理。
可以傳入 options 配置項(xiàng):v-viewer="{inline: true}"
可以先用選擇器查找到目標(biāo)元素,然后用 el.$viewer 來(lái)獲取 viewer 實(shí)例。
<template>
<div id="app">
<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 Viewer from 'v-viewer'
import Vue from 'vue'
Vue.use(Viewer)
export default {
data() {
images: ['1.jpg', '2.jpg']
},
methods: {
show () {
const viewer = this.$el.querySelector('.images').$viewer
viewer.show()
}
}
}
</script>指令修飾器:static
添加修飾器后臺(tái),viewer 的創(chuàng)建只會(huì)在元素綁定指令時(shí)執(zhí)行一次。
如果你確定元素內(nèi)的圖片不會(huì)再發(fā)生變化,使用它可以避免不必要的重建動(dòng)作。
<div class="images" v-viewer.static="{inline: true}">
<img v-for="src in images" :src="src" :key="src">
</div>Vue 組件形式用法
你也可以單獨(dú)引入全屏組件并局部注冊(cè)它。
使用 作用域插槽 來(lái)定制你的圖片展示方式。
監(jiān)聽(tīng) inited 事件來(lái)獲取 viewer 實(shí)例,或者也可以用 this.refs.xxx.$viewer 這種方法。
<template>
<div id="app">
<viewer :options="options" :images="images"
@inited="inited" class="viewer" ref="viewer">
<template scope="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 Viewer from "v-viewer/src/component.vue"
export default {
components: {
Viewer
},
data() {
images: ['1.jpg', '2.jpg']
},
methods: {
inited (viewer) {
this.$viewer = viewer
},
show () {
this.$viewer.show()
}
}
}
</script>插件配置項(xiàng)
其它詳細(xì)配置項(xiàng)請(qǐng)參考 viewer.js 的詳細(xì)說(shuō)明:Viewer.js – 強(qiáng)大的JS/jQuery圖片查看器_dowebok
1、name:String,默認(rèn)值為 viewer
為了避免重名沖突,可以添加 name 配置項(xiàng),代碼引入如下:
<template>
<div id="app">
<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() {
images: ['1.jpg', '2.jpg']
},
methods: {
show () {
const vuer = this.$el.querySelector('.images').$vuer
vuer.show()
}
}
}
</script>2、defaultOptions:Object,默認(rèn)值為 undefined
在初始化時(shí)需要修改 viewer.js 的全局默認(rèn)配置項(xiàng),代碼引入如下:
import Viewer from 'v-viewer'
import Vue from 'vue'
Vue.use(Viewer, {
defaultOptions: {
zIndex: 9999
}
})在任何時(shí)候修改全局默認(rèn)配置項(xiàng),代碼如下:
import Viewer from 'v-viewer'
import Vue from 'vue'
Vue.use(Viewer)
Viewer.setDefaults({
zIndexInline: 2017
})到此這篇關(guān)于Vue2 中使用圖片查看器 v-viewer的方法的文章就介紹到這了,更多相關(guān)Vue2 v-viewer使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2 el-table行懸停時(shí)彈出提示信息el-popover的實(shí)現(xiàn)
本文主要介紹了vue2 el-table行懸停時(shí)彈出提示信息el-popover的實(shí)現(xiàn),用到了cell-mouse-enter、cell-mouse-leave兩個(gè)事件,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
ElementUI實(shí)現(xiàn)在下拉列表里面進(jìn)行搜索功能詳解
有時(shí)候需要用到下拉列表全選和搜索,下面這篇文章主要給大家介紹了關(guān)于ElementUI實(shí)現(xiàn)在下拉列表里面進(jìn)行搜索功能的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
Vue 中 createElement 使用實(shí)例詳解
Vue 提供了createElement 來(lái)創(chuàng)建虛擬dom,方便我們來(lái)函數(shù)化的方式來(lái)定義復(fù)雜的組件結(jié)構(gòu),這篇文章主要介紹了Vue 中 createElement 使用詳解,需要的朋友可以參考下2022-10-10
vue單頁(yè)面應(yīng)用打開(kāi)新窗口顯示跳轉(zhuǎn)頁(yè)面的實(shí)例
今天小編就為大家分享一篇vue單頁(yè)面應(yīng)用打開(kāi)新窗口顯示跳轉(zhuǎn)頁(yè)面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue-cli中的babel配置文件.babelrc實(shí)例詳解
Babel是一個(gè)廣泛使用的轉(zhuǎn)碼器,可以將ES6代碼轉(zhuǎn)為ES5代碼,從而在現(xiàn)有環(huán)境執(zhí)行。本文介紹vue-cli腳手架工具根目錄的babelrc配置文件,感興趣的朋友一起看看吧2018-02-02

