首頁 > 軟體

uniapp開發小程式實現全域性懸浮按鈕的程式碼

2022-03-22 13:00:12

看效果

這是一個全域性的按鈕,可以換成圖片,自己寫樣式,每個頁面都有;

須知:

1.uni.getSystemInfoSync()獲取手機的資訊介面

可以拿到手機螢幕的寬高

2.uni.createSelectorQuery().in(this)

uniapp中式沒有window物件,和dom元素的,但是有時我們需要獲取頁面上節點的一些幾何資訊;

@touchcancel 手指觸控被打斷,如來電提醒,彈窗
@touchend 手指觸控動作結束,如鬆開按鈕
@touchmove 手指觸控後移動
@touchstart 手指觸控動作開始

3.touchmove滑動事件

@touchcancel 手指觸控被打斷,如來電提醒,彈窗
@touchend 手指觸控動作結束,如鬆開按鈕
@touchmove 手指觸控後移動
@touchstart 手指觸控動作開始

記錄使用者按下螢幕的座標 x 和 y

touchmove(e) {
				// 單指觸控
				if (e.touches.length !== 1) {
					return false;
				}
				console.log('移動',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存入本地儲存
				 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存入本地儲存
				 uni.setStorageSync("left", this.left);
				this.isMove = false;
				this.$emit('btnTouchend');
		}

每次移動這個按鈕,本地儲存的值都會改變;

取出儲存的值

onShow() {
			//獲取手機資訊設定介面
			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>

全域性註冊元件

因為我這個專案是vue3,所以註冊元件的時候,不需要全域性引入,

這個元件,需要在每個頁面引入;
元件程式碼:需要換個圖片就可以用了;

<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() {
			//獲取手機資訊設定介面
			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('移動',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>

頁面引入:

<drag-button :isDock="true" :existTabBar="true" @btnClick="btnClick" @btnTouchstart="btnTouchstart"
			@btnTouchend="btnTouchend">

引入元件

components: {
			dragButton
		},

到此這篇關於uniapp開發小程式如何實現全域性懸浮按鈕的文章就介紹到這了,更多相關uniapp小程式懸浮按鈕內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com