当前位置:首页 > VUE

vue原生实现图片预览

2026-01-22 20:02:23VUE

使用 v-viewer 插件实现图片预览

安装 v-viewer 插件:

npm install v-viewer

main.js 中引入并注册:

import Viewer from 'v-viewer'
import 'viewerjs/dist/viewer.css'
Vue.use(Viewer)

在组件中使用:

<template>
  <div>
    <img v-for="src in images" :src="src" :key="src" v-viewer>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        'image1.jpg',
        'image2.jpg',
        'image3.jpg'
      ]
    }
  }
}
</script>

使用原生 JavaScript 实现图片预览

创建一个图片预览组件:

<template>
  <div>
    <img 
      v-for="(src, index) in images" 
      :src="src" 
      :key="index"
      @click="previewImage(src)"
      class="preview-thumbnail">

    <div v-if="showPreview" class="preview-modal" @click="closePreview">
      <img :src="currentImage" class="preview-image">
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: ['image1.jpg', 'image2.jpg', 'image3.jpg'],
      showPreview: false,
      currentImage: ''
    }
  },
  methods: {
    previewImage(src) {
      this.currentImage = src
      this.showPreview = true
      document.body.style.overflow = 'hidden'
    },
    closePreview() {
      this.showPreview = false
      document.body.style.overflow = ''
    }
  }
}
</script>

<style>
.preview-thumbnail {
  width: 100px;
  height: 100px;
  margin: 5px;
  cursor: pointer;
}

.preview-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.preview-image {
  max-width: 90%;
  max-height: 90%;
}
</style>

使用 HTML5 的 dialog 元素实现预览

<template>
  <div>
    <img 
      v-for="(src, index) in images" 
      :src="src" 
      :key="index"
      @click="openDialog(src)"
      class="thumbnail">

    <dialog ref="previewDialog" class="image-dialog">
      <img :src="dialogImage" class="dialog-image">
      <button @click="closeDialog" class="close-button">×</button>
    </dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: ['image1.jpg', 'image2.jpg', 'image3.jpg'],
      dialogImage: ''
    }
  },
  methods: {
    openDialog(src) {
      this.dialogImage = src
      this.$refs.previewDialog.showModal()
    },
    closeDialog() {
      this.$refs.previewDialog.close()
    }
  }
}
</script>

<style>
.thumbnail {
  width: 100px;
  height: 100px;
  margin: 5px;
  cursor: pointer;
}

.image-dialog {
  border: none;
  padding: 0;
  background: transparent;
  max-width: 90vw;
  max-height: 90vh;
}

.dialog-image {
  max-width: 100%;
  max-height: 80vh;
  display: block;
}

.close-button {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0,0,0,0.5);
  color: white;
  border: none;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  font-size: 20px;
  cursor: pointer;
}
</style>

实现图片放大缩小功能

在原生实现的预览组件中添加以下方法:

methods: {
  zoomIn() {
    const img = this.$refs.previewImage
    img.style.transform = `scale(${img.dataset.scale || 1 * 1.2})`
    img.dataset.scale = img.dataset.scale || 1 * 1.2
  },
  zoomOut() {
    const img = this.$refs.previewImage
    img.style.transform = `scale(${img.dataset.scale || 1 / 1.2})`
    img.dataset.scale = img.dataset.scale || 1 / 1.2
  },
  resetZoom() {
    const img = this.$refs.previewImage
    img.style.transform = 'scale(1)'
    img.dataset.scale = 1
  }
}

在模板中添加控制按钮:

<div class="zoom-controls">
  <button @click="zoomIn">+</button>
  <button @click="zoomOut">-</button>
  <button @click="resetZoom">Reset</button>
</div>

vue原生实现图片预览

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

相关文章

vue实现压缩上传文件

vue实现压缩上传文件

压缩上传文件的实现方法 在Vue中实现文件压缩和上传功能,可以通过以下步骤完成。该方法结合了前端文件压缩库和HTTP请求,确保文件在上传前被有效压缩。 安装必要的依赖 需要使用compressorj…

vue前端实现打印功能

vue前端实现打印功能

使用Vue实现前端打印功能 在Vue项目中实现打印功能可以通过多种方式完成,以下是几种常见的方法: 使用window.print()方法 这是最简单的打印方式,直接调用浏览器的打印功能。 me…

vue设计与实现目录

vue设计与实现目录

以下是《Vue设计与实现》一书的典型目录结构(根据常见技术书籍及Vue核心内容整理): 响应式系统 响应式数据的基本原理 依赖收集与触发更新 实现简易响应式系统 Proxy与Reflec…

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用X…