Vue實現(xiàn)圖片預(yù)覽效果實例(放大、縮小、拖拽)
前言
這張圖是顯示的圖片放大的一個預(yù)覽情況,這里是參考預(yù)覽操作實現(xiàn)的一個背景為黑色的部分,上層的圖片可實現(xiàn)滾輪放大或者點擊上部的放大鏡圖標(biāo)進(jìn)行放大,代碼是基于Ant Design Vue框架的基礎(chǔ)上

這里先分解部分,后面有全部代碼
1.需要有黑色背景用于預(yù)覽背景:
這里的背景要占滿整個屏幕(這里的一般是參考其他插件預(yù)覽的樣式進(jìn)行模擬設(shè)計的),樣式在后方代碼內(nèi)
2.展示圖片并且把圖片展示到背景板最中間。
3.最重要的下方的兩部分:
滾輪放大縮小圖片:
bbimg() {
let e = e || window.event
this.params.zoomVal += e.wheelDelta / 1200
if (this.params.zoomVal >= 0.2) {
this.test = `transform:scale(${this.params.zoomVal});`
} else {
this.params.zoomVal = 0.2
this.test = `transform:scale(${this.params.zoomVal});`
return false
}
},
圖片拖拽:
imgMove(e) {
console.log('e', e)
let oImg = e.target
let disX = e.clientX - oImg.offsetLeft
let disY = e.clientY - oImg.offsetTop
console.log('disX', disX)
document.onmousemove = (e) => {
e.preventDefault()
let left = e.clientX - disX
let top = e.clientY - disY
this.test = this.test + `left: ${left}px;top: ${top}px;`
}
document.onmouseup = (e) => {
document.onmousemove = null
document.onmouseup = null
}
},
這里的test和classStyle是作為圖片的動態(tài)樣式,雖然名字起得著急,但是不影響使用
整體實現(xiàn)的功能:
- 點擊圖片,可以進(jìn)行滾輪放大及縮小,
- 點擊后按壓左鍵可進(jìn)行拖拽查看圖片
- 點擊上方的放大及縮小圖標(biāo)也可以進(jìn)行放大等操作,
- 點擊 x 可關(guān)于預(yù)覽
- 點擊關(guān)閉后,恢復(fù)大小,避免點擊其他照片影響大小

