uniapp uni-popup弹出层组件 弹窗 无法弹出 自定义样式问题

本文介绍了uni-app的uni-popup弹出层组件的使用,重点解决弹窗无法弹出及自定义样式的问题。通过示例代码,讲解了如何正确导入组件并设置宽高,以达到预期效果。

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

uni-popup弹出层组件

之前用到弹出层组件时发现弹出时会有一层灰色的遮罩,后来调试了几次发现是因为没有自定义给弹出层宽高,尝试了几次终于做到自己想要的效果,话不多说,直接附上完整代码:
效果图:
在这里插入图片描述

<template>
	<view>
		<view class="sign_text" @tap="toggle('center')">
			<text style="line-height: 60px;margin-top: 12px;">点击弹出</text>
		</view>
		<!-- 普通弹窗 -->
		<uni-popup ref="popup" background-color="#fff">
			<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }">
				<view style="color: green;">
					这是一个弹窗
				</view>
				<view style="color: red;" @click="close()">
					关闭弹窗
				</view>
			</view>
		</uni-popup>
	</view>
</template>

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

			}
		},
		methods: {
			//弹窗开启
			toggle(type) {
				this.type = type
				this.$refs.popup.open(type)
			},
			//弹窗关闭
			close() {
				this.$refs.popup.close()
			},

		}
	}
</script>

<style>
	.popup-content {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		padding: 15px;
		height: 200px;
		width: 280px;
		background-color: #fff;
		border-radius: 10px;
	}

</style>

下面教一下不会灵活使用的同学这个组件应该怎么用

一、uni-popup弹出层

官网组件地址:https://ext.dcloud.net.cn/plugin?id=329

可以先翻看一下官网的介绍,很有用。

二、使用步骤

.首先要去官网把这个组件导入到项目中或者下载到本地,常规放在components文件目录下:
在这里插入图片描述
最好是这三个都要一起下载,这样用的时候比较方便,使用的时候按需引入,自定义组件样式就好了

有疑问欢迎留言~

### UniAppuni-popup 组件实现底部弹窗的案例与使用方法 在 UniApp 中,`uni-popup` 是一个常用的组件,用于实现各种类型的弹窗效果。对于底部弹窗的实现,可以通过设置 `type="bottom"` 来指定弹窗从屏幕底部弹出。以下是基于引用内容和相关知识整理的完整示例代码和说明。 #### 示例代码 以下是一个完整的底部弹窗示例代码,展示了如何结合 `uni-popup` 实现底部弹窗,并隐藏/显示底部导航栏的功能。 ```html <template> <view> <!-- 触发按钮 --> <button type="primary" @click="openPopup">打开底部弹窗</button> <!-- 底部弹窗 --> <uni-popup ref="popup" type="bottom" @change="onPopupChange"> <view class="popup-content"> <text>这是一个底部弹窗</text> <button type="warn" @click="closePopup">关闭弹窗</button> </view> </uni-popup> </view> </template> <script> export default { data() { return {}; }, methods: { // 打开弹窗 openPopup() { this.$refs.popup.open(); // 打开弹窗 uni.hideTabBar(); // 隐藏底部导航栏[^1] }, // 关闭弹窗 closePopup() { this.$refs.popup.close(); // 关闭弹窗 uni.showTabBar(); // 显示底部导航栏[^1] }, // 弹窗状态变化事件 onPopupChange(e) { if (!e.show) { uni.showTabBar(); // 如果弹窗关闭,则显示底部导航栏[^1] } } } }; </script> <style> .popup-content { padding: 20px; background-color: #fff; text-align: center; } </style> ``` #### 代码解析 1. **触发弹窗**:通过点击按钮调用 `openPopup` 方法,使用 `this.$refs.popup.open()` 打开弹窗,并调用 `uni.hideTabBar()` 隐藏底部导航栏。 2. **关闭弹窗**:通过点击关闭按钮调用 `closePopup` 方法,使用 `this.$refs.popup.close()` 关闭弹窗,并调用 `uni.showTabBar()` 显示底部导航栏[^1]。 3. **弹窗状态监听**:通过 `@change` 监听弹窗的状态变化,当弹窗关闭时自动显示底部导航栏[^1]。 #### 注意事项 - 在某些场景下,如果需要阻止页面滚动,可以参考引用[2]中的方法,通过 CSSJavaScript 禁止遮盖下的页面滑动[^2]。 - 如果弹窗中包含输入且需要自定义键盘上推距离,可以参考引用[3]中的代码,通过设置 `adjustPosition=false` 和自定义样式来控制键盘行为[^3]。 - 对于底部弹窗存在空隙的问题,可以参考引用[4]中的解决方案,通过调整 `paddingBottom` 和其他样式属性解决兼容性问题[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值