uniapp实战 —— 可滚动区域 scroll-view (自适配高度,下拉刷新)

自适配高度

自定义的顶部导航栏,可参考博文
https://blog.csdn.net/weixin_41192489/article/details/134852124

如图可见,在页面滚动过程中,顶部导航栏和底栏未动,仅中间的内容区域可滚动。

  • 整个页面的高度设置为 100%,并采用 flex 布局
  • 滚动区域的高度,通过flex布局的flex-grow实现自适配
html 复制代码
<template>
  <!-- 顶部--自定义的导航栏 -->
  <CustomNavbar />
  <!-- 中间--自适配高度的滚动区 -->
  <scroll-view scroll-y class="contentBox">
  <!-- 此处省略了页面内容的相关代码 -->
  </scroll-view>
</template>
html 复制代码
<style lang="scss">
page {
  background-color: #f7f7f7;
  // 总容器高度撑满屏幕
  height: 100%;
  // 使容器内元素使用flex布局
  display: flex;
  flex-direction: column;
}
.contentBox {
  // 滚动区自适配高度
  flex-grow: 1;
}
</style>
  • 注意事项:此处样式不能加 scoped

下拉刷新

在 scroll-view 标签上新增

js 复制代码
    refresher-enabled
    @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered"

js中新增

js 复制代码
// 控制下拉刷新动画的显隐
const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开启下拉刷新动画
  isTriggered.value = true
  // 重置子组件(猜你喜欢)分页相关数据(页码重置为1,清空列表,结束标记重置为false)
  guessRef.value?.resetData()
  // 加载数据--所有接口同时开始刷新,直到耗时最长的接口返回数据
  await Promise.all([getSwiperInfo(), getCategoryInfo(), guessRef.value?.getGuessList()])
  // 关闭下拉刷新动画
  isTriggered.value = false
}
相关推荐
用户059540174462 天前
AI Agent记忆测试踩坑实录:Mock骗了我一周,Mem0+pytest一招破局
前端·css
Darling噜啦啦3 天前
CSS 3D 变换与 Flex 布局实战:从零打造旋转立方体
前端·css
用户059540174463 天前
把待办应用从Electron换成Tauri,内存占用狂降90%,打包体积仅5MB
前端·css
用户6990304848753 天前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
ITKEY_3 天前
uniapp微信开发者工具 更改AppID失败 touristappid
uni-app
小月土星3 天前
CSS 3D 从入门到炫技:手把手教你写一个旋转立方体
前端·css
xingpanvip3 天前
星盘接口开发文档:本命盘接口指南
android·开发语言·css·php·lua
HjhIron3 天前
CSS 3D 世界:从盒子模型到三维空间动画
javascript·css
参宿73 天前
CSS 悬挂空白与选区溢出
前端·css
黄敬峰4 天前
纯 CSS3 打造 3D 旋转魔方:从文档流、Flex 布局到空间变换的硬核复盘
css