国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

基于Vue2實(shí)現(xiàn)移動(dòng)端圖片上傳、壓縮、拖拽排序、拖拽刪除功能

瀏覽:3日期:2022-10-12 14:40:38

用Vue2實(shí)現(xiàn)移動(dòng)端圖片上傳、壓縮、拖拽排序、拖拽刪除功能 圖片上傳圖片壓縮拖拽排序、拖拽刪除 之前在公司開(kāi)發(fā)過(guò)一段時(shí)間的移動(dòng)端H5頁(yè)面,有個(gè)功能就是要上傳圖片+壓縮。參考了一下網(wǎng)上的方法,外加自己摸索的過(guò)程,最終實(shí)現(xiàn)了這個(gè)功能。后面在家閑的時(shí)候又加多了個(gè)長(zhǎng)按選中圖片,并且可以拖拽排序、拖拽到指定位置刪除的功能。

github地址:代碼地址

下面直接進(jìn)入正題:

圖片上傳

圖片上傳用的是HTML的input標(biāo)簽實(shí)現(xiàn)的。核心就是把獲取到的文件通過(guò)FileReader轉(zhuǎn)換成圖片,代碼如下:

<input type='file' accept='image/*' capture='camera' @change='selectFile'>selectFile(event:any){ this.showAlert = false if (event.target.files && event.target.files.length > 0) { this.isLoading = true let file:any = event.target.files[0] let fr:any = new FileReader() fr.readAsDataURL(file) fr.onload = (imgObj:any) => { let img:any = new Image() img.src = imgObj.target.result img.onload = (e:any) => { // 這里就可以獲取到上傳的圖片了 }) } } }}圖片壓縮

圖片壓縮用的是canvas重繪的方法實(shí)現(xiàn)的,具體代碼如下:

// 省略上面的代碼fr.onload = (imgObj:any) => {// 獲取到圖片文件后 let img:any = new Image() img.src = imgObj.target.result img.onload = (e:any) => { Compress(img,e.path[0].height,e.path[0].width,(newImg:any) => { this.imgList.push(newImg) this.isLoading = false // 待添加拖拽功能 }) }}/** * 圖片壓縮 * @param img 圖片對(duì)象 */export function Compress(img:any,height:number,width:number,callback:Function) { let canvas:any = document.createElement(’canvas’) let context:any = canvas.getContext(’2d’) canvas.width = width canvas.height = height context.clearRect(0,0,width,height) context.drawImage(img,0,0,width,height) callback(canvas.toDataURL('image/jpeg', 0.75))}拖拽排序、拖拽刪除

拖拽排序、拖拽到指定位置刪除是通過(guò)監(jiān)聽(tīng)touch事件來(lái)實(shí)現(xiàn)的。核心思路:1、獲取到圖片dom元素,給圖片dom元素添加ontouchstart、ontouchend、ontouchmove 方法。2、在ontouchstart方法中new一個(gè)時(shí)間節(jié)點(diǎn),在ontouchend中也new一個(gè)時(shí)間節(jié)點(diǎn),通過(guò)判斷兩個(gè)時(shí)間節(jié)點(diǎn)之間的時(shí)間間隔判斷是點(diǎn)擊事件還是長(zhǎng)按事件。3、ontouchstart中設(shè)置settimeout方法是延時(shí)判斷是點(diǎn)擊方法還是長(zhǎng)按方法,如果是長(zhǎng)按方法的則獲取圖片的所在頁(yè)面中的位置,設(shè)置圖片的位置為點(diǎn)擊屏幕的位置,改變圖片的布局方式,在ontouchmove方法中設(shè)置圖片的位置跟隨觸摸屏幕的位置變化。4、移動(dòng)圖片后松開(kāi)手時(shí),觸發(fā)ontouchend方法,判斷手指離開(kāi)后,圖片所在的位置是否處在刪除的區(qū)域當(dāng)中,如果在則刪除圖片,并且重新渲染圖片列表,重新添加touch方法。如果不在刪除的區(qū)域中,則進(jìn)行圖片位置排序,排序后還原圖片樣式。并強(qiáng)制重新渲染圖片列表。

代碼如下:

Compress(img,e.path[0].height,e.path[0].width,(newImg:any) => { this.imgList.push(newImg) this.isLoading = false // 在這里給加入方法 setTimeout(() => { this.addTouchEvent() });})addTouchEvent(){ let domList:any = document.querySelectorAll(’.show-img’) if (domList) { let domMoveFlag:boolean = true domList.forEach((item:any,index:any) => { item.ontouchstart = null item.ontouchmove = null item.ontouchend = null item.ontouchstart = (startEvent:any) => { startEvent.preventDefault() console.log(startEvent) this.touchStartTime = new Date() setTimeout(() => { if (domMoveFlag) {console.log(’執(zhí)行元素位置操作過(guò)程’) this.showDeleteArea = true let domClient:any = item.getBoundingClientRect() console.log(domClient) this.firstPosition = { x:startEvent.changedTouches[0].pageX, y:startEvent.changedTouches[0].pageY } item.style.position = ’fixed’ item.style.height = domClient.height + ’px’ item.style.width = domClient.width + ’px’ item.style.top = domClient.top + ’px’ item.style.left = domClient.left + ’px’ item.style.padding = 0 item.style.zIndex = 9 // 添加拖拽事件 item.ontouchmove = (moveEvent:any) => { // console.log(moveEvent) item.style.top = moveEvent.changedTouches[0].pageY - this.firstPosition.y + domClient.top + ’px’ item.style.left = moveEvent.changedTouches[0].pageX - this.firstPosition.x + domClient.left + ’px’ } } }, 600); } item.ontouchend = (endEvent:any) => { let time:any = new Date() console.log(time - this.touchStartTime) if (time - this.touchStartTime <= 400) { domMoveFlag = false item.click() setTimeout(() => { this.addTouchEvent() }); } else { let newItemCenter:any = item.getBoundingClientRect() let centerY:any = newItemCenter.top + newItemCenter.height / 2 let centerX:any = newItemCenter.left + newItemCenter.width / 2 let deleteDom:any = document.querySelector('.deleteImg') let deleteArea:any = deleteDom.getBoundingClientRect() if (centerY >= deleteArea.top) { let _imgList = JSON.parse(JSON.stringify(this.imgList)) let currentImg:any = _imgList.splice(index,1) this.imgList = [] this.showDeleteArea = false setTimeout(() => { this.imgList = _imgList setTimeout(() => {this.addTouchEvent()}); }); return } this.showDeleteArea = false // 計(jì)算所有圖片元素所在頁(yè)面位置 let domParentList:any = document.querySelectorAll(’.imgCtn’) domParentList && domParentList.forEach((domParent:any,cindex:any) => { let domPos:any = (domParent.getBoundingClientRect()) if (centerY >= domPos.top&& centerY <= domPos.bottom&& centerX >= domPos.left && centerX <= domPos.right ) { // 重新排序 console.log(’在目標(biāo)區(qū)域內(nèi),重新排序’) let _imgList = JSON.parse(JSON.stringify(this.imgList)) let currentImg:any = _imgList.splice(index,1) _imgList.splice(cindex,0,...currentImg) this.imgList = [] setTimeout(() => {this.imgList = _imgListsetTimeout(() => { this.addTouchEvent() }); }); } }); // 還原樣式 item.style.position = ’absolute’; item.style.height = ’100%’ item.style.width = ’100%’ item.style.top = ’0’ item.style.left = ’0’ item.style.padding = ’10px’ } } }) }}

至此,圖片的上傳、壓縮、拖拽排序、拖拽刪除功能就已經(jīng)完成了。

到此這篇關(guān)于基于Vue2實(shí)現(xiàn)移動(dòng)端圖片上傳、壓縮、拖拽排序、拖拽刪除功能的文章就介紹到這了,更多相關(guān)vue實(shí)現(xiàn)圖片上傳拖拽排序內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 兴义市| 卢氏县| 台南县| 兴安县| 寿光市| 句容市| 安顺市| 綦江县| 河东区| 辽宁省| 清水河县| 石阡县| 陇南市| 宁国市| 岳西县| 城口县| 沛县| 澳门| 于都县| 海口市| 南涧| 长垣县| 霍林郭勒市| 格尔木市| 民和| 千阳县| 平远县| 江源县| 大竹县| 弥渡县| 汤阴县| 钟祥市| 嘉义市| 泰兴市| 三明市| 通海县| 九寨沟县| 聂拉木县| 高邑县| 四子王旗| 云安县|