Vue2 实现图片预览

Vue2 实现图片预览

Vue 使用 img 实现 点击图片,使图片实现放大预览:
代码块:

<template>
  <div class="image-preview-container">
    <!-- 缩略图 -->
    <img 
      :src="imageSrc" 
      :alt="altText"
      class="thumbnail"
      @click="showPreview = true"
      :class="{ 'cursor-pointer': canPreview }"
      :style="thumbnailStyle"
    >
    
    <!-- 预览弹窗 -->
    <div 
      v-if="showPreview && canPreview" 
      class="preview-overlay"
      @click="showPreview = false"
    >
      <div class="preview-container" @click.stop>
        <!-- 关闭按钮 -->
        <button 
          class="close-btn" 
          @click="showPreview = false"
          aria-label="关闭预览"
        >
          <i class="fas fa-times"></i>
        </button>
        
        <!-- 预览图片 -->
        <div class="preview-image-wrapper">
          <img 
            :src="imageSrc" 
            :alt="altText"
            class="preview-image"
            :style="previewImageStyle"
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ImagePreview',
  props: {
    // 图片源地址
    imageSrc: {
      type: String,
      required: true
    },
    // 图片描述(用于alt属性)
    altText: {
      type: String,
      default: '图片预览'
    },
    // 是否允许预览
    canPreview: {
      type: Boolean,
      default: true
    },
    // 缩略图宽度
    thumbnailWidth: {
      type: [String, Number],
      default: 'auto'
    },
    // 缩略图高度
    thumbnailHeight: {
      type: [String, Number],
      default: 'auto'
    },
    // 预览图最大宽度
    maxPreviewWidth: {
      type: [String, Number],
      default: '90%'
    },
    // 预览图最大高度
    maxPreviewHeight: {
      type: [String, Number],
      default: '90%'
    }
  },
  data() {
    return {
      // 是否显示预览
      showPreview: false
    }
  },
  computed: {
    // 缩略图样式
    thumbnailStyle() {
      return {
        width: this.thumbnailWidth,
        height: this.thumbnailHeight,
        transition: 'all 0.3s ease'
      }
    },
    // 预览图样式
    previewImageStyle() {
      return {
        maxWidth: this.maxPreviewWidth,
        maxHeight: this.maxPreviewHeight
      }
    }
  },
  mounted() {
    // 点击ESC键关闭预览
    document.addEventListener('keydown', this.handleKeyDown)
  },
  beforeDestroy() {
    // 移除事件监听
    document.removeEventListener('keydown', this.handleKeyDown)
  },
  methods: {
    // 处理键盘事件
    handleKeyDown(e) {
      if (e.key === 'Escape' && this.showPreview) {
        this.showPreview = false
      }
    }
  }
}
</script>

<style scoped>
.thumbnail {
  object-fit: contain;
}

.thumbnail:hover {
  opacity: 0.9;
  transform: scale(1.02);
}

.preview-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.85);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
  opacity: 0;
  animation: fadeIn 0.3s ease forwards;
}

.preview-container {
  position: relative;
  max-width: 100%;
  max-height: 100%;
}

.close-btn {
  position: absolute;
  top: -40px;
  right: -40px;
  background: none;
  border: none;
  color: white;
  font-size: 24px;
  cursor: pointer;
  transition: all 0.2s ease;
  padding: 5px;
}

.close-btn:hover {
  transform: scale(1.2);
  color: #ff4d4f;
}

.preview-image-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.preview-image {
  object-fit: contain;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
  transform: scale(0.95);
  animation: scaleIn 0.3s ease forwards;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes scaleIn {
  from { transform: scale(0.95); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* 适配小屏幕设备 */
@media (max-width: 768px) {
  .close-btn {
    top: 10px;
    right: 10px;
    z-index: 10000;
  }
  
  .preview-image-wrapper {
    padding: 10px;
  }
}
</style>

使用说明:

1、首先确保您的项目中已经引入了 Font Awesome,因为组件使用了关闭按钮的图标

2、组件的基本用法

<template>
  <div>
    <ImagePreview 
      imageSrc="your-image-url.jpg" 
      altText="图片描述"
    />
  </div>
</template>

<script>
import ImagePreview from './ImagePreview.vue'

export default {
  components: {
    ImagePreview
  }
}
</script>

3、组件支持的自定义属性:

  • imageSrc:图片的 URL(必填)
  • altText:图片的描述文本(用于 alt 属性)
  • canPreview:是否允许预览(默认为 true)
  • thumbnailWidth:缩略图宽度
  • thumbnailHeight:缩略图高度
  • maxPreviewWidth:预览图最大宽度
  • maxPreviewHeight:预览图最大高度

4、交互特点:

  • 点击缩略图可放大预览
  • 点击预览层背景或关闭按钮可关闭预览
  • 按 ESC 键也可关闭预览
  • 包含平滑的过渡动画
  • 响应式设计,适配各种屏幕尺寸
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值