Using Sirv Media Viewer with Vue.js
npm install @sirv/sirvjs-vue2. Register the plugin in main.jsimport SirvjsVue from '@sirv/sirvjs-vue';
app.use(SirvjsVue);3. Load Sirv JS in your HTML<script src="https://scripts.sirv.com/sirvjs/v3/sirv.js"></script>4. Add your first viewer<sirv-media-viewer
:slides="[{ src: 'https://your-account.sirv.com/shoe.jpg', type: 'zoom' }]"
></sirv-media-viewer>Put your own Sirv domain in src. No account yet? Create one freeOpen on npmShort answer
To use Sirv Media Viewer in a Vue.js app, install the @sirv/sirvjs-vue npm package, register Sirv Media Viewer as a plugin and load Sirv JS. Then list your images, videos, spins or 3D models in its slides array. The same package works in Nuxt and Gridsome.
After reading this you will know
- How to install and register Sirv Media Viewer in Vue.js
- How to show images, videos, spins and 3D models
- How to use it with Nuxt and Gridsome
Sirv Media Viewer is a modern tool for displaying 360 degree spins, 3D models, images and videos, either separately or in a gallery.
Here’s a list of Sirv Media Viewer components you can use in a Vue.js app, either alone, in a gallery or in a smart gallery:
Getting started
Section titled “Getting started”To get the most from Sirv (dynamic image resizing, image zoom tiling and much more):
- Create a Sirv account (or log in).
- Upload media to your Sirv account.
Install Sirv Media Viewer npm package
Section titled “Install Sirv Media Viewer npm package”npm install @sirv/sirvjs-vue#pnpm add @sirv/sirvjs-vue#yarn add @sirv/sirvjs-vueRegister Sirv Media Viewer as a plugin
Section titled “Register Sirv Media Viewer as a plugin”import { createApp } from 'vue';import App from './App.vue';import SirvjsVue from '@sirv/sirvjs-vue';const app = createApp(App);
app.use(SirvjsVue);app.mount('#app');Load Sirv JS
Section titled “Load Sirv JS”You can load the Sirv Media Viewer script in your HTML:
<script src="https://scripts.sirv.com/sirvjs/v3/sirv.js"></script>If you need finer control, use the loadScript module:
import { loadScript } from '@sirv/sirvjs-vue';loadScript().then((sirv) => { sirv.start();})Using the component
Section titled “Using the component”There are two ways you can use the Sirv Media Viewer component in your Vue app.
slides array
Section titled “slides array”<sirv-media-viewer:slides="[ { src: 'https://demo.sirv.com/demo/Switch/switch-front.jpg', type: 'zoom', }, { src: 'https://demo.sirv.com/Examples/Coach/Coach.spin', type: 'spin', }, { src: 'https://demo.sirv.com/demo/Switch/nintendo_switch.glb', type: 'model', }, { src: 'https://demo.sirv.com/demo/Switch/switch.mp4', type: 'video', },]"></sirv-media-viewer>The slides object accepts other props, which you can find below.
div options
Section titled “div options”Alternatively, you can use the component by adding each element of the gallery as a div, for example:
<sirv-media-viewer data-options="fullscreen.enable:false;"><div data-type="zoom" data-src="https://demo.sirv.com/demo/Switch/switch-front.jpg"></div><div data-type="spin" data-src="https://demo.sirv.com/Examples/Coach/Coach.spin" data-options="column.reverse:true;"></div><div data-type="model" data-src="https://demo.sirv.com/demo/Switch/nintendo_switch.glb"></div><div data-type="video" data-src="https://demo.sirv.com/demo/Switch/switch.mp4"></div></sirv-media-viewer>You can apply data types and data options in the div itself. View all available options.
slides
:id- viewer id:data-src- used for*.viewfiles, overrides:slides. .view files documentationdata-options. Full list of options.:slides- an Array with slide sources. Sources can be a String or an Object.
A String accepts an url, eg https://demo.sirv.com/Examples/Coach/Coach.spin
An Object has additional props:
id- Slide id [String]src- Source [String]dataOptions- Local slide options [Object]type- Type of slide [String]. The available props are:spin,zoom,image,model,youtube,vimeo,video,htmldataThumbnailImage- Custom thumbnail image [String]dataThumbnailHtml- Custom thumbnail html [String]dataDisabled- Disable slide [Boolean]dataSwipeDisabled- Disable slide swipe [Boolean]dataHiddenSelector- Hide selector [Boolean]dataPinned- Pinned selector [String]. The available props are:left,rightstaticImage- Static image [Boolean].
Responsive image component
Section titled “Responsive image component”Achieving perfectly sized responsive images delivered in the optimal format is a piece of cake with Sirv Media Viewer. Here’s an example:
<sirv-image data-src='https://demo.sirv.com/tomatoes-basil.jpg' data-options="threshold:100;"></sirv-image>Using the Sirv API and events
Section titled “Using the Sirv API and events”You have full control of the viewer experience with API methods and events:
Here is a sample component:
<template> <sirv-media-viewer :id="id" data-src="https://demo.sirv.com/Examples/b74317.view" ></sirv-media-viewer> <div class="flex justify-center my-5"> <button class="btn mr-2" @click="prev">Previous</button> <button class="btn" @click="next">Next</button> </div></template> <script setup>import { onMounted, ref } from 'vue';import { loadScript } from '@sirv/sirvjs-vue';
const viewer = ref(null);const id = ref('smv-test');const prev = () => { if (viewer.value) { viewer.value.prev(); }};
const next = () => { if (viewer.value) { viewer.value.next(); }};
onMounted(() => { loadScript().then((sirv) => { sirv.start(); sirv.on('viewer:ready', (e) => { if (e.id === 'smv-test') { viewer.value = e; console.log('viewer:ready', e); } }); });});</script>Install the npm package
Section titled “Install the npm package”npm install @sirv/sirvjs-vue#pnpm add @sirv/sirvjs-vue#yarn add @sirv/sirvjs-vueRegister as a Nuxt plugin
Section titled “Register as a Nuxt plugin”import SirvjsVue from '@sirv/sirvjs-vue';export default defineNuxtPlugin(nuxtApp => { nuxtApp.vueApp.use(SirvjsVue, {})})Load Sirv JS
Section titled “Load Sirv JS”You can load the script (https://scripts.sirv.com/sirvjs/v3/sirv.js globally via the useHead composable, or in the nuxt config head section.
If you need more control, you can load it via loadScript:
import { loadScript } from '@sirv/sirvjs-vue';loadScript().then((sirv) => { sirv.start();})If your prefer to use Nuxt Image to handle your static images, you can use our official Sirv provider for Nuxt Image.
Gridsome
Section titled “Gridsome”The approach explained above should work fine with Gridsome. If you only need the Image CDN functionality of Sirv, you can use this third-party plugin: gridsome-plugin-image-cdn.
We hope that helped show how easily you can embed responsive images, 360 spins, videos and image zooms in your Vue.js website or app.
