modal内容不会产生滚动情况: 给modal最外层元素添加:catchtouchmove="{{true}}" 禁止滚动
modal内容会滚动情况:
<scroll-view
scroll-y
bindscrolltoupper="bindscrolltoupper" // 滚动到顶部/左边事件
bindscrolltolower="bindscrolltolower" // 滚动到底部/右边事件
bindscroll="bindscroll" // 滚动事件
>
<scroll-view>
data: {
isNoScroll: false
},
bindscrolltoupper(e) {
this.setData({
isNoScroll: true
})
},
bindscrolltolower(e) {
this.setData({
isNoScroll: true
})
},
bindscroll(e) {
if(this.data.isScroll) {
this.setData({
isNoScroll: false
})
}
}
给mask遮罩层添加属性:catchtouchmove="{{true}}"
给modal中最外层scroll-view层添加:catchtouchmove="{{ isNoScroll }}", 默认为false, 允许页面滚动,当modal滚动到顶部/底部的时候,设置变量isNoScroll为true,禁止滚动,即可实现modal内容滚动,底部body内容不会跟随body滚动
文章讲述了如何在Modal中实现内容滚动并阻止背景页面滚动。关键在于使用`catchtouchmove`属性,当Modal内容滚动到边界时,设置数据属性`isNoScroll`为true,禁止页面滚动。通过在mask遮罩层和Modal的scroll-view层应用`catchtouchmove`并结合事件监听,可以实现Modal内部滚动而外部页面保持静止的效果。
1729





