vue实现点击图片放大显示功能

刘星火
2023-12-01

vue实现点击图片放大显示功能_厦门子豪的博客-CSDN博客_vue 点击查看大图

vue 实现点击图片放大_qq_36320160的博客-CSDN博客_vue点击图片放大

父组件:

<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>

import BigImg from "./BigImg.vue";
export default {
  data() {
    return {
      photoVisible: false,
      bigImgUrl: "",
     }
  },
  methods: {
  showBigImage(e) {//点击图片函数,点击后,把photoVisible设置成true
    if (e != "") {
      this.photoVisible = true;
      this.bigImgUrl = e.currentTarget.src;
    }
  },
  components: {
     BigImg,
      },
  }

子组件:

<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 触发父组件的自定义事件
      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: 100%;
  text-align: center;
}
</style>
 

 类似资料: