vue圖片拖拉轉(zhuǎn)放大縮小組件使用詳解
更新時(shí)間:2021年10月27日 09:13:33 作者:秦浩鋮
這篇文章主要為大家詳細(xì)介紹了vue圖片拖拉轉(zhuǎn)放大縮小組件的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
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)相對元素的位置 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)相對元素的位置,得到元素的位置 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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue項(xiàng)目報(bào)錯(cuò)Uncaught runtime errors的解決方案
使用vue-cli的vue項(xiàng)目,出現(xiàn)編譯錯(cuò)誤或警告時(shí),在瀏覽器中顯示全屏覆蓋,提示報(bào)錯(cuò)Uncaught runtime errors,本文給大家介紹了vue項(xiàng)目報(bào)錯(cuò)Uncaught runtime errors的解決方案,需要的朋友可以參考下2024-01-01vue將后臺(tái)數(shù)據(jù)時(shí)間戳轉(zhuǎn)換成日期格式
這篇文章主要為大家詳細(xì)介紹了vue將后臺(tái)數(shù)據(jù)時(shí)間戳轉(zhuǎn)換成日期格式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07Vue3中實(shí)現(xiàn)發(fā)送網(wǎng)絡(luò)請求功能(最新推薦)
Axios是一個(gè)基于Promise的HTTP客戶端,可以在瀏覽器和Node.js中用于發(fā)送HTTP請求,本文主要介紹在Vue3中實(shí)現(xiàn)發(fā)送網(wǎng)絡(luò)請求功能,感興趣的朋友一起看看吧2023-12-12vue根據(jù)權(quán)限動(dòng)態(tài)渲染按鈕、組件等的函數(shù)式組件實(shí)現(xiàn)
這篇文章主要介紹了vue根據(jù)權(quán)限動(dòng)態(tài)渲染按鈕、組件等的函數(shù)式組件實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望杜大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11