溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序如何實(shí)現(xiàn)圖片雙滑縮放大小

發(fā)布時(shí)間:2022-03-15 10:55:10 來源:億速云 閱讀:1774 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了微信小程序如何實(shí)現(xiàn)圖片雙滑縮放大小 ,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

在做小程序開發(fā)的過程中,后端傳來一張圖片地圖,需要實(shí)現(xiàn)雙手指滑動(dòng),使圖片縮放,最終得出了一下代碼:

  js :

   Page({
  data: {
    touch: {
      distance: 0,
      scale: 1,
      baseWidth: null,
      baseHeight: null,
      scaleWidth: null,
      scaleHeight: null
    }
  },
  touchStartHandle(e) {
    // 單手指縮放開始,也不做任何處理 
    if (e.touches.length == 1) {
      console.log("單滑了")
      return
    }
    console.log('雙手指觸發(fā)開始')
    // 注意touchstartCallback 真正代碼的開始 
    // 一開始我并沒有這個(gè)回調(diào)函數(shù),會(huì)出現(xiàn)縮小的時(shí)候有瞬間被放大過程的bug 
    // 當(dāng)兩根手指放上去的時(shí)候,就將distance 初始化。 
    let xMove = e.touches[1].clientX - e.touches[0].clientX;
    let yMove = e.touches[1].clientY - e.touches[0].clientY;
    let distance = Math.sqrt(xMove * xMove + yMove * yMove);
    this.setData({
      'touch.distance': distance,
    })
  },
  touchMoveHandle(e) {
    let touch = this.data.touch
    // 單手指縮放我們不做任何操作 
    if (e.touches.length == 1) {
      console.log("單滑了");
      return
    }
    console.log('雙手指運(yùn)動(dòng)開始')
    let xMove = e.touches[1].clientX - e.touches[0].clientX;
    let yMove = e.touches[1].clientY - e.touches[0].clientY;
    // 新的 ditance 
    let distance = Math.sqrt(xMove * xMove + yMove * yMove);
    let distanceDiff = distance - touch.distance;
    let newScale = touch.scale + 0.005 * distanceDiff
    // 為了防止縮放得太大,所以scale需要限制,同理最小值也是 
    if (newScale >= 2) {
      newScale = 2
    }
    if (newScale <= 0.6) {
      newScale = 0.6
    }
    let scaleWidth = newScale * touch.baseWidth
    let scaleHeight = newScale * touch.baseHeight
    // 賦值 新的 => 舊的 
    this.setData({
      'touch.distance': distance,
      'touch.scale': newScale,
      'touch.scaleWidth': scaleWidth,
      'touch.scaleHeight': scaleHeight,
      'touch.diff': distanceDiff
    })
  },
  load: function (e) {
    // bindload 這個(gè)api是<image>組件的api類似<img>的onload屬性 
    this.setData({
      'touch.baseWidth': e.detail.width,
      'touch.baseHeight': e.detail.height,
      'touch.scaleWidth': e.detail.width,
      'touch.scaleHeight': e.detail.height
    });
  }
})

然后將新獲得的圖片寬度和高度賦值給圖片即可實(shí)現(xiàn)滑動(dòng)縮放

  wxml:

<image mode='scaleToFill' src='../../../images/01.jpg' bindtouchstart='touchStartHandle' bindtouchmove='touchMoveHandle' bindload='load' style="width: {{ touch.scaleWidth }}px;height: {{ touch.scaleHeight }}px"></image>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“微信小程序如何實(shí)現(xiàn)圖片雙滑縮放大小 ”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI