uni-app做app自定义弹窗实现

本文介绍如何在uni-app中使用uView组件库实现一个带有透明背景的弹窗,并覆盖整个页面,包括创建弹窗页面、配置pages.json以及调用方法等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简单描述:

uni.showModel的阴影背景不能遮挡导航栏,弹窗效果不是很完美。要求弹出弹框时,将整个页面都覆盖在阴影层下。

个人用的ui组件uView,具体方案也是采用uView的压窗屏,其思路和方案可以参考官方文档

不过个人在此上踩了不少坑,主要是弹框的背景是白色,没有透明,在此做个记录。


1、创建一个弹窗页面,

<template>
	<u-modal v-model="show" :show-cancel-button="showCancelButton" confirm-text="升级" title="发现新版本" @cancel="cancel" @confirm="confirm">
		<view class="u-update-content">
			<rich-text :nodes="content"></rich-text>
		</view>
	</u-modal>
</template>

<script>
	export default {
		data() {
			return {
				show: false,
				content:'',
				showCancelButton:false
			}
		},
		onLoad(data) {
			this.content = data.content 
			this.showCancelButton = data.showCancelButton
		},
		onReady() {
			this.show = true;
		},
		methods: {
			cancel() {
				this.closeModal();
			},
			confirm() {
				//要执行的业务代码
				
				this.closeModal();
			},
			closeModal() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: transparent;
	}
	
	.u-full-content {
		background-color: #00C777;
	}
	
	.u-update-content {
		font-size: 26rpx;
		color: $u-content-color;
		line-height: 1.7;
		padding: 30rpx;
	}
</style>


2、pages.json添加注册刚刚新建的弹窗页面

{
            "path" : "mycomponents/screen/version",
            "style" :                                                                                    
            {
                "navigationStyle": "custom",
				"backgroundColor": "transparent",
				"app-plus": {
					"animationType": "fade-in",
					"background": "transparent",
					"popGesture": "none",
					"backgroundColor": "rgba(0,0,0,0)", // 背景透明
					"webviewBGTransparent":true,
					"mask":"none"
				}
            }
        }

3、在需要使用的地方,不管在vue页面,或是在js里面,通过如下跳转到弹窗页面,其本质是跳转页面,但看起来像弹窗,打开页面同时显示u-model

	uni.navigateTo({
		url:'/mycomponents/screen/version?content=有新的版本&showCancelButton=true'
		})

到这里基本上可以实现弹窗的效果,为了必坑,下面说下个人遇到的问题:

注意点:

1、试了几个手机,弹窗效果都是白底,不具备透明效果,可以在ap-plus添加透明属性"opacity":0.9,但这样整个页面都显得透明,不能我想要的效果。
2、在弹窗页面加上,
page {
background: transparent;
}
这样就能够实现之前说的透明效果了,但这里注意,一定要去掉 scoped,要不然还是不行的
3、至于点击确定如何传递参数信息,可以采用uni. e m i t ( " l i s t e r S o c k e t " , j s o n ) , 然 后 采 用 u n i . emit("listerSocket",json),然后采用uni. emit("listerSocket",json)uni.on(‘listerSocket’,(res)=>{})来处理结果。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值