欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年01月05日 11:44:11   作者:前端小白菜~  
這篇文章主要介紹了基于Vue2實現(xiàn)移動端圖片上傳、壓縮、拖拽排序、拖拽刪除功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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

github地址:代碼地址

下面直接進入正題:

圖片上傳

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

<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重繪的方法實現(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 圖片對象
 */
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))
}

拖拽排序、拖拽刪除

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

代碼如下:

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í)行元素位置操作過程')
      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
     // 計算所有圖片元素所在頁面位置
     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('在目標區(qū)域內,重新排序')
       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 = _imgList
        setTimeout(() => {
         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)完成了。

到此這篇關于基于Vue2實現(xiàn)移動端圖片上傳、壓縮、拖拽排序、拖拽刪除功能的文章就介紹到這了,更多相關vue實現(xiàn)圖片上傳拖拽排序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue關于select組件綁定的值為數(shù)字類型的問題

    vue關于select組件綁定的值為數(shù)字類型的問題

    這篇文章主要介紹了vue關于select組件綁定的值為數(shù)字類型的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue.config.js完整配置教程

    vue.config.js完整配置教程

    這篇文章主要為大家介紹了vue.config.js的完整配置教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • vue項目如何實現(xiàn)ip和localhost同時訪問

    vue項目如何實現(xiàn)ip和localhost同時訪問

    這篇文章主要介紹了vue項目如何實現(xiàn)ip和localhost同時訪問,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • element el-table如何實現(xiàn)表格動態(tài)增加/刪除/編輯表格行(帶校驗規(guī)則)

    element el-table如何實現(xiàn)表格動態(tài)增加/刪除/編輯表格行(帶校驗規(guī)則)

    這篇文章主要介紹了element el-table如何實現(xiàn)表格動態(tài)增加/刪除/編輯表格行(帶校驗規(guī)則),本篇文章記錄el-table增加一行可編輯的數(shù)據(jù)列,進行增刪改,感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • vue2+springsecurity權限系統(tǒng)的實現(xiàn)

    vue2+springsecurity權限系統(tǒng)的實現(xiàn)

    本文主要介紹了vue2+springsecurity權限系統(tǒng)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-05-05
  • 詳解Vue雙向數(shù)據(jù)綁定原理解析

    詳解Vue雙向數(shù)據(jù)綁定原理解析

    本篇文章主要介紹了詳解Vue雙向數(shù)據(jù)綁定原理解析 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Vue編寫炫酷的時鐘插件

    Vue編寫炫酷的時鐘插件

    這篇文章主要為大家詳細介紹了Vue編寫炫酷的時鐘插件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Vue press 支持圖片放大功能的實例代碼

    Vue press 支持圖片放大功能的實例代碼

    這篇文章主要介紹了 Vue press 支持圖片放大功能,本文通過代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-11-11
  • Vue項目全局配置微信分享思路詳解

    Vue項目全局配置微信分享思路詳解

    這篇文章給大家介紹了vue項目全局配置微信分享思路講解,使用vue作為框架,使用vux作為ui組件庫,具體內容詳情大家跟隨腳本之家小編一起學習吧
    2018-05-05
  • vuejs事件中心管理組件間的通信詳解

    vuejs事件中心管理組件間的通信詳解

    這篇文章主要為大家詳細介紹了vuejs事件中心管理組件間的通信,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08

最新評論