vue Vant的Popup弹出层滚动问题,底部元素fixed定位异常问题

最近开发移动端h5,用的vant 移动端组件库框架,使用popup弹出层时,需要在底部固定一个操作模块。

但我发现在popup组件里使用fixed定位后,如果内容没有超出popup高度,底部fixed内容显示正常,否则内容超出出现滚动条后,底部fixed定位元素随页面滚动,fixed失效。效果如下:

正常情况和异常情况对比图(左正右异):

 解决办法有两种方式
方式一:

<div class="box-man">
		<van-popup
			class="menu-pop"
			v-model="showPopup"
			closeable
			close-on-popstate
			position="right"
			@close="close"
			@open="open"
			:lazy-render="false"
            //overflow: 'hidden' 为了隐藏内容超出
			:style="{ height: '100%', width: '100%', padding: '10% 6% 11%', overflow: 
            'hidden' }">
 
            //渲染的数据
			<div class="item" ref="itemBox">
				<div v-for="(item, index) in topicList" :key="index">
					<p v-text="item.title"></p>
				</div>
			</div>
 
            //底部fixed元素
			<div class="ft-btn" id="ft-btn">
				<span v-text="`clear`"></span>
				<van-button round block color="#391bf1" text="apply"></van-button>
			</div>
		</van-popup>
</div>
 
 
 
//所需样式
<style lang="stylus">
.box-man
    //为了隐藏滚动条
	.item
		width: 110%
		overflow: hidden
		overflow-y: auto
 
	.ft-btn
        width: 100%
        position: fixed;
		left: 0
		bottom: 0
		border-top: 1px solid #e7e7e7
        background-color: #fff;
</style>

//js代码
 
<script>
export default {
	name: 'PopularTopicsPopup',
	components: {},
	props: {
		showMenuPopup: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
            //控制popup的显示与隐藏
			showPopup: this.showMenuPopup,
            //模拟的数据
			topicList: Array(30).fill({
				title: 'Payments'
			})
		}
	},
	computed: {},
	watch: {
		showMenuPopup: {
			handler(newval, oldval) {
				this.showPopup = newval
			},
			immediate: true,
			deep: true
		}
	},
	created() {},
	mounted() {},
	// 销毁监听
	destroyed() {},
	methods: {
		//关闭弹出层时触发
		close() {
			this.showPopup = false
			this.$emit('showMenuPopups', this.showPopup)
		},
		//打开弹出层时触发
		open() {
			this.$nextTick(_ => {
				this.getHight()
			})
		},
		// 设置渲染数据内容区域的高度
		getHight() {
            //获取底部定位元素的高度和渲染数据盒子距离页面顶部距离
			let h = document.getElementById('ft-btn').clientHeight,
				itemOffTop = this.$refs.itemBox.offsetTop
 
            //设置渲染数据盒子的高度 限制它只能在指定高度内滚动内容
			this.$refs.itemBox.style.height =
				document.documentElement.clientHeight - h - itemOffTop + 'px'
			this.$forceUpdate()
		}
	}
}
</script>

效果图:

 滑动到底部 能完全显示最后一项内容,并且底部fixed元素始终定位在底部。

方式二

//html代码
 
<div class="box-man">
		<van-popup
			class="menu-pop"
			v-model="showPopup"
			closeable
			close-on-popstate
			position="right"
			@close="close"
			@open="open"
			:lazy-render="false"
            //overflow: 'hidden' 移除内容超出隐藏样式,把padding最后一个值改为0
			:style="{ height: '100%', width: '100%', padding: '10% 6% 0' }">
 
            //渲染的数据
			<div class="item" ref="itemBox">
				<div v-for="(item, index) in topicList" :key="index">
					<p v-text="item.title"></p>
				</div>
			</div>
		</van-popup>
 
        //底部fixed元素
		<div class="ft-btn" id="ft-btn" v-if="showPopup">
			<span v-text="`clear`"></span>
			<van-button round block color="#391bf1" text="apply"></van-button>
		</div>
</div>
 
 
 
//所需样式
<style lang="stylus">
.box-man
    //去除item样式
	.item
/* 		width: 110%
		overflow: hidden
		overflow-y: auto */
 
	.ft-btn
        width: 100%
        position: fixed;
		left: 0
		bottom: 0
		border-top: 1px solid #e7e7e7
        background-color: #fff;
</style>
//js代码
 
<script>
export default {
	name: 'PopularTopicsPopup',
	components: {},
	props: {
		showMenuPopup: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
            //控制popup的显示与隐藏
			showPopup: this.showMenuPopup,
            //模拟的数据
			topicList: Array(30).fill({
				title: 'Payments'
			})
		}
	},
	computed: {},
	watch: {
		showMenuPopup: {
			handler(newval, oldval) {
				this.showPopup = newval
			},
			immediate: true,
			deep: true
		}
	},
	created() {},
	mounted() {},
	// 销毁监听
	destroyed() {},
	methods: {
		//关闭弹出层时触发
		close() {
			this.showPopup = false
			this.$emit('showMenuPopups', this.showPopup)
		},
		//打开弹出层时触发
		open() {
			this.$nextTick(_ => {
				this.getHight()
			})
		},
		// 设置渲染数据内容区域的padding-bottom
		getHight() {
            //获取底部定位元素的高度
			let h = document.getElementById('ft-btn').clientHeight
 
            //设置渲染数据盒子的内下边距 为了解决元素被fixed定位元素遮挡问题
			this.$refs.itemBox.style.paddingBottom = h + 'px'
			this.$forceUpdate()
		}
	}
}
</script>

 把需要定位的元素放在popup组件外,显示隐藏直接和控制弹出层的变量绑定同一个值,然后动态获取底部定位元素高度,赋值给渲染数据盒子的padding-bottom就可以了!

  效果图:


 滑动到底部 能完全显示最后一项内容,并且底部fixed元素始终定位在底部。 

 到此结束。

原文链接:https://blog.csdn.net/weixin_43743175/article/details/125328500


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 中创建 Popup 弹出的一种常见方法是使用组件和动态绑定。以下是一个简单的示例: 首先,创建一个名为 `Popup` 的 Vue 组件,该组件将用于显示弹出内容。可以在组件中使用 `v-if` 或 `v-show` 来控制弹出的显示与隐藏。 ```vue <template> <div class="popup" v-show="showPopup"> <!-- 弹出内容 --> <div class="popup-content"> <!-- 内容 --> </div> </div> </template> <script> export default { data() { return { showPopup: false // 控制弹出显示与隐藏的标志位 }; } }; </script> <style scoped> .popup { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); // 半透明遮罩 } .popup-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: #fff; } </style> ``` 然后,在需要触发弹出的地方,使用一个按钮或其他交互元素并绑定一个点击事件。在事件处理程序中,通过修改 `showPopup` 数据属性来控制弹出的显示与隐藏。 ```vue <template> <div> <button @click="showPopup = !showPopup">显示弹出</button> <Popup :showPopup="showPopup" /> <!-- 在需要显示弹出的地方添加组件 --> </div> </template> <script> import Popup from './Popup.vue'; export default { components: { Popup }, data() { return { showPopup: false // 控制弹出显示与隐藏的标志位 }; } }; </script> ``` 这样,当按钮被点击时,`showPopup` 的值将被切换,从而控制弹出的显示与隐藏。可以根据需要对弹出的样式和行为进行进一步的自定义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值