微信小程序?qū)崿F(xiàn)可長按移動(dòng)控件
微信小程序可長按移動(dòng)控件,供大家參考,具體內(nèi)容如下
–包含超出邊界監(jiān)測,性能不夠理想【原因:setData在毫秒級(jí)刷新時(shí)會(huì)極大程度影響渲染性能】
Javascript
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
//浮動(dòng)按鈕坐標(biāo)
flow_y: 130,
flow_x: 20,
pre_y:0,
pre_x:0,
//標(biāo)記移動(dòng)
moveFlag: false,
//標(biāo)記控件是否可放置
canNotPlace: false,
imgSrc:"",
},
//長按按鈕移動(dòng)控件
longPress: function (e) {
console.log(e)
this.setData({
moveFlag: true,
pre_x:e.touches[0].clientX,
pre_y:e.touches[0].clientY,
})
// console.log(e)
console.log("begin:(", this.data.flow_x, this.data.flow_y, ")")
// this.setData({
// flow_x: 1334-e.touches[0].pageX, // 獲取觸摸時(shí)的原點(diǎn)
// flow_y: 750-e.touches[0].pageY,
// })
//console.log(startX)
},
// 觸摸結(jié)束事件
touchEnd: function (e) {
this.setData({
moveFlag: false
})
// console.log("flow:(", this.data.flow_x, this.data.flow_y, ")")
//監(jiān)測按鈕放置區(qū)域
if (!((this.data.flow_x < 730) && (this.data.flow_x > 20))&&((this.data.flow_y < 1150) && (this.data.flow_y > 130))) {
this.setData({
flow_x: 20, // 轉(zhuǎn)換rpx
flow_y: 130,
})
}
// this.setData({
// flow_x: 20, // 轉(zhuǎn)換rpx
// })
},
touchMove: function (e) {
//根據(jù)move差值動(dòng)態(tài)布局
if (this.data.moveFlag) {
// console.log("page:(", e.touches[0].pageX, e.touches[0].pageY, ")")
// console.log(move_x,move_y)
var x =this.data.flow_x -(e.touches[0].clientX-this.data.pre_x)*2
var y =this.data.flow_y -(e.touches[0].clientY-this.data.pre_y)*2
this.setData({
flow_x: x, // 轉(zhuǎn)換rpx
flow_y: y,
})
// console.log(x,y)
// this.setData({
// flow_x: x, // 轉(zhuǎn)換rpx
// flow_y: y,
// })
if (!(((x < 730) && (x > 20))&&((y < 1000) && (y > 130)))) {
this.setData({
canNotPlace: true,
})
} else {
this.setData({
canNotPlace: false,
})
}
this.setData({
pre_x:e.touches[0].clientX,
pre_y:e.touches[0].clientY,
})
}
},
catchtouchmove: function () {
//防止蒙層觸摸穿透
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function (options) {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
*/
onReady: function () {
wx.hideLoading()
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面卸載
*/
onUnload: function () {
},
/**
* 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作
*/
onPullDownRefresh: function () {
this.getArticle()
},
/**
* 頁面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
},
/**
* 用戶點(diǎn)擊右上角分享
*/
onShareAppMessage: function () {
}
})
WXSS
.follow_icon{
position: fixed;
/* margin: 0 10rpx; */
width:70rpx;
height:70rpx;
bottom: var(--flow_y--);
right: var(--flow_x--);
z-index: 1001;
}
/*mask*/
.drawer_screen {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 5;
background: #000;
opacity: 0.5;
overflow: hidden;
}
WXML
<!-- 刷新按鈕 -->
<!-- <view bindtap='getArticle' style="opacity:0.8"> -->
<view catchtouchmove="catchtouchmove" ><image bindtap='getArticle' bindlongpress="longPress" bindtouchmove='touchMove' bindtouchend="touchEnd" style="--flow_y--:{{flow_y}}rpx;--flow_x--:{{flow_x}}rpx;" class="follow_icon" src="{{imgSrc}}"></image></view>
<!-- 蒙版屏蔽,防止觸控穿透 -->
<view style="{{!canNotPlace?'background: #777;':'background: #b81e1e;'}}" catchtouchmove="catchtouchmove" class="drawer_screen" data-statu="open" wx:if="{{moveFlag}}"></view>
<!-- </view> -->



為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
原生JS封裝animate運(yùn)動(dòng)框架的實(shí)例
下面小編就為大家?guī)硪黄鶭S封裝animate運(yùn)動(dòng)框架的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
JavaScript實(shí)現(xiàn)跑馬燈抽獎(jiǎng)活動(dòng)實(shí)例代碼解析與優(yōu)化(二)
這篇文章主要介紹了JavaScript實(shí)現(xiàn)跑馬燈抽獎(jiǎng)活動(dòng)實(shí)例代碼解析與優(yōu)化(二)的相關(guān)資料,需要的朋友可以參考下2016-02-02
js點(diǎn)擊文本框后才加載驗(yàn)證碼實(shí)例代碼
這篇文章是一段關(guān)于js點(diǎn)擊文本框后才加載驗(yàn)證碼實(shí)例代碼,而不是直接顯示驗(yàn)證碼,感興趣的小伙伴們可以參考一下2015-10-10
談?wù)勎覍?duì)JavaScript原型和閉包系列理解(隨手筆記8)
這篇文章主要介紹了談?wù)勎覍?duì)JavaScript原型和閉包系列理解(隨手筆記8)的相關(guān)資料,需要的朋友可以參考下2015-12-12
詳解如何構(gòu)建Promise隊(duì)列實(shí)現(xiàn)異步函數(shù)順序執(zhí)行
這篇文章主要介紹了詳解如何構(gòu)建Promise隊(duì)列實(shí)現(xiàn)異步函數(shù)順序執(zhí)行,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
JavaScript延遲加載之a(chǎn)sync與defer的應(yīng)用
這篇文章主要介紹了JavaScript延遲加載之a(chǎn)sync與defer的應(yīng)用場景與使用區(qū)別的介紹,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09
JavaScript實(shí)現(xiàn)簡單音樂播放器
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡單音樂播放器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11

