Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Image viewer component for vue, supports rotation, scale, zoom and so on, based on viewer.js
Image viewer component for vue, supports rotation, scale, zoom and so on, based on viewer.js
.css
file:import 'viewerjs/dist/viewer.css'
Install from GitHub via NPM
npm install v-viewer
To use v-viewer
, simply import it and the css
file, and call Vue.use()
to install.
<template>
<div id="app">
<!-- directive -->
<div class="images" v-viewer>
<img src="1.jpg">
<img src="2.jpg">
...
</div>
<!-- component -->
<viewer :images="images">
<img v-for="src in images" :src="src" :key="src">
</viewer>
</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']
}
}
</script>
<link href="//path/viewer.css" rel="stylesheet">
<script src="//path/vue.js"></script>
<script src="//path/viewer.js"></script>
<script src="//path/v-viewer.js"></script>
...
<script>
Vue.use(VueViewer.default)
</script>
var VueViewer = require('VueViewer')
require(['VueViewer'], function (VueViewer) {});
Just add the directive v-viewer
to any element, then all img
elements in it will be handled by viewer
.
You can set the options like this: v-viewer="{inline: true}"
Get the element by selector and then use el.$viewer
to get the viewer
instance if you need.
<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>
The viewer
instance will be created only once after the directive binded.
If you're sure the images inside this element won't change again, use it to avoid unnecessary re-render.
<div class="images" v-viewer.static="{inline: true}">
<img v-for="src in images" :src="src" :key="src">
</div>
The viewer
instance will be updated by update
method when the source images changed (added, removed or sorted) by default.
If you encounter any display problems, try rebuilding instead of updating.
<div class="images" v-viewer.rebuild="{inline: true}">
<img v-for="src in images" :src="src" :key="src">
</div>
You can simply import the component and register it locally too.
Use scoped slot to customize the presentation of your images.
<template>
<div id="app">
<viewer :options="options" :images="images"
@inited="inited"
class="viewer" ref="viewer"
>
<template slot-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>
Array
Array
You can replace images
with trigger
, to accept any type of prop.
when the trigger
changes, the component will re-render the viewer.
<viewer :trigger="externallyGeneratedHtmlWithImages">
<div v-html="externallyGeneratedHtmlWithImages"/>
</viewer>
Boolean
false
The viewer instance will be updated by update
method when the source images changed (added, removed or sorted) by default.
If you encounter any display problems, try rebuilding instead of updating.
<viewer
ref="viewer"
:options="options"
:images="images"
rebuild
class="viewer"
@inited="inited"
>
<template slot-scope="scope">
<img v-for="src in scope.images" :src="src" :key="src">
{{scope.options}}
</template>
</viewer>
Viewer
Listen for the inited
event to get the viewer
instance, or use this.refs.xxx.$viewer
.
Only available in modal mode.
You can call the function: this.$viewerApi({options: {}, images: []})
to show gallery without rendering the img
elements yourself.
The function this.$viewer
returns the current viewer instance.
<template>
<div id="app">
<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 Viewer from 'v-viewer'
import Vue from 'vue'
Vue.use(Viewer)
export default {
data() {
sourceImageURLs: ['1.png', '2.png'],
sourceImageObjects: [{'src':'thumbnail.png', 'data-source':'source.png'}]
},
methods: {
previewURL () {
const $viewer = this.$viewerApi({
images: this.sourceImageURLs
})
},
previewImgObject () {
const $viewer = this.$viewerApi({
options: {
toolbar: true,
url: 'data-source',
initialViewIndex: 2
},
images: this.sourceImageObjects
})
}
}
}
</script>
Refer to viewer.js.
String
viewer
If you need to avoid name conflict, you can import it like this:
<template>
<div id="app">
<!-- directive name -->
<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>
<!-- component name -->
<vuer :images="images">
<img v-for="src in images" :src="src" :key="src">
</vuer>
</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 () {
// viewerjs instance name
const vuer = this.$el.querySelector('.images').$vuer
vuer.show()
// api name
this.$vuerApi({
images: this.images
})
}
}
}
</script>
Object
undefined
If you need to set the viewer default options, you can import it like this:
import Viewer from 'v-viewer'
import Vue from 'vue'
Vue.use(Viewer, {
defaultOptions: {
zIndex: 9999
}
})
And you can reset the default options at any other time:
import Viewer from 'v-viewer'
import Vue from 'vue'
Vue.use(Viewer)
Viewer.setDefaults({
zIndexInline: 2021
})
FAQs
Image viewer component for vue, supports rotation, scale, zoom and so on, based on viewer.js
The npm package v-viewer receives a total of 20,685 weekly downloads. As such, v-viewer popularity was classified as popular.
We found that v-viewer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.