Skip to content

Using Sirv Media Viewer with Vue.js

By , Head of Sirv StudioUpdated

Open this page in a new chat

Short 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:

View full demo on StackBlitz

To get the most from Sirv (dynamic image resizing, image zoom tiling and much more):

  1. Create a Sirv account (or log in).
  2. Upload media to your Sirv account.
npm install @sirv/sirvjs-vue
#pnpm add @sirv/sirvjs-vue
#yarn add @sirv/sirvjs-vue
main.js
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');

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();
})

There are two ways you can use the Sirv Media Viewer component in your Vue app.

<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.

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

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, html
  • dataThumbnailImage - 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, right
  • staticImage - Static image [Boolean].

Achieving perfectly sized responsive images delivered in the optimal format is a piece of cake with Sirv Media Viewer. Here’s an example:

Tomatoes and basil
<sirv-image
data-src='https://demo.sirv.com/tomatoes-basil.jpg'
data-options="threshold:100;"
></sirv-image>

Full list of options.

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>
npm install @sirv/sirvjs-vue
#pnpm add @sirv/sirvjs-vue
#yarn add @sirv/sirvjs-vue
plugins/smv.client.ts
import SirvjsVue from '@sirv/sirvjs-vue';
export default defineNuxtPlugin(nuxtApp => {
nuxtApp.vueApp.use(SirvjsVue, {})
})

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.

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.

Back to top