Vue實(shí)現(xiàn)點(diǎn)擊圖片放大顯示功能
方式一:列表中感應(yīng)鼠標(biāo)顯示大圖
我管理后臺使用的是 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>
在列表中實(shí)現(xiàn)放大圖片使用的是 el-popover 使用說明文檔
方式二:自定義通用組件實(shí)現(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) {//點(diǎn)擊圖片函數(shù),點(diǎn)擊后,把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實(shí)現(xiàn)點(diǎn)擊圖片放大顯示功能的文章就介紹到這了,更多相關(guān)Vue點(diǎn)擊圖片放大內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3中關(guān)于ref和reactive的區(qū)別分析
這篇文章主要介紹了vue3關(guān)于ref和reactive的區(qū)別分析,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-06-06Vue 封裝防刷新考試倒計時組件的實(shí)現(xiàn)
這篇文章主要介紹了Vue 封裝防刷新考試倒計時組件的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Vue中的ref作用詳解(實(shí)現(xiàn)DOM的聯(lián)動操作)
這篇文章主要介紹了Vue中的ref作用詳解(實(shí)現(xiàn)DOM的聯(lián)動操作),需要的朋友可以參考下2017-08-08vue2實(shí)現(xiàn)directive自定義指令的封裝與全局注冊流程
自定義指令是對普通DOM元素進(jìn)行的底層操作,它是一種有效的的補(bǔ)充和擴(kuò)展,不僅可以用于定義任何的dom操作,并且是可以復(fù)用的,下面這篇文章主要給大家介紹了關(guān)于vue2實(shí)現(xiàn)directive自定義指令的封裝與全局注冊流程的相關(guān)資料,需要的朋友可以參考下2023-02-02VueX瀏覽器刷新如何實(shí)現(xiàn)保存數(shù)據(jù)
這篇文章主要介紹了VueX瀏覽器刷新如何實(shí)現(xiàn)保存數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07