基于Vue3+TypeScript實(shí)現(xiàn)圖片預(yù)覽組件
簡(jiǎn)介
在現(xiàn)代的 Web 應(yīng)用中,圖片預(yù)覽是一個(gè)常見(jiàn)的需求。本文將介紹如何使用 Vue3 和 TypeScript 開(kāi)發(fā)一個(gè)圖片預(yù)覽組件,支持展示單張或多張圖片,并提供了豐富的配置選項(xiàng)。
組件功能
- 支持單張或多張圖片: 可以同時(shí)預(yù)覽單張或多張圖片,支持左右切換。
- 自定義配置選項(xiàng): 提供了豐富的配置選項(xiàng),如圖片縮放方式、懶加載、組件尺寸等。
- 多張圖片:無(wú)限循環(huán)預(yù)覽,默認(rèn)點(diǎn)擊為首張,布局左右自適應(yīng)父元素寬度,上下間距可通過(guò)參數(shù)(rowGap)控制。
組件實(shí)現(xiàn)
<script setup lang="ts" name="ImagePreview">
import { computed } from "vue";
interface ImageProps {
imageUrl: string | string[]; // 圖片地址 ==> 必傳
imageFit?: "fill" | "contain" | "cover" | "none" | "scale-down"; // 圖片縮放方式 ==> 非必傳(默認(rèn)為 cover)
imageLazy?: boolean; // 是否懶加載 ==> 非必傳(默認(rèn)為 true)
height?: string; // 組件高度 ==> 非必傳(默認(rèn)為 150px)
width?: string; // 組件寬度 ==> 非必傳(默認(rèn)為 150px)
borderRadius?: string; // 組件邊框圓角 ==> 非必傳(默認(rèn)為 8px)
rowGap?: string; // 組件行間距 ==> 非必傳(默認(rèn)為 10px)
}
// 接收父組件參數(shù)并設(shè)置默認(rèn)值
const props = withDefaults(defineProps<ImageProps>(), {
imageUrl: "",
imageFit: "cover",
imageLazy: true,
height: "150px",
width: "150px",
borderRadius: "8px",
rowGap: "10px"
});
// 圖片列表
const imageList = computed<string[]>(() => {
if (Array.isArray(props.imageUrl)) {
return props.imageUrl;
}
return [props.imageUrl];
});
</script>
<template>
<div class="image-list">
<div v-for="(item, index) in imageList" :key="index">
<el-image
class="image-style"
:src="item"
hide-on-click-modal
:initial-index="index"
:preview-src-list="imageList"
:lazy="imageLazy"
:fit="imageFit"
:z-index="99999"
preview-teleported
/>
</div>
</div>
</template>
<style lang="scss" scoped>
.image-list {
display: grid;
grid-row-gap: v-bind(rowGap);
grid-template-columns: repeat(auto-fill, v-bind(width));
justify-content: space-between;
.image-style {
width: v-bind(width);
height: v-bind(height);
border-radius: v-bind(borderRadius);
}
}
</style>
知識(shí)拓展
vue2使用v-viewer實(shí)現(xiàn)圖片預(yù)覽
v-viewer
用于圖片瀏覽的Vue組件,支持旋轉(zhuǎn)、縮放、翻轉(zhuǎn)等操作,基于viewer.js。
在Vue.js 2中使用v-viewer插件實(shí)現(xiàn)圖片預(yù)覽功能相對(duì)簡(jiǎn)單。v-viewer是一個(gè)Vue.js的圖片預(yù)覽插件,可以輕松實(shí)現(xiàn)圖片的放大、縮小和滑動(dòng)預(yù)覽等功能。以下是實(shí)現(xiàn)步驟:
安裝 v-viewer 插件:
在項(xiàng)目目錄下使用 npm 或 yarn 安裝 v-viewer 插件。
npm install v-viewer --save npm i -S viewerjs # 或 yarn add v-viewer yarn add viewerjs
在 main.js 文件中引入和配置 v-viewer 插件:
這行放在:import App from './App.vue'; 之前
import Viewer from 'v-viewer';
import 'viewerjs/dist/viewer.css';
Vue.use(Viewer);
或者
Vue.use(Viewer, {
defaultOptions: {
zIndex: 9999, // 設(shè)置圖片預(yù)覽組件的層級(jí),確保能在其他組件之上
},
});

在需要預(yù)覽圖片的組件中使用 v-viewer 指令:
<template>
<div>
<!-- 點(diǎn)擊圖片觸發(fā)預(yù)覽 -->
<img v-for="(image, index) in imageList" :key="index" :src="image" v-viewer />
</div>
</template>
<script>
export default {
data() {
return {
imageList: [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
// 添加更多圖片鏈接
],
};
},
};
</script
也可以使用以下方法
<template>
<div>
<button type="button" class="button" @click="previewURL">URL Array</button>
</div>
</template>
<script>
export default {
data() {
sourceImageURLs: [
'https://picsum.photos/200/200?random=1',
'https://picsum.photos/200/200?random=2',
],
},
methods: {
previewURL () {
// 如果使用`app.use`進(jìn)行全局安裝, 你就可以像這樣直接調(diào)用`this.$viewerApi`
const $viewer = this.$viewerApi({
images: this.sourceImageURLs
});
},
},
};
</script>
在上面的代碼中,我們將 v-viewer 指令應(yīng)用在 img 標(biāo)簽上,這樣點(diǎn)擊圖片時(shí)會(huì)觸發(fā)預(yù)覽效果。
總結(jié)
通過(guò)使用 Vue3 和 TypeScript,我們可以輕松地開(kāi)發(fā)出高度可定制的圖片預(yù)覽組件。這個(gè)組件可以幫助我們展示圖片,提供了豐富的配置選項(xiàng),以滿(mǎn)足不同項(xiàng)目的需求。
希望本文能幫助你更好地理解如何開(kāi)發(fā)圖片預(yù)覽組件!如果你有任何問(wèn)題或建議,請(qǐng)隨時(shí)提出。
以上就是基于Vue3+TypeScript實(shí)現(xiàn)圖片預(yù)覽組件的詳細(xì)內(nèi)容,更多關(guān)于Vue3 TypeScript圖片預(yù)覽的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于怎么在vue項(xiàng)目里寫(xiě)react詳情
本篇文章是在vue項(xiàng)目里寫(xiě)tsx的一篇介紹。其實(shí)vue里面寫(xiě)jsx也挺有意思的,接下來(lái)小編九給大家詳細(xì)介紹吧,感興趣的小伙伴請(qǐng)參考下面的文章內(nèi)容2021-09-09
vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式
這篇文章主要介紹了vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
fullcalendar日程管理插件月份切換回調(diào)處理方案
這篇文章主要為大家介紹了fullcalendar日程管理插件月份切換回調(diào)處理的方案示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
vue?electron實(shí)現(xiàn)無(wú)邊框窗口示例詳解
這篇文章主要為大家介紹了vue?electron實(shí)現(xiàn)無(wú)邊框窗口示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

