小程序?qū)崿F(xiàn)圖片移動(dòng)縮放效果
本文實(shí)例為大家分享了小程序?qū)崿F(xiàn)圖片移動(dòng)縮放效果的具體代碼,供大家參考,具體內(nèi)容如下
git地址 , 如果對(duì)您有幫助給個(gè)start唄
嘗試了movable-view標(biāo)簽是很方便, 但是我想有個(gè)拉伸按鈕去縮放圖片, 于是嘗試自己寫(xiě)了.
思想利用catchtouchmove屬性計(jì)算偏移量, 實(shí)時(shí)更新坐標(biāo)

以下是完整代碼
js
/**
* All right by NieYinlong
*/
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
bgBoxHeight: 400, // 背景的高度
bgBoxWidth: 320, // 背景的寬度
moveImgLeft: 40,
moveImgTop: 80,
moveImgH: 100,
moveImgW: 100,
scaleIconFixWidth: 30,
scaleLeft: 0, // 拉伸按鈕默認(rèn)x坐標(biāo) (拉伸按鈕默認(rèn)寬高30)
scaleTop: 0, // 拉伸按鈕默認(rèn)y坐標(biāo)
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad: function (options) {
const halfWidth = this.data.scaleIconFixWidth / 2
this.setData({
scaleLeft: this.data.moveImgLeft + this.data.moveImgW - halfWidth,
scaleTop: this.data.moveImgTop + this.data.moveImgH - halfWidth
})
},
// 圖片移動(dòng)
moveImgTouchmove: function(e) {
console.log(e)
let pageX = e.changedTouches[0].pageX
let pageY = e.changedTouches[0].pageY
// (this.data.moveImgW / 2)是因?yàn)橛|摸放到中間位置
let toLeft = pageX - this.data.moveImgW / 2;
let toTop = pageY - this.data.moveImgH / 2;
const halfWidth = this.data.scaleIconFixWidth / 2
// 限制x左邊不能出邊框
if (pageX - (this.data.moveImgW / 2) <= 0) {
return;
}
// 限制右邊不能出超過(guò)邊框
if ((pageX + (this.data.moveImgW / 2)) >= (this.data.bgBoxWidth)) {
return;
}
// 限制top
if (pageY - (this.data.moveImgH / 2) <= 1) {
return;
}
// 限制bottom
if ((pageY + (this.data.moveImgH / 2)) >= this.data.bgBoxHeight) {
return;
}
this.setData({
moveImgLeft: toLeft,
moveImgTop: toTop,
scaleLeft: toLeft + this.data.moveImgW - halfWidth,
scaleTop: toTop + this.data.moveImgH - halfWidth
})
},
// 拉伸按鈕移動(dòng)
scaleTouchmove: function (e) {
console.log(e)
let pageX = e.changedTouches[0].pageX
let pageY = e.changedTouches[0].pageY
const halfWidth = this.data.scaleIconFixWidth / 2
let toLeft = pageX - halfWidth // 減去halfWidth是拉伸按鈕寬度的一半
let toTop = pageY - halfWidth
let changedW = pageX - this.data.moveImgLeft
let changedH = pageY - this.data.moveImgTop
// 限制最moveImg小尺寸
if (toLeft <= (this.data.moveImgLeft + halfWidth)) {
return;
}
if (toTop <= (this.data.moveImgTop + halfWidth)) {
return;
}
// 限制moveImg最大尺寸
if(pageX - this.data.moveImgLeft > 250) {
// 寬度達(dá)到最大值
return;
}
if (pageY - this.data.moveImgTop > 250) {
// 高度達(dá)到最大值
return;
}
// 限制拉伸按鈕的right
if(this.data.scaleLeft + this.data.scaleIconFixWidth >= this.data.bgBoxWidth) {
return;
}
// 限制拉伸按鈕的bottom
if (this.data.scaleTop + this.data.scaleIconFixWidth >= this.data.bgBoxHeight) {
return;
}
this.setData({
scaleLeft: toLeft,
scaleTop: toTop,
moveImgW: pageX - this.data.moveImgLeft,
moveImgH: pageY - this.data.moveImgTop,
})
},
})
wxml
<view
class='bgBox'
style="height:{{bgBoxHeight}}px; width:{{bgBoxWidth}}px"
>
<image
class='movedImg'
src='../../image/moveImg.png'
catchtouchmove='moveImgTouchmove'
style="width:{{moveImgW}}px;height:{{moveImgH}}px; left:{{moveImgLeft}}px;top:{{moveImgTop}}px"
/>
<image
src='../../image/spreadIcon.png'
class='scaleIcon'
catchtouchmove='scaleTouchmove'
style="width:{{scaleIconFixWidth}}px;height:{{scaleIconFixWidth}}px; left:{{scaleLeft}}px; top:{{scaleTop}}px"
/>
</view>
wxss
.bgBox {
border: 2px solid green;
height: 400px;
width: 99vw;
}
.movedImg {
position: absolute;
border: 3px dotted rgb(255, 166, 0);
}
.scaleIcon {
position: absolute;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js 事件對(duì)象 鼠標(biāo)滾輪效果演示說(shuō)明
第三篇,繼續(xù)總結(jié)事件對(duì)象 先看看監(jiān)聽(tīng)鼠標(biāo)滾輪事件能幫我們做什么2010-05-05
一文解決前端JS小數(shù)運(yùn)算精度問(wèn)題
在做項(xiàng)目的時(shí)候,前端需要在表格的底部做一個(gè)匯總的功能,在采用reduce對(duì)當(dāng)前屬性所有值匯總時(shí),發(fā)現(xiàn)匯總結(jié)果存在好長(zhǎng)的小數(shù)位,本文給大家介紹了如何解決前端JS小數(shù)運(yùn)算精度問(wèn)題,需要的朋友可以參考下2024-02-02
Javascript 各瀏覽器的 Javascript 效率對(duì)比
2008-01-01
javascript用DIV模擬彈出窗口_窗體滾動(dòng)跟隨
可滾動(dòng)跟隨彈出框效果代碼,非常實(shí)用的應(yīng)用于網(wǎng)絡(luò)廣告2008-09-09
JavaScript創(chuàng)建對(duì)象的幾種方式及關(guān)于this指向問(wèn)題
這篇文章主要介紹了JavaScript創(chuàng)建對(duì)象的幾種方式及關(guān)于this指向問(wèn)題,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值。需要的小伙伴可以參考一下2022-07-07
JavaScript中this的學(xué)習(xí)筆記及用法整理
在本篇文章里小編給大家整理的是關(guān)于JavaScript中this的使用以及代碼實(shí)例,需要的朋友們學(xué)習(xí)下。2020-02-02
js性能優(yōu)化之?dāng)?shù)組模式實(shí)例詳解
這篇文章主要為大家介紹了js性能優(yōu)化之?dāng)?shù)組模式實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
一次微信小程序內(nèi)地圖的使用實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于一次微信小程序內(nèi)地圖使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用微信小程序具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

