2.4.0 • Published 4 months ago

vue-previewable-image v2.4.0

Weekly downloads
-
License
-
Repository
github
Last release
4 months ago

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

VersionSupport Vue versionDocs
^1.7.0+^2.7.14See v1
^2.1.2+^3.2.0 and aboveSee 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 nameDescriptionTypeAvailable valueDefault value
widthThe img container widthstring-undefined
heightThe img container heightstring-undefined
srcThe src of imgstring-undefined
altThe alt of imgstring-undefined
referrerPolicyThe referrerPolicy of imgstring-undefined
lazyWhether to enable image lazy loadboolean-true
zIndexDefine the CSS z-index value of the viewer in modal modenumber or string-2015
fitThe object-fit of imgstringfill / contain / cover / none / scale-downfill
previewSrcListDefine your previewable image liststring[] or { src: string; alt: string}[]-[]
currentPreviewIndexCurrent preview image shown index, support v-modelnumber-0
viewerOptionsDefine viewerjs Options--{}
viewerTitleDefine 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 countFunction-undefined

Events

Event nameDescriptionCallback arguments
switchEmit when preview image switch.(index: number, viewer: Viewer) => void
loadEmit when image load success.(e: Event) => void
errorEmit when image load error.(e: Event) => void

Slots

NameDescription
placeholderDefine the placeholder content to display when image is not loaded
errorDefine the content to display when image load error
2.4.0

4 months ago

1.10.0

4 months ago

1.9.1

5 months ago

1.9.0

6 months ago

2.3.1

5 months ago

1.9.2

5 months ago

1.8.2

12 months ago

1.8.1

12 months ago

1.8.0

12 months ago

2.3.0

8 months ago

2.2.1

9 months ago

2.2.0

12 months ago

2.2.2

9 months ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.0

1 year ago

1.7.0

1 year ago

1.6.0

1 year ago

1.5.0

1 year ago

1.4.2

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago