uniapp页面设置手机不滑动切换页面
时间: 2023-08-26 21:05:06 浏览: 95
要禁止页面滑动切换,可以在 `manifest.json` 中设置 `"window": { "disableScroll": true }`,这将在应用程序级别禁止页面滚动。但是,如果您只想在特定页面上禁用滚动,则需要在该页面的 `onLoad` 生命周期函数中设置 `disableScroll` 选项。例如:
```javascript
export default {
onLoad() {
uni.getSystemInfo({
success: (res) => {
// 禁止页面滚动
if (res.platform === 'android') {
uni.createSelectorQuery().select('.page-container').boundingClientRect((rect) => {
uni.createSelectorQuery().selectViewport().scrollOffset((res) => {
this.pageTop = res.scrollTop;
this.pageHeight = rect.height;
uni.createSelectorQuery().select('.page-container').fields({
size: true
}, (res) => {
this.pageWidth = res.width;
}).exec();
}).exec();
}).exec();
uni.createSelectorQuery().select('.page-container').boundingClientRect((rect) => {
uni.createSelectorQuery().selectViewport().scrollOffset((res) => {
this.pageTop = res.scrollTop;
this.pageHeight = rect.height;
uni.createSelectorQuery().select('.page-container').fields({
size: true
}, (res) => {
this.pageWidth = res.width;
}).exec();
}).exec();
}).exec();
uni.createSelectorQuery().select('.page-container').boundingClientRect((rect) => {
uni.createSelectorQuery().selectViewport().scrollOffset((res) => {
this.pageTop = res.scrollTop;
this.pageHeight = rect.height;
uni.createSelectorQuery().select('.page-container').fields({
size: true
}, (res) => {
this.pageWidth = res.width;
}).exec();
}).exec();
}).exec();
uni.createSelectorQuery().select('.page-container').boundingClientRect((rect) => {
uni.createSelectorQuery().selectViewport().scrollOffset((res) => {
this.pageTop = res.scrollTop;
this.pageHeight = rect.height;
uni.createSelectorQuery().select('.page-container').fields({
size: true
}, (res) => {
this.pageWidth = res.width;
}).exec();
}).exec();
}).exec();
}
}
});
},
onUnload() {
// 允许页面滚动
if (uni.getSystemInfoSync().platform === 'android') {
uni.createSelectorQuery().select('.page-container').boundingClientRect((rect) => {
uni.pageScrollTo({
scrollTop: this.pageTop,
duration: 0
});
uni.createSelectorQuery().select('.page-container').fields({
size: true
}, (res) => {
uni.pageScrollTo({
selector: '.page-container',
scrollTop: this.pageTop,
duration: 0
});
}).exec();
}).exec();
}
}
};
```
在这个例子中,我们在 `onLoad` 生命周期函数中使用 `uni.createSelectorQuery()` 获取页面容器的高度和宽度,并在 `disableScroll` 选项中设置它们。在 `onUnload` 生命周期函数中,我们允许页面滚动并将页面滚动到初始位置。
阅读全文