在做小程序开发的历程中,,,后端传来一张图片地图,,,需要实现双手指滑动,,,使图片缩放,,,最终得出了一下代码:

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('双手指触发最先')
// 注重touchstartCallback 真正代码的最先
// 一最先我并没有这个回调函数,,,会泛起缩小的时间有瞬间被放大历程的bug
// 当两根手指放上去的时间,,,就将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('双手指运动最先')
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 这个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
});
}
})然后将新获得的图片宽度和高度赋值给图片即可实现滑动缩放
wxml:
<image mode='scaleToFill' src='../../../images/01.jpg' bindtouchstart='touchStartHandle' bindtouchmove='touchMoveHandle' bindload='load' style="width: {{ touch.scaleWidth }}px;height: {{ touch.scaleHeight }}px"></image>最后,,,通过手机预览,,,就会发明已抵达预想的效果!
KESION pp电子软件
KESION pp电子软件是海内领先的在线教育软件及私域社交电商软件服务提供商,,,恒久专注于为企业提供在线教育软件及社交电商SaaS平台解决方案。。
公司焦点产品云开店SaaS社交电商服务平台、在线教育SaaS服务平台、教育企业数字化SaaS云平台、企微营销助手、私有化自力安排品牌网校和在线教育咨询等。。KESION 一直通过手艺立异,,,提供产品和服务,,,助力企业向数字化转型,,,通过科技驱动商业刷新,,,让商业变得更智慧!
微信小程序图片开发一般包括加载外地图片,,,加载服务器图片,,,已经配景图,,,那么微信小程序下面为各人介绍图片处理的常用的小程序图片使用样式。。...
小游戏的火热,,,让许多开发者纷纷入局小游戏市场,,,那么怎样用微信开发小游戏,,,开发小游戏需要注重哪些问题??????...