uniapp開發(fā)小程序?qū)崿F(xiàn)全局懸浮按鈕的代碼
看效果
這是一個全局的按鈕,可以換成圖片,自己寫樣式,每個頁面都有;
須知:
1.uni.getSystemInfoSync()獲取手機的信息接口
可以拿到手機屏幕的寬高
2.uni.createSelectorQuery().in(this)
uniapp中式?jīng)]有window對象,和dom元素的,但是有時我們需要獲取頁面上節(jié)點的一些幾何信息;
@touchcancel 手指觸摸被打斷,如來電提醒,彈窗 @touchend 手指觸摸動作結(jié)束,如松開按鈕 @touchmove 手指觸摸后移動 @touchstart 手指觸摸動作開始
3.touchmove滑動事件
@touchcancel 手指觸摸被打斷,如來電提醒,彈窗 @touchend 手指觸摸動作結(jié)束,如松開按鈕 @touchmove 手指觸摸后移動 @touchstart 手指觸摸動作開始
記錄用戶按下屏幕的坐標(biāo) 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 },
到此這篇關(guān)于uniapp開發(fā)小程序如何實現(xiàn)全局懸浮按鈕的文章就介紹到這了,更多相關(guān)uniapp小程序懸浮按鈕內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
uni-app開發(fā)微信小程序遇到的部分踩坑實戰(zhàn)
最近在用uni-app開發(fā)微信小程序,這里將開發(fā)中遇到的坑和問題記錄一下,所以下面這篇文章主要給大家介紹了關(guān)于uni-app開發(fā)微信小程序遇到的部分踩坑,需要的朋友可以參考下2023-02-02將數(shù)字轉(zhuǎn)換成大寫的人民幣表達式的js函數(shù)
將數(shù)字轉(zhuǎn)換成大寫的人民幣,方法有很多,本例介紹的是使用js來完成的,有需要的朋友可以參考下2014-09-09js從輸入框讀取內(nèi)容,比較兩個數(shù)字的大小方法
下面小編就為大家?guī)硪黄猨s從輸入框讀取內(nèi)容,比較兩個數(shù)字的大小方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03使用JS實現(xiàn)一個功能豐富的待辦事項應(yīng)用
在日常工作和生活中,我們經(jīng)常需要處理各種各樣的待辦事項,這篇文章主要為大家詳細介紹了如何使用JavaScript實現(xiàn)一個功能豐富的待辦事項應(yīng)用,需要的可以了解下2024-01-01three.js實現(xiàn)圍繞某物體旋轉(zhuǎn)
本篇文章主要介紹了three.js實現(xiàn)圍繞某物體旋轉(zhuǎn)的示例代碼。具有很好的參考價值,下面跟著小編一起來看下吧2017-01-01