Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vue-previewable-image

yisibell2592.4.2TypeScript support: included

A previewable image Vue component based on viewerjs.

aidol, aidol-image, viewer, vue, vue-image, el-image, component, viewerjs, image, preview, previewable, image-viewer

readme


vue-previewable-image

A previewable image Vue component based on viewerjs.

:warning: TIPS: Current vue-previewable-image needs Vue verison to 3.2.0+. For vue2, please use 1.x.

Features

  • :heavy_check_mark: Support preview image via viewerjs.
  • :heavy_check_mark: Support image lazy load.
  • :heavy_check_mark: Support using image viewer as a component via ImageViewer.

Package

Version Support Vue version Docs
^1.7.0+ ^2.7.14 See v1
^2.1.2+ ^3.2.0 and above See v2

Installation

# pnpm
$ pnpm add vue-previewable-image

# yarn
$ yarn add vue-previewable-image

# npm
$ npm i vue-previewable-image

Usage

Do not forgot to import the style vue-previewable-image/dist/style.css

<template>
  <main>
    <PreviewableImage
      v-model:current-preview-index="currentIndex"
      :src="src"
      :preview-src-list="srcList"
      :viewer-title="viewerTitle"
      width="100px"
      @switch="handleSwitch"
    />
  </main>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { PreviewableImage } from 'vue-previewable-image'
import type { CustomViewerTitle, ViewerSwitchEvent } from 'vue-previewable-image'

const src =
  'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'

const srcList = [
  'https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg',
  'https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg',
]

const viewerTitle: CustomViewerTitle = (img, { index, total }) => {
  console.log('img:', img)
  return `${img.alt} (${index + 1}/${total})`
}

const handleSwitch: ViewerSwitchEvent = (index, viewer) => {
  console.log('on switch:', index, viewer)
}

const currentIndex = ref(0)
</script>

You also can use viewerjs from this package, This is equal to import Viewer from 'viewerjs'.

import { Viewer } from 'vue-previewable-image'

Or, you can using image viewer as a component, See ImageViewer.

Using via Vue plugin

// main.ts

import { createApp } from 'vue'
import App from './App.vue'
import PreviewableImage from 'vue-previewable-image'
import type { PreviewableImageOptions } from 'vue-previewable-image'

const app = createApp(App)

app.use(PreviewableImage, {
  // set global component name
  componentName: 'PreviewableImage',

  // set Viewer default options
  defaultViewerOptions: {
    // ...
  }

} as PreviewableImageOptions).mount('#app')

Attributes

Prop name Description Type Available value Default value
width The img container width string - undefined
height The img container height string - undefined
src The src of img string - undefined
alt The alt of img string - undefined
referrerPolicy The referrerPolicy of img string - undefined
lazy Whether to enable image lazy load boolean - true
zIndex Define the CSS z-index value of the viewer in modal mode number or string - 2015
fit The object-fit of img string fill / contain / cover / none / scale-down fill
previewSrcList Define your previewable image list string[] or { src: string; alt: string}[] - []
currentPreviewIndex Current preview image shown index, support v-model number - 0
viewerOptions Define viewerjs Options - - {}
viewerTitle Define viewer title. First argument is HTMLImageElement which is generated by previewSrcList, second argument is a object { index: number; total: number } which record current viewer index and previewable image count Function - undefined

Events

Event name Description Callback arguments
switch Emit when preview image switch. (index: number, viewer: Viewer) => void
load Emit when image load success. (e: Event) => void
error Emit when image load error. (e: Event) => void

Slots

Name Description
placeholder Define the placeholder content to display when image is not loaded
error Define the content to display when image load error

changelog

Changelog

v2.4.2

compare changes

🩹 Fixes

❤️ Contributors

v2.4.1

compare changes

🩹 Fixes

  • Reinit viewer when preview src list, and options changed (62460c8)

🏡 Chore

  • Update lazy load example (382ae6b)

❤️ Contributors

v2.4.0

compare changes

🚀 Enhancements

  • Upgrade all deps to latest version and update example site (3b7640d)

🏡 Chore

❤️ Contributors

v2.3.1

compare changes

🏡 Chore

❤️ Contributors

v2.3.0

compare changes

🚀 Enhancements

  • Upgrade typescript to v5 and other all non-major deps (0938a6a)

🏡 Chore

❤️ Contributors

v2.2.2

🏡 Chore

  • example: Add example deploy (b2736af)
  • Ignore example (9561fe9)
  • example: Fix site base url (c87e4c3)
  • Using pnpm instead of yarn (22b67d7)
  • Update readme (8445810)
  • example: Fix example (6cda92e)
  • Add aidol-image keywords (e922211)
  • Fix release scripts (80fc40f)
  • Update keywords (32b81c6)

❤️ Contributors

v2.2.0

🚀 Enhancements

  • v2: Support vue3 (ba190de)
  • Add image viewer component (d8f317f)
  • Add image viewer component (a55e311)

🏡 Chore

  • release: V2.1.0 (7599de8)
  • docs: Update readme (64c9b8e)
  • release: V2.1.1 (5c11b88)
  • docs: Fix v3 docs (f848209)
  • release: V2.1.2 (22ff3c5)
  • release: V1.8.0 (1cc0ed5)
  • docs: Update readme (4e14a7e)
  • release: V1.8.1 (1285d9c)
  • docs: Update readme (2cf5a71)
  • release: V1.8.2 (ac1fadf)

❤️ Contributors

v2.1.2

🏡 Chore

  • docs: Fix v3 docs (f848209)

❤️ Contributors

v2.1.1

🏡 Chore

  • docs: Update readme (64c9b8e)

❤️ Contributors

v2.1.0

🚀 Enhancements

  • v2: Support vue3 (ba190de)

❤️ Contributors

v1.7.0

🚀 Enhancements

  • Support using via a vue plugin (0353e5a)

❤️ Contributors

v1.6.0

🚀 Enhancements

  • Support quick define viewer z-index and img referrerPolicy attribute (1e37dba)

🏡 Chore

  • docs: Update readme (980e0e8)

❤️ Contributors

v1.5.0

🚀 Enhancements

  • Add image load and error event; fix lazy load (c7e3e9b)

❤️ Contributors

v1.4.2

🏡 Chore

  • docs: Update docs (d461cd8)

❤️ Contributors

v1.4.1

🩹 Fixes

  • types: Add lazy attr type (6a4ad17)

❤️ Contributors

v1.4.0

🚀 Enhancements

  • Support image lazy load (0c9df21)

🏡 Chore

  • docs: Update readme (c2c6b69)

❤️ Contributors

v1.3.0

🚀 Enhancements

  • Support currentPreviewIndex; update types (d480d13)

❤️ Contributors

v1.2.1

🩹 Fixes

  • When unmounted destory viewer instance (aa49359)

❤️ Contributors

v1.2.0

🚀 Enhancements

  • Support vue 2.7 (71f1402)

❤️ Contributors

v1.1.2

🩹 Fixes

  • vue-demi: Vue-demi config (b8fc8e7)

❤️ Contributors

v1.1.1

🩹 Fixes

  • Use vue-demi (ef1c2b1)

🏡 Chore

  • git: Show dist (8be9739)

❤️ Contributors

v1.1.0

🚀 Enhancements

  • V1 (6584b02)

🏡 Chore

  • Init (62cbf32)
  • docs: Update readme (7c484fe)

❤️ Contributors