uniapp底部弹窗过渡效果显示与隐藏

在uniapp项目中,经常需要实现底部弹窗的功能,在弹窗的显示与隐藏中增加过渡效果页面功能效果会更好。

以下为是实现代码

为了代码复用,在components目录中创建popup组件

<template>
	<view>
		<view class="sheet-popup" :class="[show ? 'sheet-popup-show' : '']" :style="{'border-radius': `${borderRadius}rpx ${borderRadius}rpx 0rpx 0rpx`}">
			<slot></slot>
		</view>
		<view class="popup-mask" :class="[show ? 'popup-mask-show' : '']" @click="closePopup"></view>
	</view>
</template>

<script>
	export default {
		name:"popup",
		props:{
			// 显示状态
			show: {
				type: Boolean,
				default: false
			},
			// 显示弹窗的圆角
			borderRadius: {
				type: [Number, String],
				default: 0
			}
		},
		data() {
			return {
				
			};
		},
		methods: {
			closePopup() {
				this.$emit('close')
			}
		}
	}
</script>

<style>
	.sheet-popup{
		width: 100%;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 9998;
		visibility: hidden;
		transform: translate3d(0, 100%, 0);
		transform-origin: center;
		transition: all 0.2s ease-in-out;
		overflow: hidden;
		max-width: 500px;
		margin: 0 auto;
	}
	.sheet-popup-show {
		transform: translate3d(0, 0, 0);
		visibility: visible;
	}
	.popup-mask{
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 9996;
		background: rgba(0,0,0,0.5);
		max-width: 500px;
		margin: 0 auto;
		transition: all 0.2s ease-in-out;
		opacity: 0;
		visibility: hidden;
	}
	.popup-mask-show {
		opacity: 1;
		visibility: visible;
	}
</style>

在需要弹窗的界面中引用

<template>
    <view>
        <view @tap="showPopu = true">弹窗</view>
		<popup :show="showPopu" @close="showPopu=false" borderRadius='20'>
			<!-- 弹窗的内容样式 -->
			<view class="content">
				<view>1</view>
				<view>1</view>
				<view>1</view>
				<view>1</view>
			</view>
		</popup>
    </view>
</template>
 
<script>
    import popup from '@/components/popup/popup.vue'
    export default {
		components: {
			popup
		},
        data() {
			return {
				showPopu: false
			}
		},
        methods: {
			
		}
    }
</script>
<style>
	
</style>

您可以通过在 `template` 中编写底部弹窗的布局和样式,然后在需要使用底部弹窗的页面引入该 `template`。当用户触发弹窗显示的事件时,使用 `uni.showModal` 方法将弹窗显示出来。示例代码如下: 1. 在 `template` 中编写底部弹窗的布局和样式,如下所示: ```html <!-- bottom-popup 组件 --> <template name="bottom-popup"> <view class="popup-main"> <view class="popup-content"> <!-- 弹窗内容区域 --> </view> <view class="popup-close">关闭</view> </view> </template> <!-- 引入 bottom-popup 组件 --> <import src="../components/bottom-popup/bottom-popup"></import> ``` 2. 在需要使用底部弹窗的页面引入 `template`,并在页面中定义触发弹窗显示的事件,如下所示: ```html <template> <view> <view class="btn" @tap="showPopup">显示底部弹窗</view> </view> </template> <script> export default { methods: { showPopup() { uni.showModal({ title: '提示', content: '', showCancel: false, success: (res) => { // 将底部弹窗显示出来 this.$refs.bottomPopup.show() } }) } } } </script> <style> .btn { background-color: #409EFF; color: #fff; padding: 10px 20px; border-radius: 4px; text-align: center; margin-top: 50px; cursor: pointer; } </style> ``` 3. 在页面中使用引入的 `template`,并给底部弹窗组件添加 `ref` 属性方便在页面中调用该组件的方法,如下所示: ```html <template> <view> <!-- 使用 bottom-popup 组件 --> <bottom-popup ref="bottomPopup"></bottom-popup> </view> </template> <script> import bottomPopup from '../components/bottom-popup/bottom-popup' export default { components: { bottomPopup } } </script> ``` 4. 在 `bottom-popup` 组件中编写显示隐藏弹窗的方法,如下所示: ```html <template> <view class="popup-mask" @tap="hide"> <view class="popup-container" @tap.stop> <slot></slot> </view> </view> </template> <script> export default { methods: { show() { this.$nextTick(() => { this.$el.style.display = 'block' setTimeout(() => { this.$el.querySelector('.popup-container').classList.add('active') }, 100) }) }, hide() { this.$el.querySelector('.popup-container').classList.remove('active') setTimeout(() => { this.$el.style.display = 'none' }, 300) } } } </script> <style> .popup-mask { position: fixed; top: 0; bottom: 0; left: 0; right: 0; background-color: rgba(0, 0, 0, 0.4); z-index: 9999; display: none; } .popup-container { position: absolute; bottom: 0; left: 0; right: 0; background-color: #fff; border-top-left-radius: 10px; border-top-right-radius: 10px; transition: all 0.3s; transform: translateY(100%); padding: 20px; } .popup-container.active { transform: translateY(0); } </style> ``` 这样就可以在您的 uniapp 小程序中实现底部弹窗的选择功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值