下面是全部實現(xiàn)代碼:
<template>
<a-card style="width: 100%">
<div>
<img
:src="file"
alt=""
@click="handlePhotoShow(file)"
/>
<!-- preview="0"
preview-text="圖片" -->
</div>
<div class="showImg" v-if="pictShow" @mousewheel="bbimg(this)">
<div class="setting_box">
<a-icon
class="setting_zoom"
v-if="zoomInShow == false"
type="zoom-in"
@click="handleZoomIn"
/>
<a-icon
color="#fff"
class="setting_zoom"
v-if="zoomInShow == true"
type="zoom-out"
@click="handleZoomOut"
/>
<a-icon color="#fff" class="setting_close" type="close" @click="handleClose" />
</div>
<img :src="file" alt="" :class="classStyle" :style="test" @mousedown="imgMove" />
</div>
</a-card>
</template>
<script>
export default {
data() {
return {
test: '',
pictShow: false,
zoomInShow: false,
params: {
zoomVal: 1,
left: 0,
top: 0,
currentX: 0,
currentY: 0,
flag: false,
},
file: '',
}
},
computed: {
classStyle() {
return this.zoomInShow ? 'a1' : 'a2'
},
},
methods: {
// 實現(xiàn)圖片放大縮小
bbimg() {
let e = e || window.event
this.params.zoomVal += e.wheelDelta / 1200
if (this.params.zoomVal >= 0.2) {
this.test = `transform:scale(${this.params.zoomVal});`
} else {
this.params.zoomVal = 0.2
this.test = `transform:scale(${this.params.zoomVal});`
return false
}
},
// 實現(xiàn)圖片拖拽
imgMove(e) {
console.log('e', e)
let oImg = e.target
let disX = e.clientX - oImg.offsetLeft
let disY = e.clientY - oImg.offsetTop
console.log('disX', disX)
document.onmousemove = (e) => {
e.preventDefault()
let left = e.clientX - disX
let top = e.clientY - disY
this.test = this.test + `left: ${left}px;top: ${top}px;`
}
document.onmouseup = (e) => {
document.onmousemove = null
document.onmouseup = null
}
},
handleZoomIn() {
this.zoomInShow = true
},
handleZoomOut() {
this.zoomInShow = false
},
handlePhotoShow(file) {
console.log('file', file)
this.file = file
this.pictShow = true
},
handleClose() {
this.pictShow = false
this.test = `transform:scale(1)`
},
},
}
</script>
<style scoped lang="less">
.showImg {
width: 100%;
height: 100vh;
background-color: rgba(0, 0, 0, 1);
position: fixed;
*position: absolute;
z-index: 20;
margin: 0 auto;
top: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
.setting_box {
width: 100%;
height: 50px;
line-height: 50px;
font-size: 20px;
background-color: rgba(0, 0, 0, 0.3);
position: absolute;
top: 0;
z-index: 999;
.setting_zoom,
.setting_close {
position: absolute;
z-index: 1000;
top: 20px;
color: #fff;
opacity: 1;
}
.setting_zoom {
right: 50px;
}
.setting_close {
right: 10px;
}
}
}
.a1 {
max-width: 200vw;
max-height: 180vh;
position: absolute;
z-index: 22;
margin-top: 40px;
cursor: move;
}
.a2 {
max-width: 95vw;
max-height: 90vh;
position: absolute;
z-index: 22;
margin-top: 40px;
cursor: move;
}
.zoom-box {
cursor: zoom-in;
}
.photo_box {
margin: 0 5px 5px 0;
}
</style>
因為具體也是查看了很多博客等資源最后完成的。
其實在代碼內(nèi)有一部分代碼:
<img :src="file" preview="0" preview-text="圖片" alt="" @click="handlePhotoShow(file)" />
其實有 preview="0" preview-text="圖片" 這兩行實現(xiàn)圖片的預(yù)覽,但是找了資料沒找到具體實現(xiàn)的部分,但是這個屬性確實實現(xiàn)了
這里手寫預(yù)覽的原因是這個插件在數(shù)量大的情況下是沒有反應(yīng)的。
總結(jié)
到此這篇關(guān)于Vue實現(xiàn)圖片預(yù)覽效果實例(放大、縮小、拖拽)的文章就介紹到這了,更多相關(guān)Vue圖片放大縮小拖拽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue?element-plus圖片預(yù)覽實現(xiàn)方法
- vue中關(guān)于element的el-image 圖片預(yù)覽功能增加一個下載按鈕(操作方法)
- Vue使用v-viewer插件實現(xiàn)圖片預(yù)覽和縮放和旋轉(zhuǎn)等功能(推薦)
- vue實現(xiàn)圖片預(yù)覽放大以及縮小問題
- vue-photo-preview圖片預(yù)覽失效的問題及解決
- Vue使用v-viewer實現(xiàn)圖片預(yù)覽
- vue+vant使用圖片預(yù)覽功能ImagePreview的問題解決
- vue實現(xiàn)壓縮圖片預(yù)覽并上傳功能(promise封裝)
- vue項目中實現(xiàn)圖片預(yù)覽的公用組件功能
- Vue.js圖片預(yù)覽插件使用詳解
- vue3中使用viewerjs實現(xiàn)圖片預(yù)覽效果的項目實踐
相關(guān)文章
vue中循環(huán)多個li(表格)并獲取對應(yīng)的ref的操作代碼
我想要獲取每一個循環(huán)并獲取每一個li(或者其它循環(huán)項)的ref,以便于后續(xù)的操作,接下來通過本文給大家分享vue中循環(huán)多個li(表格)并獲取對應(yīng)的ref的操作代碼,感興趣的朋友跟隨小編一起看看吧2024-02-02
Vue.js 2.0窺探之Virtual DOM到底是什么?
大家可能聽說Vue.js 2.0已經(jīng)發(fā)布,并且在其中新添加如了一些新功能。其中一個功能就是“Virtual DOM”。那么下面這篇文章就來給大家詳細(xì)介紹Vue.js 2.0中的Virtual DOM到底是什么?需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02

