当前位置:首页 > VUE

vue实现弹照片

2026-03-08 05:08:23VUE

Vue 实现图片弹窗展示

基础弹窗实现

使用 Vue 的 v-ifv-show 控制弹窗显示状态,结合 CSS 实现遮罩层效果

<template>
  <div>
    <img 
      src="your-image.jpg" 
      @click="showModal = true"
      class="thumbnail"
    >

    <div v-if="showModal" class="modal">
      <span class="close" @click="showModal = false">&times;</span>
      <img class="modal-content" :src="selectedImage">
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showModal: false,
      selectedImage: 'your-image.jpg'
    }
  }
}
</script>

<style>
.thumbnail {
  cursor: pointer;
  width: 200px;
  height: auto;
}

.modal {
  position: fixed;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.9);
}

.modal-content {
  margin: auto;
  display: block;
  max-width: 80%;
  max-height: 80%;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

.close {
  position: absolute;
  top: 15px;
  right: 35px;
  color: white;
  font-size: 40px;
  font-weight: bold;
  cursor: pointer;
}
</style>

使用第三方组件库

Element UI 实现方式:

<template>
  <div>
    <el-image 
      style="width: 200px; height: 200px"
      :src="url"
      :preview-src-list="[url]"
    >
    </el-image>
  </div>
</template>

<script>
export default {
  data() {
    return {
      url: 'your-image.jpg'
    }
  }
}
</script>

图片画廊实现

实现多图片预览功能:

<template>
  <div>
    <div v-for="(img, index) in images" :key="index">
      <img 
        :src="img.thumbnail" 
        @click="openGallery(index)"
        class="gallery-thumb"
      >
    </div>

    <div v-if="galleryVisible" class="gallery-modal">
      <button @click="prevImage">❮</button>
      <img :src="images[currentIndex].fullSize">
      <button @click="nextImage">❯</button>
      <span @click="closeGallery" class="close">×</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        { thumbnail: 'thumb1.jpg', fullSize: 'full1.jpg' },
        { thumbnail: 'thumb2.jpg', fullSize: 'full2.jpg' }
      ],
      galleryVisible: false,
      currentIndex: 0
    }
  },
  methods: {
    openGallery(index) {
      this.currentIndex = index
      this.galleryVisible = true
    },
    closeGallery() {
      this.galleryVisible = false
    },
    prevImage() {
      this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length
    },
    nextImage() {
      this.currentIndex = (this.currentIndex + 1) % this.images.length
    }
  }
}
</script>

动画效果增强

添加过渡动画使弹窗更平滑:

vue实现弹照片

.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}

.modal-content {
  animation: zoomIn 0.3s;
}

@keyframes zoomIn {
  from { transform: scale(0.5) translateY(-50%); }
  to { transform: scale(1) translateY(-50%); }
}

标签: 照片vue
分享给朋友:

相关文章

vue实现冰墩墩

vue实现冰墩墩

使用 Vue 实现冰墩墩动画 通过 Vue 的动画特性和 SVG 技术,可以动态渲染冰墩墩的形象。以下是具体实现方法: 准备 SVG 素材 冰墩墩的形象通常由多个 SVG 路径组成,包括身体轮廓、面…

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name:…

vue实现点击

vue实现点击

实现点击事件的基本方法 在Vue中实现点击事件可以通过v-on指令或@简写方式绑定。以下是常见的点击事件绑定方法: <template> <button v-on:click=…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括动态生成、滚动显示、颜色和速度随机化等特性。以下是基于 Vue 3 的实现方法。 核心思路 使用 CSS 动画控制弹幕从右向左滚动。 动态生成弹幕数据,随机设置…

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有组…