feat: 兼容在app上页面滚动与键盘问题
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<template>
|
||||
<view class="flex flex-col h-screen relative">
|
||||
<view class="flex flex-col h-screen relative overflow-hidden">
|
||||
<!-- 顶部自定义导航栏 -->
|
||||
<view class="header absolute top-0 left-0 w-full z-10" :style="{ paddingTop: statusBarHeight + 'px' }">
|
||||
<ChatTopNavBar
|
||||
@@ -21,19 +21,31 @@
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view v-show="tabIndex === 0" class="flex-full overflow-hidden scroll-y">
|
||||
<Discovery />
|
||||
<view
|
||||
v-show="tabIndex === 0"
|
||||
class="main-scroll-area flex-full overflow-hidden min-height-0"
|
||||
@touchstart.capture="handleScrollAreaTouchStart"
|
||||
@touchstart="handleScrollAreaTouchStart"
|
||||
@touchmove="handleScrollAreaTouchMove"
|
||||
>
|
||||
<Discovery
|
||||
@scroll-touch-start="handleScrollAreaTouchStart"
|
||||
@scroll-touch="handleScrollAreaTouchMove"
|
||||
/>
|
||||
</view>
|
||||
|
||||
<!-- 消息列表(可滚动区域) -->
|
||||
<scroll-view
|
||||
v-show="tabIndex === 1"
|
||||
class="main flex-full overflow-hidden scroll-y"
|
||||
class="main main-scroll-area flex-full overflow-hidden min-height-0"
|
||||
scroll-y
|
||||
:scroll-top="scrollTop"
|
||||
:scroll-with-animation="true"
|
||||
@scroll="handleScroll"
|
||||
@scrolltolower="handleScrollToLower"
|
||||
@touchstart.capture="handleScrollAreaTouchStart"
|
||||
@touchstart="handleScrollAreaTouchStart"
|
||||
@touchmove="handleScrollAreaTouchMove"
|
||||
>
|
||||
|
||||
<view
|
||||
@@ -195,7 +207,7 @@
|
||||
|
||||
<script setup>
|
||||
import { onMounted, nextTick, onUnmounted, ref } from "vue";
|
||||
import { onLoad } from "@dcloudio/uni-app";
|
||||
import { onLoad, onReady } from "@dcloudio/uni-app";
|
||||
import {
|
||||
SWITCH_TO_COMPANION_TAB,
|
||||
SWITCH_TO_DISCOVERY_TAB,
|
||||
@@ -271,6 +283,8 @@ const inputMessage = ref("");
|
||||
|
||||
/// 是否自动滚动到底部 (人工手动向上滚动时设为false)
|
||||
const isAutoScroll = ref(true);
|
||||
let lastHideKeyboardAt = 0;
|
||||
let lastScrollTouchAt = 0;
|
||||
|
||||
/// agentId 首页接口中获取
|
||||
const agentId = ref("1");
|
||||
@@ -348,10 +362,46 @@ const handleKeyboardHide = () => {
|
||||
holdKeyboard.value = false;
|
||||
};
|
||||
|
||||
const hideKeyboardByScroll = () => {
|
||||
const now = Date.now();
|
||||
if (now - lastHideKeyboardAt < 300) return;
|
||||
lastHideKeyboardAt = now;
|
||||
|
||||
holdKeyboard.value = false;
|
||||
holdKeyboardFlag.value = true;
|
||||
isKeyboardShow.value = false;
|
||||
|
||||
if (inputAreaRef.value && inputAreaRef.value.blurInput) {
|
||||
inputAreaRef.value.blurInput();
|
||||
}
|
||||
|
||||
uni.hideKeyboard();
|
||||
|
||||
// #ifdef APP-PLUS
|
||||
if (typeof plus !== "undefined" && plus.key && plus.key.hideSoftKeybord) {
|
||||
plus.key.hideSoftKeybord();
|
||||
}
|
||||
// #endif
|
||||
};
|
||||
|
||||
const handleScrollAreaTouchStart = () => {
|
||||
lastScrollTouchAt = Date.now();
|
||||
hideKeyboardByScroll();
|
||||
};
|
||||
|
||||
const handleScrollAreaTouchMove = () => {
|
||||
lastScrollTouchAt = Date.now();
|
||||
hideKeyboardByScroll();
|
||||
};
|
||||
|
||||
// 处理用户滚动事件
|
||||
const welcomeHeight = ref(0);
|
||||
let lastScrollTop = 0;
|
||||
const handleScroll = (e) => {
|
||||
if (Date.now() - lastScrollTouchAt < 1000) {
|
||||
hideKeyboardByScroll();
|
||||
}
|
||||
|
||||
const detail = e.detail;
|
||||
topNavBarRef.value.show = parseInt(detail.scrollTop) > welcomeHeight.value;
|
||||
|
||||
@@ -484,6 +534,21 @@ onLoad(() => {
|
||||
});
|
||||
});
|
||||
|
||||
onReady(() => {
|
||||
// #ifdef APP-PLUS
|
||||
const pages = getCurrentPages();
|
||||
const currentPage = pages[pages.length - 1];
|
||||
const currentWebview = currentPage && currentPage.$getAppWebview && currentPage.$getAppWebview();
|
||||
|
||||
if (currentWebview && currentWebview.setStyle) {
|
||||
currentWebview.setStyle({
|
||||
bounce: "none",
|
||||
scrollIndicator: "none",
|
||||
});
|
||||
}
|
||||
// #endif
|
||||
});
|
||||
|
||||
// token存在,初始化数据
|
||||
const initHandler = () => {
|
||||
console.log("initHandler");
|
||||
@@ -1229,4 +1294,11 @@ const resetConfig = () => {
|
||||
};
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped></style>
|
||||
<style lang="scss" scoped>
|
||||
.main-scroll-area {
|
||||
flex-basis: 0;
|
||||
height: 0;
|
||||
min-height: 0;
|
||||
overscroll-behavior-y: contain;
|
||||
}
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user