vue圖片拖拉轉(zhuǎn)放大縮小組件使用詳解
vue圖片拖拉轉(zhuǎn)放大縮小組件的具體使用方法,供大家參考,具體內(nèi)容如下
<doc>
圖片組件 - 用戶放大縮小以及拖拽
</doc>
<template>
<div style="width: 100%;position: relative;overflow: hidden;text-align: center;border: 1px solid #f1f2f3;">
<el-button size='mini' @click="toBIgChange" icon="el-icon-zoom-in"
style="position: absolute;top: 2px ;left: 2px;z-index: 999;"></el-button>
<el-button size='mini' @click="toSmallChange" icon="el-icon-zoom-out"
style="position: absolute;top: 2px ;left: 40px;z-index: 999;"></el-button>
<img id="img" :src="src" alt="" @mousedown.prevent="dropImage" :style="{transform:'scale('+multiples+')'}">
</div>
</template>
<script>
export default {
props: ['src'],
data() {
return {
multiples: 1,
odiv: null,
}
},
mounted() {
this.dropImage()
},
watch: {
src(newValue, oldValue) {
this.multiples = 1
if (this.odiv !== null) {
this.odiv.style.left = '0px';
this.odiv.style.top = '0px';
}
},
},
methods: {
toBIgChange() {
if (this.multiples >= 2) {
return;
}
this.multiples += 0.25;
},
// 縮小
toSmallChange() {
if (this.multiples <= 1) {
return;
}
this.multiples -= 0.25;
},
// 拖拽
dropImage(e) {
if (e === null) {
return
}
this.odiv = e.target; //獲取目標(biāo)元素
//算出鼠標(biāo)相對(duì)元素的位置
let disX = e.clientX - this.odiv.offsetLeft;
let disY = e.clientY - this.odiv.offsetTop;
document.onmousemove = (e) => { //鼠標(biāo)按下并移動(dòng)的事件
//用鼠標(biāo)的位置減去鼠標(biāo)相對(duì)元素的位置,得到元素的位置
let left = e.clientX - disX;
let top = e.clientY - disY;
//綁定元素位置到positionX和positionY上面
this.positionX = top;
this.positionY = left;
//移動(dòng)當(dāng)前元素
this.odiv.style.left = left + 'px';
this.odiv.style.top = top + 'px';
};
document.onmouseup = (e) => {
document.onmousemove = null;
document.onmouseup = null;
};
},
}
}
</script>
<style scoped>
img {
width: 100%;
position: relative;
}
</style>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決vue無(wú)法設(shè)置滾動(dòng)位置的問(wèn)題
這篇文章主要介紹了解決vue無(wú)法設(shè)置滾動(dòng)位置的問(wèn)題 ,需要的朋友可以參考下2018-10-10
Vue過(guò)濾器與內(nèi)置指令和自定義指令及組件使用詳解
這篇文章主要介紹了Vue過(guò)濾器與內(nèi)置指令和自定義指令及組件使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12
Vite結(jié)合whistle實(shí)現(xiàn)一勞永逸開發(fā)環(huán)境代理方案
這篇文章主要為大家介紹了Vite結(jié)合whistle實(shí)現(xiàn)一勞永逸開發(fā)環(huán)境代理方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Vue開發(fā)中出現(xiàn)Loading?Chunk?Failed的問(wèn)題解決
本文主要介紹了Vue開發(fā)中出現(xiàn)Loading?Chunk?Failed的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
關(guān)于vue-property-decorator的基礎(chǔ)使用實(shí)踐
這篇文章主要介紹了關(guān)于vue-property-decorator的基礎(chǔ)使用實(shí)踐,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue router解決路由帶參數(shù)跳轉(zhuǎn)時(shí)出現(xiàn)404問(wèn)題
我的頁(yè)面是從一個(gè)vue頁(yè)面router跳轉(zhuǎn)到另一個(gè)vue頁(yè)面,并且利用windows.open() 瀏覽器重新創(chuàng)建一個(gè)頁(yè)簽,但是不知道為什么有時(shí)候可以有時(shí)候又不行,所以本文給大家介紹了vue router解決路由帶參數(shù)跳轉(zhuǎn)時(shí)出現(xiàn)404問(wèn)題,需要的朋友可以參考下2024-03-03

