微信小程序?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({ /** * 頁(yè)面的初始數(shù)據(jù) */ data: { //按鈕位置參數(shù) buttonTop: 0, buttonLeft: 0, windowHeight: '', windowWidth: '', }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載 */ 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)聽(tīng)頁(yè)面初次渲染完成 */ onReady() { }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示 */ onShow() { this.buttonInit(); }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面隱藏 */ onHide() { }, /** * 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面卸載 */ onUnload() { }, /** * 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽(tīng)用戶下拉動(dòng)作 */ onPullDownRefresh() { }, /** * 頁(yè)面上拉觸底事件的處理函數(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)文章
45個(gè)JavaScript編程注意事項(xiàng)、技巧大全
這篇文章主要介紹了45個(gè)JavaScript編程注意事項(xiàng)、技巧大全,在這篇文章里,我將分享一些JavaScript的技巧、秘訣和最佳實(shí)踐,除了少數(shù)幾個(gè)外,不管是瀏覽器的JavaScript引擎,還是服務(wù)器端JavaScript解釋器,均適用,需要的朋友可以參考下2015-02-02淺談bootstrap源碼分析之scrollspy(滾動(dòng)偵聽(tīng))
下面小編就為大家?guī)?lái)一篇淺談bootstrap源碼分析之scrollspy(滾動(dòng)偵聽(tīng))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06從盛大通行證上摘下來(lái)的身份證驗(yàn)證js代碼
偶然發(fā)現(xiàn)盛大通行證上的身份證驗(yàn)證代碼,特扒下來(lái),方便大家的使用。2011-01-01JS監(jiān)聽(tīng)dom高度變化幾種常用方法總結(jié)
我們?cè)陂_發(fā)中會(huì)遇到一些需求,需要監(jiān)聽(tīng)元素變化,比如元素屬性變化,元素大小變化,這篇文章主要給大家介紹了關(guān)于JS監(jiān)聽(tīng)dom高度變化幾種常用方法的相關(guān)資料,需要的朋友可以參考下2023-10-10JS判斷字符串是否為整數(shù)的方法--簡(jiǎn)單的正則判斷
今天小編就為大家分享一篇JS判斷字符串是否為整數(shù)的方法--簡(jiǎn)單的正則判斷,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07JS Thunk 函數(shù)的含義和用法實(shí)例總結(jié)
這篇文章主要介紹了JS Thunk 函數(shù)的含義和用法,結(jié)合實(shí)例形式總結(jié)分析了JS Thunk 函數(shù)的具體含義、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04使用BootStrap實(shí)現(xiàn)標(biāo)簽切換原理解析
本文給大家分享使用BootStrap實(shí)現(xiàn)標(biāo)簽切換原理解析及核心代碼,需要的朋友參考下2017-03-03