微信小程序?qū)崿F(xiàn)可拖動懸浮圖標(biāo)的示例代碼
更新時間:2023年12月25日 15:52:51 作者:雪芽藍(lán)域zzs
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)可拖動懸浮圖標(biāo)的示例代碼,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
效果圖

(1)wxml
<view class="float-layout" bindtap="floatClick" catchtouchmove="buttonMove" bindtouchstart="buttonStart" bindtouchend="buttonEnd" style="top:{{buttonTop}}px;left:{{buttonLeft}}px;">
<image class='float-img' src="../icon_service.png" mode="scaleToFill" />
<view class="float-txt" >問題反饋</view>
</view>(2)js
var startPoint
Page({
data: {
//按鈕位置參數(shù)
buttonTop: 0,
buttonLeft: 0,
windowHeight: '',
windowWidth: '',
},
onLoad(options) {
var that = this;
wx.getSystemInfo({
success: function (res) {
// 高度,寬度 單位為px
that.setData({
windowHeight: res.windowHeight,
windowWidth: res.windowWidth,
buttonTop: res.windowHeight * 0.50, //這里定義按鈕的初始位置
buttonLeft: res.windowWidth * 0.75, //這里定義按鈕的初始位置
})
}
})
},
/**
* 可拖動懸浮按鈕點(diǎn)擊事件
*/
floatClick: function () {
wx.showToast({
title: 'd點(diǎn)擊了',
icon: 'success',
duration: 1000
})
},
//以下是按鈕拖動事件
buttonStart: function (e) {
startPoint = e.touches[0] //獲取拖動開始點(diǎn)
},
buttonMove: function (e) {
var endPoint = e.touches[e.touches.length - 1] //獲取拖動結(jié)束點(diǎn)
//計(jì)算在X軸上拖動的距離和在Y軸上拖動的距離
var translateX = endPoint.clientX - startPoint.clientX
var translateY = endPoint.clientY - startPoint.clientY
startPoint = endPoint //重置開始位置
var buttonTop = this.data.buttonTop + translateY
var buttonLeft = this.data.buttonLeft + translateX
//判斷是移動否超出屏幕
if (buttonLeft + 50 >= this.data.windowWidth) {
buttonLeft = this.data.windowWidth - 50;
}
if (buttonLeft <= 0) {
buttonLeft = 0;
}
if (buttonTop <= 0) {
buttonTop = 0
}
if (buttonTop + 50 >= this.data.windowHeight) {
buttonTop = this.data.windowHeight - 50;
}
this.setData({
buttonTop: buttonTop,
buttonLeft: buttonLeft
})
},
buttonEnd: function (e) {}
})(3)wxss
@import "../../public/wxss/base.wxss";
/**可拖動懸浮按鈕樣式表**/
.float-layout{
position: fixed;
padding: 15rpx 30rpx;
background-color: rgba(255, 255, 255, 0.755);
border-radius: 30%;
display: flex;
align-items: center;
justify-content: center;
z-index: 99999;
box-shadow: 1px 1px 1px 1px #ede7e7;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.float-img{
width: 75rpx;
height: 75rpx;
}
.float-txt{
left:23%;
top:27%;
font-weight: 800;
font-size: 32rpx;
color: #3691FB;
}到此這篇關(guān)于微信小程序 實(shí)現(xiàn)可拖動懸浮圖標(biāo)的文章就介紹到這了,更多相關(guān)微信小程序懸浮圖標(biāo) 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
純javascript實(shí)現(xiàn)自動發(fā)送郵件
當(dāng)我們發(fā)送郵件時,可以自定義郵件發(fā)送的時間,那么使用代碼是如何實(shí)現(xiàn)的呢?下面通過本篇文章給大家介紹使用純javascript實(shí)現(xiàn)自動發(fā)送郵件,感興趣的朋友可以參考下2015-10-10
前端表單驗(yàn)證實(shí)戰(zhàn)策略之別讓用戶輸入垃圾數(shù)據(jù)
表單驗(yàn)證是現(xiàn)代Web前端開發(fā)中不可或缺的核心環(huán)節(jié),其本質(zhì)在于確保用戶在提交數(shù)據(jù)前,輸入的內(nèi)容符合業(yè)務(wù)邏輯、數(shù)據(jù)規(guī)范與安全要求,這篇文章主要介紹了前端表單驗(yàn)證實(shí)戰(zhàn)策略之別讓用戶輸入垃圾數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2026-05-05
js實(shí)現(xiàn)base64文件的處理以及下載方法
Base64是一種將二進(jìn)制數(shù)據(jù)編碼為ASCII字符的編碼方式,這篇文章主要給大家介紹了關(guān)于js實(shí)現(xiàn)base64文件的處理以及下載的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
Javascript查詢DBpedia小應(yīng)用實(shí)例學(xué)習(xí)
本文則嘗試?yán)肧PARQLWrapper.js來讀取DBpedia的數(shù)據(jù),并顯示出來,感興趣的你可以參考下,或許對你有所幫助2013-03-03
js與jquery獲取input輸入框中的值實(shí)例講解
在本篇文章里小編給大家整理了關(guān)于js與jquery獲取input輸入框中的值實(shí)例講解,需要的朋友們可以學(xué)習(xí)參考下。2020-02-02

