国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 熱點(diǎn) > 微信 > 正文

微信小程序?qū)崿F(xiàn)通過(guò)雙向滑動(dòng)縮放圖片大小的方法

2024-07-22 01:19:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了微信小程序?qū)崿F(xiàn)通過(guò)雙向滑動(dòng)縮放圖片大小的方法。分享給大家供大家參考,具體如下:

在做小程序開發(fā)的過(guò)程中,后端傳來(lái)一張圖片地圖,需要實(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 真正代碼的開始  // 一開始我并沒(méi)有這個(gè)回調(diào)函數(shù),會(huì)出現(xiàn)縮小的時(shí)候有瞬間被放大過(guò)程的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>

最后,通過(guò)手機(jī)預(yù)覽,就會(huì)發(fā)現(xiàn)已達(dá)到預(yù)想的效果!

希望本文所述對(duì)大家微信小程序開發(fā)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 灵石县| 达日县| 如东县| 如皋市| 清涧县| 比如县| 沅陵县| 克东县| 穆棱市| 黄平县| 汉寿县| 广安市| 凌海市| 依兰县| 南漳县| 新郑市| 黔西县| 潍坊市| 河源市| 利川市| 海林市| 蒙城县| 康马县| 潍坊市| 汉沽区| 广南县| 广宗县| 盐山县| 浦县| 嘉祥县| 竹北市| 阿拉善左旗| 正镶白旗| 沙雅县| 郓城县| 万盛区| 应用必备| 德保县| 紫金县| 疏勒县| 台东县|