Skip to content

API ​

Suitable for scenarios such as opening a specified image array after a button click.

Playground ​

Edit this example

vue
<script>
import Vue from 'vue'
import VueViewer from 'v-viewer'
Vue.use(VueViewer)
const sourceImages = [
  '/images/photo-01.jpg',
  '/images/photo-02.jpg',
  '/images/photo-03.jpg',
  '/images/photo-04.jpg',
  '/images/photo-05.jpg',
  '/images/photo-06.jpg',
  '/images/photo-07.jpg',
  '/images/photo-08.jpg',
  '/images/photo-09.jpg',
  '/images/photo-10.jpg',
].map((original) => ({
  src: original.replace('/images/', '/images/thumbnails/'),
  'data-source': original,
}))
export default {
  data() {
    return {
      sourceImages,
      images: sourceImages.slice(0, 5),
      options: { toolbar: true, url: 'data-source', initialViewIndex: 0 },
    }
  },
  beforeDestroy() {
    this.viewerInstance?.destroy()
  },
  methods: {
    add() {
      this.images.push(this.sourceImages[this.images.length])
    },
    remove() {
      this.images.pop()
    },
    show() {
      this.viewerInstance?.destroy()
      this.viewerInstance = this.$viewerApi({
        images: this.images,
        options: this.options,
      })
    },
  },
}
</script>

<template>
  <main class="viewer-example">
    <div class="controls">
      <button :disabled="images.length === sourceImages.length" @click="add">
        Add image
      </button>
      <button :disabled="images.length === 0" @click="remove">
        Remove image
      </button>
      <button class="open" :disabled="images.length === 0" @click="show()">
        Show viewer
      </button>
      <div class="toolbar" role="group" aria-label="Toggle toolbar">
        <span>Toggle toolbar</span>
        <button
          :aria-pressed="options.toolbar ? 'true' : 'false'"
          @click="options.toolbar = true"
        >
          On
        </button>
        <button
          :aria-pressed="options.toolbar ? 'false' : 'true'"
          @click="options.toolbar = false"
        >
          Off
        </button>
      </div>
    </div>
    <p>Images: {{ images.length }}</p>
  </main>
</template>

url: 'data-source' uses the image’s data-source attribute as the preview URL; initialViewIndex sets the starting image index (from 0).

Parameters and return values ​

this.$viewerApi({ images, options }) / viewerApi({ images, options }) returns a Viewer.js instance. Closing or destroying it removes the temporary gallery.

Call destroy() on page unmount to clean up an open gallery.

Access all Viewer.js methods on the instance. Viewer.js methods

Options ​

Pass these settings through options in viewerApi({ images, options }). The API always opens in modal mode. inline is fixed to false and cannot be overridden.

These match Viewer.js options. Each name links to its original documentation; available settings and defaults depend on the installed Viewer.js version.

PropTypePurpose
backdropboolean / stringControl the modal backdrop; static keeps clicks on it from closing the viewer.
buttonbooleanShow the button in the upper-right corner.
classNamestringAdd custom classes to the viewer root.
containerstring / ElementChoose the modal container using an element or CSS selector.
filterfunctionInclude images for which the filter returns true; exclude the others.
fullscreenboolean / FullscreenOptionsRequest browser fullscreen during playback, optionally with Fullscreen API settings.
focusbooleanFocus the active thumbnail on initialization; requires keyboard.
inheritedAttributesarrayChoose additional source-image attributes to copy; src and alt are always copied.
initialCoveragenumberSet the initial image coverage, greater than 0 and at most 1.
initialViewIndexnumberChoose the starting image index, also used when view() receives no index.
autoplaybooleanAdvance slides automatically during playback.
intervalnumberSet the delay between slides in milliseconds.
keyboardbooleanAllow keyboard controls.
loadingbooleanDisplay a loading indicator while fetching an image.
loopbooleanWrap image navigation between the first and last images.
magnifierboolean / objectShow a hover magnifier in fullscreen view, with configurable size, ratio and opacity; unavailable on touch screens.
maxZoomRationumber / functionLimit magnification with a ratio or a function of the image and its data.
minZoomRationumber / functionLimit reduction with a ratio or a function of the image and its data.
movablebooleanAllow image movement.
navbarboolean / number / string / objectConfigure thumbnail visibility, size and position, with further layout settings in object form.
navigationboolean / number / objectConfigure the side navigation buttons, including individual visibility and size.
preloadbooleanLoad adjacent images before they are viewed.
rotatablebooleanAllow image rotation.
rotateOnGesturebooleanRotate through gesture events, such as two-finger gestures in Mac Safari.
rotateOnTouchbooleanRotate with two fingers on a touch screen.
scalablebooleanAllow axis scaling, including horizontal and vertical flips.
slideOnTouchbooleanNavigate between images with touch swipes.
slideOnWheelboolean / stringNavigate with the wheel, optionally requiring modifier keys; applies over thumbnails or when wheel zoom does not handle the event.
titleboolean / number / function / arrayConfigure title visibility and content; the default uses alt or the filename from the URL.
toggleOnDblclickbooleanSwitch between natural and initial image sizes on double-click.
toolbarboolean / number / objectConfigure toolbar placement and button visibility, size and click behavior.
tooltipbooleanDisplay the current zoom percentage while zooming.
transitionboolean / objectEnable CSS transitions, or disable selected action transitions using an object.
urlstring / functionRead the full image URL from a named image attribute or a function result.
zIndexnumberSet the modal viewer’s CSS stacking order.
zoomOnGesturebooleanZoom through gesture events, such as pinch gestures in Mac Safari.
zoomOnTouchbooleanZoom through dragging gestures on a touch screen.
zoomOnWheelboolean / stringZoom with the wheel, optionally requiring one or more modifier keys.
zoomRationumberSet the ratio increment for wheel zoom.
zoomablebooleanAllow image zooming.

Callbacks ​

Set callback functions such as viewed(event) in options. They correspond to Viewer.js events and receive the original DOM event, with operation data in event.detail. Click a name for its original documentation.

NameWhen it fires / useful data
readyThe viewer UI is initialized. In modal mode this can happen on the first opening.
show / shownBefore opening / after the opening transition completes in modal mode.
hide / hiddenBefore closing / after the closing transition completes in modal mode.
view / viewedBefore displaying an image / after it is displayed. detail.index is zero-based; image and originalImage identify the preview and source images.
move / movedBefore / after moving. detail.x, y, oldX and oldY describe the position change.
rotate / rotatedBefore / after rotation. detail.degree and oldDegree describe the angle change.
scale / scaledBefore / after scaling or flipping. detail.scaleX, scaleY, oldScaleX and oldScaleY describe the change.
zoom / zoomedBefore / after zooming. detail.ratio and oldRatio contain the zoom ratios.
play / stopBefore starting / stopping a slideshow.
playingBefore showing each slide during playback.

Released under the MIT License.