微信小程序?qū)崿F(xiàn)拖動(dòng)懸浮圖標(biāo)效果
- 小程序上是實(shí)現(xiàn)拖動(dòng)圖標(biāo)
- 效果

index.wxml
<view>
<view class="move-box" catchtouchmove="buttonMove" bindtouchstart="buttonStart" style="top:{{buttonTop}}px;left:{{buttonLeft}}px;" >
懸浮圖標(biāo)
</view>
</view>index.ts
let startPoint: any;
Page({
/**
* 頁面的初始數(shù)據(jù)
*/
data: {
//按鈕位置參數(shù)
buttonTop: 0,
buttonLeft: 0,
windowHeight: '',
windowWidth: '',
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad() {
},
buttonInit() {
// 獲取圖標(biāo)控件適配參數(shù)
var that = this;
wx.getSystemInfo({
success: function (res: any) {
// 屏幕寬度、高度
// 高度,寬度 單位為px
that.setData({
windowHeight: res.windowHeight,
windowWidth: res.windowWidth,
buttonTop: res.windowHeight * 0.70, // 這里定義按鈕的初始位置
buttonLeft: res.windowWidth * 0.70, // 這里定義按鈕的初始位置
})
}
})
},
//以下是按鈕拖動(dòng)事件
buttonStart: function (e: any) {
startPoint = e.touches[0]//獲取拖動(dòng)開始點(diǎn)
},
buttonMove: function (e: any) {
const endPoint = e.touches[e.touches.length - 1]//獲取拖動(dòng)結(jié)束點(diǎn)
//計(jì)算在X軸上拖動(dòng)的距離和在Y軸上拖動(dòng)的距離
const translateX = endPoint.clientX - startPoint.clientX
const translateY = endPoint.clientY - startPoint.clientY
startPoint = endPoint//重置開始位置
let buttonTop: any = this.data.buttonTop + translateY
let buttonLeft: any = this.data.buttonLeft + translateX
//判斷是移動(dòng)否超出屏幕
const windowWidth: any = this.data.windowWidth;
const windowHeight: any = this.data.windowHeight;
if (buttonLeft + 60 >= windowWidth) {
buttonLeft = windowWidth - 60;
}
if (buttonLeft <= 0) {
buttonLeft = 0;
}
if (buttonTop <= 0) {
buttonTop = 0
}
if (buttonTop + 60 >= windowHeight) {
buttonTop = windowHeight - 60 - 40;
}
this.setData({
buttonTop: buttonTop,
buttonLeft: buttonLeft
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面顯示
*/
onShow() {
this.buttonInit();
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面隱藏
*/
onHide() {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面卸載
*/
onUnload() {
},
/**
* 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作
*/
onPullDownRefresh() {
},
/**
* 頁面上拉觸底事件的處理函數(shù)
*/
onReachBottom() {
},
/**
* 用戶點(diǎn)擊右上角分享
*/
onShareAppMessage() {
}
})index.wxss
.move-box {
position: fixed;
width: 45px;
height: 45px;
background-color: aquamarine;
border-radius: 50%;
font-size:12px;
text-align: center;
padding: 5px;
box-sizing: border-box;
color:blueviolet;
font-weight: 600;
}index.json
{
"navigationBarTitleText":"拖動(dòng)懸浮圖標(biāo)",
"usingComponents": {}
}到此這篇關(guān)于微信小程序?qū)崿F(xiàn)拖動(dòng)懸浮圖標(biāo)效果的文章就介紹到這了,更多相關(guān)小程序拖動(dòng)懸浮圖標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js中symbol類型以及symbol的三大應(yīng)用場景詳解
Symbol是ES6新推出的一種基本類型,它表示獨(dú)一無二的值,它可以接受一個(gè)字符串作為參數(shù),帶有相同參數(shù)的兩個(gè)Symbol值不相等,這個(gè)參數(shù)只是表示Symbol值的描述而已,下面這篇文章主要給大家介紹了關(guān)于js中symbol類型以及symbol的三大應(yīng)用場景,需要的朋友可以參考下2022-09-09
JavaScript跳出循環(huán)的幾種常用方法總結(jié)
這篇文章主要給大家介紹了關(guān)于JavaScript跳出循環(huán)的幾種常用方法,continue跳過當(dāng)前迭代,break終止循環(huán),label+break跳出多層循環(huán),return退出函數(shù),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-06-06
在Js頁面通過POST傳遞參數(shù)跳轉(zhuǎn)到新頁面詳解
這篇文章主要給大家介紹了關(guān)于在Js頁面通過POST傳遞參數(shù)跳轉(zhuǎn)到新頁面的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
JavaScript直接調(diào)用函數(shù)與call調(diào)用的區(qū)別實(shí)例分析
這篇文章主要介紹了JavaScript直接調(diào)用函數(shù)與call調(diào)用的區(qū)別,結(jié)合額實(shí)例形式分析了JavaScript直接調(diào)用函數(shù)與call調(diào)用的基本用法、區(qū)別及相關(guān)注意事項(xiàng),需要的朋友可以參考下2020-05-05
Vue之vue-tree-color組件實(shí)現(xiàn)組織架構(gòu)圖案例詳解
這篇文章主要介紹了Vue之vue-tree-color組件實(shí)現(xiàn)組織架構(gòu)圖案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09

