實現(xiàn)vue圖片縮放方式-拖拽組件
實現(xiàn)效果如下
這幾天做了個沒做過的組件,以此記錄下,需要的效果是在一個dom內(nèi),縮放拖拽圖片。
封裝的子組件imgbox.Vue。父組件中使用,需要在父組件中準備一個盒子用來裝圖片,在這個盒子中可以縮放拽拽圖片。
父組件如下
父組件html部分
<!-- 普通方形盒子 --> <div class="box1"> ? ? ? <imgbox :config="data1" @enlarge="enlargeImg" @narrow="narrowImg" @changeImg="change"></imgbox> </div>
父組件的css部分
.box1{ ? ? width: 300px; ? ? height: 300px; ?? ?border: 1px solid #000; ?? ?/deep/ .dragImg{//深度css,由于vue中的style標簽的scoped屬性不能直接給子組件樣式,需要使用deep在父組件中給子組件中的dom給樣式 ? ? ? width: 420px;//子組件中的圖片大小 ? ? ? height: 280px; ? ? } ? ? /deep/ .btnbox{//深度css,由于vue中的style標簽的scoped屬性不能直接給子組件樣式,需要使用deep在父組件中給子組件中的dom給樣式 ? ? ? width: 70px;//子組件中按鈕盒子的大小 ? ? ? height: 20px; ? ? ? top: 20px;//子組件盒子的位置 ? ? ? left: 20px; ? ? ? .operChange{//按鈕的大小 ? ? ? ? width: 20px; ? ? ? ? height: 20px; ? ? ? } ? ? } ? }
父組件應用子組件
import imgbox from './imgbox' //拖拽,放大縮小圖片 ?子組件 components:{ imgbox },
配置數(shù)據(jù)
data1:{ ? ? ? ? name:"data1",//標識數(shù)據(jù) ? ? ? ? imgsrc:require('@/assets/timg.jpg'),//圖片路徑 ? ? ? ? imgname:"img01",//圖片對應的名字 ? 用該屬性和下面的圖片數(shù)組屬性對照,用于子組件獲取索引,給默認高亮 ? ? ? ? scale:1,//默認縮放1 ? ? ? }
方法
enlargeImg:function(val){//放大圖片 ? ? ? this[val.name].scale = this[val.name].scale + 0.1 ? ? } ,narrowImg:function(val){//縮小圖片 ? ? ? if(this[val.name].scale >= 0.1){ ? ? ? ? this[val.name].scale = this[val.name].scale - 0.1 ? ? ? } ? ? }
子組件imgbox.vue如下
子組件html部分
<template> ? <div class="myDiv"> ? ? <img class="dragImg" ref="img" name="removeImg" :src="imgsrc" v-drag :style="scaleFun"> ? ? <div class="btnbox" :ref="config.ref"> ? ? ? <img src="../assets/operaImg2.png" title="放大" @click="clickEnlarge" class="operChange"> ? ? ? <img src="../assets/operaImg3.png" title="縮小" @click="clickNarrow" class="operChange"> ? ? </div> ? </div> </template>
子組件接受父組件傳遞參數(shù),自定義指令
export default { ? components:{}, ? props:['config'], ? data(){ ? ? return { ? ? ? imgsrc:"",//圖片的路徑 ? ? } ? }, ? directives:{//注冊指令 ? ? drag:function(el){ ? ? ? let dragBox = el; //獲取當前元素 ? ? ? dragBox.onmousedown = e => { ? ? ? ? e.preventDefault(); ? ? ? ? //算出鼠標相對元素的位置 ? ? ? ? let disX = e.clientX - dragBox.offsetLeft; ? ? ? ? let disY = e.clientY - dragBox.offsetTop; ? ? ? ? document.onmousemove = e => { ? ? ? ? ? //用鼠標的位置減去鼠標相對元素的位置,得到元素的位置 ? ? ? ? ? e.preventDefault(); ? ? ? ? ? let left = e.clientX - disX; ? ? ? ? ? let top = e.clientY - disY; ? ? ? ? ? //移動當前元素 ? ? ? ? ? dragBox.style.left = left + "px"; ? ? ? ? ? dragBox.style.top = top + "px"; ? ? ? ? }; ? ? ? ? document.onmouseup = e => { ? ? ? ? ? e.preventDefault(); ? ? ? ? ? //鼠標彈起來的時候不再移動 ? ? ? ? ? document.onmousemove = null; ? ? ? ? ? //預防鼠標彈起來后還會循環(huán)(即預防鼠標放上去的時候還會移動) ? ? ? ? ? ? document.onmouseup = null; ? ? ? ? }; ? ? ? } ? ? } ? }, ? watch:{ ? ? config:function(val){ ? ? ? this.imgsrc = val.imgsrc ? ? } ? }, ? computed:{ ? ? scaleFun:function(){ ? ? ? return `transform:scale(${this.config.scale})` ? ? } ? }, ? created(){}, ? methods:{ ? ? clickEnlarge(){//點擊放大 ? ? ? let data = this.config ? ? ? this.$emit('enlarge',data) ? ? } ? ? ,clickNarrow(){//點擊縮小 ? ? ? let data = this.config ? ? ? this.$emit('narrow',data) ? ? } ? }, }
子組件css
.myDiv{ ? width: 100%; ? height: 100%; ? position: relative; ? overflow: hidden; ? img{ ? ? width: 100%; ? ? height: 100%; ? ? position: relative; ? } ? .btnbox{ ? ? display: flex; ? ? position: absolute; ? ? top: 20px; ? ? left: 20px; ? ? width: 70px; ? ? height: 20px; ? ? justify-content: space-around; ? ? z-index: 99; ? ? img{ ? ? ? width: 20px; ? ? ? height: 20px; ? ? ? opacity: 0.7; ? ? ? cursor: pointer; ? ? ? display: inline-block; ? ? } ? } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決mpvue + vuex 開發(fā)微信小程序vuex輔助函數(shù)mapState、mapGetters不可用問題
這篇文章主要介紹了解決mpvue + vuex 開發(fā)微信小程序 vuex輔助函數(shù)mapState、mapGetters不可用問題,需要的朋友可以參考下2018-08-08詳解vue+axios給開發(fā)環(huán)境和生產(chǎn)環(huán)境配置不同的接口地址
這篇文章主要介紹了詳解vue+axios給開發(fā)環(huán)境和生產(chǎn)環(huán)境配置不同的接口地址,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08elementUI+Springboot實現(xiàn)導出excel功能的全過程
這篇文章主要介紹了elementUI+Springboot實現(xiàn)導出excel功能,現(xiàn)在也對這個導出功能進行一個匯總整理寫出來,結(jié)合實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-09-09