最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2022年03月22日 11:15:17   作者:小莉愛編程  
這篇文章主要介紹了uniapp開發(fā)小程序如何實現(xiàn)全局懸浮按鈕,但是在uniapp中式?jīng)]有window對象,和dom元素的,需要獲取頁面上節(jié)點的幾何信息,具體實例代碼詳細(xì)跟隨小編一起看看吧

看效果

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

須知:

1.uni.getSystemInfoSync()獲取手機(jī)的信息接口

可以拿到手機(jī)屏幕的寬高

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() {
			//獲取手機(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>

全局注冊組件

因為我這個項目是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() {
			//獲取手機(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('移動',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)文章

  • javascript 小數(shù)取整簡單實現(xiàn)方式

    javascript 小數(shù)取整簡單實現(xiàn)方式

    這篇文章主要介紹了javascript 小數(shù)取整d的簡單實現(xiàn)方式,需要的朋友可以參考下
    2014-05-05
  • 微信小程序?qū)崿F(xiàn)文字跑馬燈效果

    微信小程序?qū)崿F(xiàn)文字跑馬燈效果

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)文字跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • JS+DIV實現(xiàn)鼠標(biāo)劃過切換層效果的實例代碼

    JS+DIV實現(xiàn)鼠標(biāo)劃過切換層效果的實例代碼

    這篇文章主要是對JS+DIV實現(xiàn)鼠標(biāo)劃過切換層效果的實例代碼進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-11-11
  • 圖解JavaScript作用域鏈底層原理

    圖解JavaScript作用域鏈底層原理

    當(dāng)代碼在一個環(huán)境中執(zhí)行時,會創(chuàng)建變量對象的一個作用域鏈,作用域鏈的用途是保證對執(zhí)行環(huán)境有權(quán)訪問的所有變量和函數(shù)的有序訪問,下面這篇文章主要給大家介紹了關(guān)于JavaScript作用域鏈底層原理的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • 傾力總結(jié)40條常見的移動端Web頁面問題解決方案

    傾力總結(jié)40條常見的移動端Web頁面問題解決方案

    移動端Web需要照顧觸摸操作的體驗,以及更多的屏幕旋轉(zhuǎn)與尺寸適配等問題,非常瑣碎,在這里為大家傾力總結(jié)40條常見的移動端Web頁面問題解決方案,歡迎收看收藏!
    2016-05-05
  • 一個不用onmouseup的拖動函數(shù)

    一個不用onmouseup的拖動函數(shù)

    一個不用onmouseup的拖動函數(shù)...
    2007-05-05
  • js類中的公有變量和私有變量

    js類中的公有變量和私有變量

    實例分析js類中的公有和私有變量
    2008-07-07
  • javascript 處理HTML元素必須避免使用的一種方法

    javascript 處理HTML元素必須避免使用的一種方法

    我們在編寫前臺頁面的時候,可能經(jīng)常會用到“javascript+數(shù)據(jù)”生成頁面元素的方法,但當(dāng)我們要處理的數(shù)據(jù)量較大,導(dǎo)致頁面需要展現(xiàn)過多的控件的時候,頁面的響應(yīng)速度也會直線下降
    2009-07-07
  • 詳解JVM系列之內(nèi)存模型

    詳解JVM系列之內(nèi)存模型

    JVM是一種用于計算設(shè)備的規(guī)范,它是一個虛構(gòu)出來的機(jī)器,是通過在實際的計算機(jī)上仿真模擬各種功能實現(xiàn)的。JVM的內(nèi)存區(qū)域可以被分為:線程、棧、堆、靜態(tài)方法區(qū)。本文將介紹JVM的內(nèi)存模型,感興趣的小伙伴,可以參考下
    2021-06-06
  • javascript運(yùn)行機(jī)制之執(zhí)行順序理解

    javascript運(yùn)行機(jī)制之執(zhí)行順序理解

    這篇文章主要介紹了javascript運(yùn)行機(jī)制之執(zhí)行順序理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08

最新評論

兴仁县| 九龙坡区| 临安市| 广安市| 新民市| 南汇区| 拉萨市| 静海县| 汤原县| 黄大仙区| 从江县| 南充市| 思南县| 依安县| 安徽省| 康乐县| 崇文区| 会昌县| 简阳市| 孝感市| 法库县| 平陆县| 永昌县| 武宣县| 曲松县| 福安市| 周宁县| 台东市| 宁津县| 古交市| 吐鲁番市| 丹东市| 资源县| 新泰市| 修水县| 加查县| 长治县| 廉江市| 宁乡县| 阿克苏市| 彭泽县|