当前位置:首页 > VUE

vue原生实现图片预览

2026-01-22 20:02:23VUE

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

安装 v-viewer 插件:

npm install v-viewer

main.js 中引入并注册:

vue原生实现图片预览

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 实现图片预览

创建一个图片预览组件:

vue原生实现图片预览

<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实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-ra…

vue实现mvc

vue实现mvc

Vue 实现 MVC 模式 Vue 本身是一个 MVVM(Model-View-ViewModel)框架,但可以通过结构调整实现 MVC(Model-View-Controller)模式。以下是具体实…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

vue实现机制

vue实现机制

Vue 实现机制的核心原理 Vue.js 的核心实现机制主要基于响应式系统、虚拟 DOM 和组件化设计。以下是关键实现细节: 响应式系统 Vue 通过 Object.defineProperty(V…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…