微信小程序?qū)崿F(xiàn)拖動懸浮圖標效果
更新時間:2024年04月17日 10:12:15 作者:不完美的完美
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)拖動懸浮圖標效果,小程序上是實現(xiàn)拖動圖標,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
- 小程序上是實現(xiàn)拖動圖標
- 效果
index.wxml
<view> <view class="move-box" catchtouchmove="buttonMove" bindtouchstart="buttonStart" style="top:{{buttonTop}}px;left:{{buttonLeft}}px;" > 懸浮圖標 </view> </view>
index.ts
let startPoint: any; Page({ /** * 頁面的初始數(shù)據(jù) */ data: { //按鈕位置參數(shù) buttonTop: 0, buttonLeft: 0, windowHeight: '', windowWidth: '', }, /** * 生命周期函數(shù)--監(jiān)聽頁面加載 */ onLoad() { }, buttonInit() { // 獲取圖標控件適配參數(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, // 這里定義按鈕的初始位置 }) } }) }, //以下是按鈕拖動事件 buttonStart: function (e: any) { startPoint = e.touches[0]//獲取拖動開始點 }, buttonMove: function (e: any) { const endPoint = e.touches[e.touches.length - 1]//獲取拖動結(jié)束點 //計算在X軸上拖動的距離和在Y軸上拖動的距離 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 //判斷是移動否超出屏幕 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)聽用戶下拉動作 */ onPullDownRefresh() { }, /** * 頁面上拉觸底事件的處理函數(shù) */ onReachBottom() { }, /** * 用戶點擊右上角分享 */ 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":"拖動懸浮圖標", "usingComponents": {} }
到此這篇關(guān)于微信小程序?qū)崿F(xiàn)拖動懸浮圖標效果的文章就介紹到這了,更多相關(guān)小程序拖動懸浮圖標內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談bootstrap源碼分析之scrollspy(滾動偵聽)
下面小編就為大家?guī)硪黄獪\談bootstrap源碼分析之scrollspy(滾動偵聽)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06JS監(jiān)聽dom高度變化幾種常用方法總結(jié)
我們在開發(fā)中會遇到一些需求,需要監(jiān)聽元素變化,比如元素屬性變化,元素大小變化,這篇文章主要給大家介紹了關(guān)于JS監(jiān)聽dom高度變化幾種常用方法的相關(guān)資料,需要的朋友可以參考下2023-10-10JS Thunk 函數(shù)的含義和用法實例總結(jié)
這篇文章主要介紹了JS Thunk 函數(shù)的含義和用法,結(jié)合實例形式總結(jié)分析了JS Thunk 函數(shù)的具體含義、用法及操作注意事項,需要的朋友可以參考下2020-04-04