uni-app 实现置顶悬浮框功能

试了下,,,mu模拟器、安卓手机、iOS手机,基本兼容,,滑动也不卡顿。

第三方插件地址


详述App端的2种方案

  1. 使用nvue页面

app-nvue页面渲染在原生引擎下,支持sticky,不存在浏览器兼容问题,可直接使用。

本示例就是基于nvue的示例,全端可实现粘性布局。

对一个列表头设置position: sticky,并且设置距离顶部多少开始吸顶,即同时设置top值,即可实现滚动到距离顶部多少时,固定位置不再滚动。

  1. 使用x5内核渲染vue页面
    app-vue是渲染在webview下的。默认使用系统webview渲染,在低端Android手机上,不支持position: sticky。

如果你的App要在Android 4.4、5.x上正常运行,则需要引入腾讯的x5浏览器内核来替代系统webview渲染vue页面


代码如下:


<template>
	<view class="full">
		<view class="full">
			<image src="/static/shuijiao.jpg" style="width: 750upx;height: 200px;"></image><!-- 注意这里图片的高度,必须是偶数。否则在H5端的部分chrome版本上会触发chrome的bug,在标题栏下方会留下一条线的空隙 -->
		</view>
		<view class="sticky-box">
			<view style="width: 250upx;text-align: center;"><text class="textcenter">条件1</text></view>
			<view style="width: 250upx;text-align: center;"><text class="textcenter">条件2</text></view>
			<view style="width: 250upx;text-align: center;"><text class="textcenter">条件3</text></view>
		</view>
		<view>
			<text style="line-height: 90px;" class="textcenter">
				1
				2
				3
				4
				5
				6
				7
				8
				9
				10
			</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			}
		},
		methods: {
			
		}
	}
</script>

<style>
.full{
	width: 750upx;
	margin: 0;
	padding: 0;
}

.sticky-box {
	/* #ifndef APP-PLUS-NVUE */
	display: flex;
	position: -webkit-sticky;
	/* #endif */
	position: sticky;
	top: var(--window-top);
	z-index: 99;
	flex-direction: row;
	margin: 0px;
	padding: 15px 0 15px 0;
	background-color: #F4F5F6;
	border-bottom-style: solid;
	border-bottom-color: #E2E2E2;
}

.textcenter{
	text-align: center;
	font-size: 18px;
}
</style>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,为您提供uni-app实现点击悬浮按钮展开悬浮菜单的步骤: 1. 在页面中添加一个悬浮按钮,如下: ```html <view class="float-btn" @tap="showMenu">+</view> ``` 2. 在data中添加一个变量,用来控制悬浮菜单的显示和隐藏,如下: ```javascript data() { return { isShowMenu: false, menuList: [ { text: '菜单1', icon: 'home', onClick() { // 处理菜单1的点击事件 } }, { text: '菜单2', icon: 'search', onClick() { // 处理菜单2的点击事件 } }, { text: '菜单3', icon: 'settings', onClick() { // 处理菜单3的点击事件 } } ] } } ``` 其中,menuList是悬浮菜单中的菜单列表,每个菜单都包括文本、图标和点击事件。 3. 在methods中添加一个方法,用来控制悬浮菜单的显示和隐藏,如下: ```javascript methods: { showMenu() { this.isShowMenu = !this.isShowMenu; }, handleMenuClick(index) { const menu = this.menuList[index]; if (menu.onClick) { menu.onClick(); } this.isShowMenu = false; } } ``` 其中,showMenu方法用于控制isShowMenu的值,从而显示或隐藏悬浮菜单;handleMenuClick方法用于处理悬浮菜单中的菜单被点击的事件。 4. 在template中添加悬浮菜单的代码,如下: ```html <view class="float-menu" v-show="isShowMenu"> <view class="menu-item" v-for="(menu, index) in menuList" :key="index" @tap="handleMenuClick(index)"> <view class="menu-icon"> <uni-icons :type="menu.icon"></uni-icons> </view> <view class="menu-text">{{ menu.text }}</view> </view> </view> ``` 其中,float-menu是悬浮菜单的容器,menu-item是悬浮菜单中的每个菜单项,menu-icon是菜单项的图标,menu-text是菜单项的文本。 5. 在CSS中设置悬浮按钮和悬浮菜单的样式,如下: ```css .float-btn { position: fixed; right: 20px; bottom: 20px; width: 60px; height: 60px; line-height: 60px; text-align: center; font-size: 30px; border-radius: 50%; background-color: #007AFF; color: #fff; z-index: 9999; } .float-menu { position: fixed; right: 20px; bottom: 90px; width: 150px; height: 150px; border-radius: 10px; background-color: #fff; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3); z-index: 9998; display: flex; flex-wrap: wrap; justify-content: space-between; align-content: space-between; padding: 10px; opacity: 0.95; transition: all 0.3s; } .menu-item { width: 40%; height: 40%; display: flex; flex-direction: column; justify-content: center; align-items: center; font-size: 12px; color: #333; } .menu-icon { font-size: 20px; color: #007AFF; } .menu-text { margin-top: 5px; } ``` 这样就可以实现uni-app点击悬浮按钮展开悬浮菜单的效果了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值