uniapp開發(fā)小程序?qū)崿F(xiàn)全局懸浮按鈕的代碼
看效果

這是一個(gè)全局的按鈕,可以換成圖片,自己寫樣式,每個(gè)頁(yè)面都有;
須知:
1.uni.getSystemInfoSync()獲取手機(jī)的信息接口
可以拿到手機(jī)屏幕的寬高


2.uni.createSelectorQuery().in(this)
uniapp中式?jīng)]有window對(duì)象,和dom元素的,但是有時(shí)我們需要獲取頁(yè)面上節(jié)點(diǎn)的一些幾何信息;
@touchcancel 手指觸摸被打斷,如來(lái)電提醒,彈窗 @touchend 手指觸摸動(dòng)作結(jié)束,如松開按鈕 @touchmove 手指觸摸后移動(dòng) @touchstart 手指觸摸動(dòng)作開始

3.touchmove滑動(dòng)事件
@touchcancel 手指觸摸被打斷,如來(lái)電提醒,彈窗 @touchend 手指觸摸動(dòng)作結(jié)束,如松開按鈕 @touchmove 手指觸摸后移動(dòng) @touchstart 手指觸摸動(dòng)作開始

記錄用戶按下屏幕的坐標(biāo) x 和 y
touchmove(e) {
// 單指觸摸
if (e.touches.length !== 1) {
return false;
}
console.log('移動(dòng)',e);
this.isMove = true;
this.left = e.touches[0].clientX - this.offsetWidth;
let clientY = e.touches[0].clientY - this.offsetHeight;
// #ifdef H5
clientY += this.height;
// #endif
let edgeBottom = this.windowHeight - this.height - this.edge;
// 上下觸及邊界
if (clientY < this.edge) {
this.top = this.edge;
} else if (clientY > edgeBottom) {
this.top = edgeBottom;
} else {
this.top = clientY
//將top存入本地存儲(chǔ)
uni.setStorageSync("top", this.top);
},
touchend(e) {
if (this.isDock) {
let edgeRigth = this.windowWidth - this.width - this.edge;
if (this.left < this.windowWidth / 2 - this.offsetWidth) {
this.left = this.edge;
} else {
this.left = edgeRigth;
}
//將left存入本地存儲(chǔ)
uni.setStorageSync("left", this.left);
this.isMove = false;
this.$emit('btnTouchend');
}
每次移動(dòng)這個(gè)按鈕,本地存儲(chǔ)的值都會(huì)改變;
取出存儲(chǔ)的值
onShow() {
//獲取手機(jī)信息配置接口
const sys = uni.getSystemInfoSync();
//屏幕的寬高
this.windowWidth = sys.windowWidth;
this.windowHeight = sys.windowHeight;
// #ifdef APP-PLUS
this.existTabBar && (this.windowHeight -= 50);
// #endif
if (sys.windowTop) {
this.windowHeight += sys.windowTop;
}
//獲取元素
const query = uni.createSelectorQuery().in(this);
query.select('#_drag_button').boundingClientRect(data => {
console.log(data);
this.width = data.width;
this.height = data.height;
this.offsetWidth = data.width / 2;
this.offsetHeight = data.height / 2;
// this.left = this.windowWidth - this.width - this.edge;
// this.top = this.windowHeight - this.height - this.edge;
this.left = uni.getStorageSync('left')
this.top=uni.getStorageSync('top')
this.$nextTick(() => {
this.firstIn = true
})
}).exec();
},
賦值
<view id="_drag_button" class="drag" :style="{top:top+'px',left:left+'px',opacity:firstIn?1:0}"
@touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend="touchend"
@click.stop.prevent="click" :class="{transition: isDock && !isMove }">
<button class="btn" open-type="contact" style="border: none;padding: 0;margin: 0;">
<image class="img"
src="圖片地址">
</image>
</button>
</view>
全局注冊(cè)組件
因?yàn)槲疫@個(gè)項(xiàng)目是vue3,所以注冊(cè)組件的時(shí)候,不需要全局引入,

這個(gè)組件,需要在每個(gè)頁(yè)面引入;
組件代碼:需要換個(gè)圖片就可以用了;
<template>
<view>
<view id="_drag_button" class="drag" :style="{top:top+'px',left:left+'px',opacity:firstIn?1:0}"
@touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend="touchend"
@click.stop.prevent="click" :class="{transition: isDock && !isMove }">
<button class="btn" open-type="contact" style="border: none;padding: 0;margin: 0;">
<image class="img"
src="圖片地址">
</image>
</button>
</view>
</view>
</template>
<script>
export default {
name: 'drag-button',
props: {
isDock: {
type: Boolean,
default: false
},
existTabBar: {
}
},
data() {
return {
top: 0,
left: 0,
width: 0,
height: 0,
offsetWidth: 0,
offsetHeight: 0,
windowWidth: 0,
windowHeight: 0,
isMove: true,
edge: 10,
text: ' ',
firstIn: false
onShow() {
//獲取手機(jī)信息配置接口
const sys = uni.getSystemInfoSync();
//屏幕的寬高
this.windowWidth = sys.windowWidth;
this.windowHeight = sys.windowHeight;
// #ifdef APP-PLUS
this.existTabBar && (this.windowHeight -= 50);
// #endif
if (sys.windowTop) {
this.windowHeight += sys.windowTop;
//獲取元素
const query = uni.createSelectorQuery().in(this);
query.select('#_drag_button').boundingClientRect(data => {
console.log(data);
this.width = data.width;
this.height = data.height;
this.offsetWidth = data.width / 2;
this.offsetHeight = data.height / 2;
// this.left = this.windowWidth - this.width - this.edge;
// this.top = this.windowHeight - this.height - this.edge;
this.left = uni.getStorageSync('left')
this.top=uni.getStorageSync('top')
this.$nextTick(() => {
this.firstIn = true
})
}).exec();
methods: {
click() {
this.$emit('btnClick');
touchstart(e) {
this.$emit('btnTouchstart');
touchmove(e) {
// 單指觸摸
if (e.touches.length !== 1) {
return false;
}
console.log('移動(dòng)',e);
this.isMove = true;
this.left = e.touches[0].clientX - this.offsetWidth;
let clientY = e.touches[0].clientY - this.offsetHeight;
// #ifdef H5
clientY += this.height;
// #endif
let edgeBottom = this.windowHeight - this.height - this.edge;
// 上下觸及邊界
if (clientY < this.edge) {
this.top = this.edge;
} else if (clientY > edgeBottom) {
this.top = edgeBottom;
} else {
this.top = clientY
uni.setStorageSync("top", this.top);
touchend(e) {
if (this.isDock) {
let edgeRigth = this.windowWidth - this.width - this.edge;
if (this.left < this.windowWidth / 2 - this.offsetWidth) {
this.left = this.edge;
} else {
this.left = edgeRigth;
}
uni.setStorageSync("left", this.left);
this.isMove = false;
this.$emit('btnTouchend');
}
}
</script>
<style lang="scss">
.drag {
display: flex;
justify-content: center;
align-items: center;
width: 180rpx;
height: 135rpx;
border-radius: 50%;
font-size: $uni-font-size-sm;
position: fixed;
z-index: 999999;
&.transition {
transition: left .3s ease, top .3s ease;
.btn {
background-color: transparent;
width: 135rpx;
height: 130rpx;
z-index: 9999;
button::after {
border: none;
.img {
height: 100%;
width: 100%;
</style>頁(yè)面引入:
<drag-button :isDock="true" :existTabBar="true" @btnClick="btnClick" @btnTouchstart="btnTouchstart" @btnTouchend="btnTouchend">
引入組件
components: {
dragButton
},

到此這篇關(guān)于uniapp開發(fā)小程序如何實(shí)現(xiàn)全局懸浮按鈕的文章就介紹到這了,更多相關(guān)uniapp小程序懸浮按鈕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uni-app開發(fā)微信小程序遇到的部分踩坑實(shí)戰(zhàn)
最近在用uni-app開發(fā)微信小程序,這里將開發(fā)中遇到的坑和問(wèn)題記錄一下,所以下面這篇文章主要給大家介紹了關(guān)于uni-app開發(fā)微信小程序遇到的部分踩坑,需要的朋友可以參考下2023-02-02
將數(shù)字轉(zhuǎn)換成大寫的人民幣表達(dá)式的js函數(shù)
將數(shù)字轉(zhuǎn)換成大寫的人民幣,方法有很多,本例介紹的是使用js來(lái)完成的,有需要的朋友可以參考下2014-09-09
js從輸入框讀取內(nèi)容,比較兩個(gè)數(shù)字的大小方法
下面小編就為大家?guī)?lái)一篇js從輸入框讀取內(nèi)容,比較兩個(gè)數(shù)字的大小方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
使用JS實(shí)現(xiàn)一個(gè)功能豐富的待辦事項(xiàng)應(yīng)用
在日常工作和生活中,我們經(jīng)常需要處理各種各樣的待辦事項(xiàng),這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript實(shí)現(xiàn)一個(gè)功能豐富的待辦事項(xiàng)應(yīng)用,需要的可以了解下2024-01-01
代碼精簡(jiǎn)的可以實(shí)現(xiàn)元素圓角的js函數(shù)
代碼精簡(jiǎn)的可以實(shí)現(xiàn)元素圓角的js函數(shù)...2007-07-07
用js動(dòng)態(tài)添加html元素,以及屬性的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇用js動(dòng)態(tài)添加html元素,以及屬性的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07
three.js實(shí)現(xiàn)圍繞某物體旋轉(zhuǎn)
本篇文章主要介紹了three.js實(shí)現(xiàn)圍繞某物體旋轉(zhuǎn)的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01
SWFUpload多文件上傳及文件個(gè)數(shù)限制的方法
這篇文章主要介紹了SWFUpload多文件上傳及文件個(gè)數(shù)限制的方法,較為詳細(xì)的分析了SWFUpload組件實(shí)現(xiàn)多文件上傳的原理、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-05-05

