Vue實現(xiàn)點擊圖片放大顯示功能
更新時間:2023年03月30日 10:00:04 作者:早起的年輕人
這篇文章主要為大家詳細介紹了如何利用Vue實現(xiàn)點擊圖片放大顯示功能,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的可以了解一下
方式一:列表中感應鼠標顯示大圖

我管理后臺使用的是 element , 列表使用的是 el-tabe
<el-table-column
prop="identifImg"
header-align="center"
align="center"
label="證件照"
width="100">
<template slot-scope="scope">
<el-popover
placement="top-start"
trigger="hover">
<div class="row_reserve"><img class="big-img" :src="scope.row.identifImg"/></div>
<div slot="reference"><img class="td-img" :src="scope.row.identifImg"/></div>
</el-popover>
</template>
</el-table-column>
在列表中實現(xiàn)放大圖片使用的是 el-popover 使用說明文檔

方式二:自定義通用組件實現(xiàn)
首先是自定義大圖顯示的通用組件:big-img.vue
<template>
<div v-show="visible" @click="closeClick" class="showPhoto">
<img class="img" :src="url" alt="圖片加載失敗" />
</div>
</template>
<script>
export default {
props: {
url: {
type: String,
default: "",
},
visible: {
type: Boolean,
default: false,
},
},
methods: {
closeClick() {
//子組件可以使用 $emit 觸發(fā)父組件的自定義事件
this.$emit("closeClick");
},
},
};
</script>
<style lang="css" scoped>
.showPhoto {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 99999;
display: flex;
align-items: center;
justify-content: center;
}
.showPhoto .img {
display: block;
margin: auto 0;
max-width: 20%;
text-align: center;
}
</style>然后在使用到文件中 引入組件并注冊組件
import BigImg from "../components/big-img"
export default {
data() {
return {
photoVisible: false,
bigImgUrl: ""
};
},
components:{
BigImg
},
methods: {
showBigImage(e) {//點擊圖片函數(shù),點擊后,把photoVisible設(shè)置成true
if (e != "") {
this.photoVisible = true;
this.bigImgUrl = e;
}
},
};
然后在圖片 img 處使用
<template>
<div>
<!-- imgBaseUrl為圖片URL-->
<img v-if="imgBaseUrl"
style="width:100%"
:src="imgBaseUrl"
@click.self="showBigImage(imgBaseUrl)">
<img
@click.self="showBigImage($event)"
src="~@/assets/img/liaojiewt/202141.png"
alt=""
/>
<!--顯示放大圖片的組件-->
<BigImg :visible="photoVisible" :url="bigImgUrl" @closeClick="()=>{photoVisible=false}"></BigImg>
</div>
</template>到此這篇關(guān)于Vue實現(xiàn)點擊圖片放大顯示功能的文章就介紹到這了,更多相關(guān)Vue點擊圖片放大內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Win11&Win10配置vue開發(fā)環(huán)境詳細圖文教程
目前前端三大框架(vue、react、angular)中vue是前端工程師經(jīng)常使用的,在使用之前需要搭建vue開發(fā)環(huán)境,這篇文章主要給大家介紹了關(guān)于Win11&Win10配置vue開發(fā)環(huán)境的相關(guān)資料,需要的朋友可以參考下2024-02-02
Vue使用Vue Elements實現(xiàn)文件預覽功能
在現(xiàn)代 web 開發(fā)中,用戶與系統(tǒng)的交互體驗越來越重要,而文件上傳和文件預覽是最常見的交互場景之一,本文將詳細介紹如何在 Vue 項目中使用 Vue Elements 來實現(xiàn)文件預覽的功能,包括基本使用方法、常見實例、性能優(yōu)化以及樣式自定義等內(nèi)容,需要的朋友可以參考下2025-01-01

