这里有简单的几种查看图片的效果。
1.可以使用element ui 里面组件来使用
代码块:
<div>
<div>图片预览el-image</div>
<el-image style="width: 100px; height: 100px" :src="url" :preview-src-list="srcList">
</el-image>
</div>
data() {
return {
url:
"https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
srcList: [
"https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg",
"https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg"
],
},
2.使用 npm install v-viewer 插件来实现也行。
首先需要安装 v-viewer npm install v-viewer
然后在 main.js 中进行导入引用。
import Viewer from 'v-viewer'
import 'viewerjs/dist/viewer.css'
Vue.use(Viewer)
使用:
<viewer>
<img alt="a duck" src="../assets/duck.jpg">
</viewer>
3.自定义图片预览组件
首先创建imagePreview.vue组件
<template>
<div class="custom-preview-container">
<h3>自定义图片预览</h3>
<!-- 图片列表 -->
<div class="image-list">
<div
v-for="(image, index) in images"
:key="index"
class="image-item"
@click="openPreview(index)"
>
<img :src="image.url" :alt="image.title" class="thumbnail" />
<div class="image-title">{{ image.title }}</div>
</div>
</div>
<!-- 预览弹窗 -->
<div class="preview-modal" v-if="previewVisible" @click="closePreview">
<div class="preview-content" @click.stop>
<div class="preview-header">
<span class="preview-title">{{ currentImage.title }}</span>
<button class="close-btn" @click="closePreview">×</button>
</div>
<div class="preview-body">
<button class="nav-btn prev-btn" @click="prevImage" v-if="images.length > 1"><</button>
<img :src="currentImage.url" :alt="currentImage.title" class="preview-img" />
<button class="nav-btn next-btn" @click="nextImage" v-if="images.length > 1">></button>
</div>
<div class="preview-footer">
<div class="preview-counter">{{ currentIndex + 1 }} / {{ images.length }}</div>
<div class="preview-tools">
<button class="tool-btn" @click="zoomIn">放大</button>
<button class="tool-btn" @click="zoomOut">缩小</button>
<button class="tool-btn" @click="resetZoom">重置</button>
<button class="tool-btn" @click="rotateLeft">↺</button>
<button class="tool-btn" @click="rotateRight">↻</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "CustomImagePreview",
data() {
return {
images: [
{
url:
"https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
title: "图片1"
},
{
url:
"https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg",
title: "图片2"
},
{
url:
"https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
title: "图片3"
},
{
url:
"https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg",
title: "图片4"
},
{
url:
"https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
title: "图片5"
},
{
url:
"https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg",
title: "图片6"
}
],
previewVisible: false,
currentIndex: 0,
scale: 1,
rotation: 0
};
},
computed: {
currentImage() {
return this.images[this.currentIndex] || {};
}
},
methods: {
openPreview(index) {
this.currentIndex = index;
this.previewVisible = true;
this.scale = 1;
this.rotation = 0;
// 添加键盘事件监听
document.addEventListener("keydown", this.handleKeyDown);
// 阻止背景滚动
document.body.style.overflow = "hidden";
},
closePreview() {
this.previewVisible = false;
// 移除键盘事件监听
document.removeEventListener("keydown", this.handleKeyDown);
// 恢复背景滚动
document.body.style.overflow = "";
},
nextImage() {
this.currentIndex = (this.currentIndex + 1) % this.images.length;
this.resetTransform();
},
prevImage() {
this.currentIndex =
(this.currentIndex - 1 + this.images.length) % this.images.length;
this.resetTransform();
},
zoomIn() {
this.scale = Math.min(this.scale + 0.25, 3);
this.updateTransform();
},
zoomOut() {
this.scale = Math.max(this.scale - 0.25, 0.5);
this.updateTransform();
},
resetZoom() {
this.scale = 1;
this.updateTransform();
},
rotateLeft() {
this.rotation -= 90;
this.updateTransform();
},
rotateRight() {
this.rotation += 90;
this.updateTransform();
},
resetTransform() {
this.scale = 1;
this.rotation = 0;
this.updateTransform();
},
updateTransform() {
const img = document.querySelector(".preview-img");
if (img) {
img.style.transform = `scale(${this.scale}) rotate(${this.rotation}deg)`;
}
},
handleKeyDown(e) {
switch (e.key) {
case "Escape":
this.closePreview();
break;
case "ArrowLeft":
this.prevImage();
break;
case "ArrowRight":
this.nextImage();
break;
case "+":
this.zoomIn();
break;
case "-":
this.zoomOut();
break;
case "r":
this.resetTransform();
break;
}
}
},
beforeDestroy() {
// 确保在组件销毁时移除事件监听
document.removeEventListener("keydown", this.handleKeyDown);
document.body.style.overflow = "";
}
};
</script>
<style scoped>
.custom-preview-container {
padding: 20px;
max-width: 1200px;
margin: 0 auto;
}
.image-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 20px;
}
.image-item {
cursor: pointer;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease;
}
.image-item:hover {
transform: translateY(-5px);
}
.thumbnail {
width: 100%;
height: 150px;
object-fit: cover;
display: block;
}
.image-title {
padding: 10px;
text-align: center;
background-color: #f8f8f8;
font-size: 14px;
}
/* 预览弹窗样式 */
.preview-modal {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
.preview-content {
width: 90%;
max-width: 1200px;
height: 90%;
background-color: #fff;
border-radius: 8px;
display: flex;
flex-direction: column;
overflow: hidden;
}
.preview-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 20px;
border-bottom: 1px solid #eee;
}
.preview-title {
font-size: 18px;
font-weight: 500;
}
.close-btn {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
color: #555;
}
.preview-body {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
position: relative;
overflow: hidden;
background-color: #f5f5f5;
}
.preview-img {
max-width: 90%;
max-height: 90%;
object-fit: contain;
transition: transform 0.3s ease;
}
.nav-btn {
position: absolute;
top: 50%;
transform: translateY(-50%);
background-color: rgba(255, 255, 255, 0.7);
border: none;
width: 40px;
height: 40px;
border-radius: 50%;
font-size: 20px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
z-index: 10;
}
.prev-btn {
left: 20px;
}
.next-btn {
right: 20px;
}
.preview-footer {
padding: 15px 20px;
display: flex;
justify-content: space-between;
align-items: center;
border-top: 1px solid #eee;
}
.preview-counter {
font-size: 14px;
color: #666;
}
.preview-tools {
display: flex;
gap: 10px;
}
.tool-btn {
background-color: #f0f0f0;
border: none;
padding: 6px 12px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.2s;
}
.tool-btn:hover {
background-color: #e0e0e0;
}
</style>
然后进行引入使用
<div>
<div>预览图片--手搓旋转等操作</div>
<image-preview></image-preview>
</div>
import imagePreview from "../components/imagePreview.vue";
export default {
components: { imagePreview },
}
最终效果:截图
ok搞定,以上预览图片的几种方式都可以实现点击图片进行预览,日常记录!!!