当前位置: 首页 > news >正文

uniapp 顶部tab + 占满剩余高度的内容区域swiper

在这里插入图片描述
在这里插入图片描述


<template><view class="page-container"><!-- 顶部 Tab 栏 --><view class="tabs-container"><scroll-view scroll-x class="tabs-scroll" :show-scrollbar="false"><view class="tabs"><view v-for="(tab, index) in tabs" :key="index" class="tab-item" :class="{ active: currentTab === index }"@click="switchTab(index)">{{ tab }}<view class="underline" v-if="currentTab === index"></view></view></view></scroll-view></view><!-- 占满剩余高度的 swiper --><swiper class="content-swiper" :current="currentTab" @change="onSwiperChange":style="{ height: swiperHeight + 'px' }"><swiper-item v-for="(tab, index) in tabs" :key="index" class="swiper-item"><scroll-view scroll-y class="content-scroll" :show-scrollbar="false"><!-- 内容区域 --><view class="content-box"><text class="title">{{ tab }} 的内容</text><view v-for="item in 20" :key="item" class="demo-item">{{ tab }} 的内容项 {{ item }}</view></view></scroll-view></swiper-item></swiper></view>
</template><script setup lang="ts">import { ref, getCurrentInstance } from 'vue'import { onLoad, onShow } from '@dcloudio/uni-app'const currentTab = ref(0)const tabs = ref(['全部', '矿场咨询', '安全教育', '岗位规范'])const swiperHeight = ref(500) // 初始高度,后续动态计算// 动态计算高度const calcSwiperHeight = () => {uni.getSystemInfo({success: (res) => {// 获取屏幕高度const screenHeight = res.windowHeight// 获取 tabs 容器高度(需要确保 DOM 已渲染)// 在 setup 中获取组件实例const instance = getCurrentInstance()const query = uni.createSelectorQuery().in(instance?.proxy)query.select('.tabs-container').boundingClientRect(data => {// 计算剩余高度 = 屏幕高度 - tabs 高度 - 安全区域(可选)const safeAreaInsets = res.safeAreaInsets?.top || 0swiperHeight.value = screenHeight - data.height - safeAreaInsets}).exec()}})}// 切换 Tabconst switchTab = (index : number) => {currentTab.value = index}// 滑动 swiper 时同步 Tabconst onSwiperChange = (e : any) => {currentTab.value = e.detail.current}// 初始化时计算高度onLoad(() => {calcSwiperHeight()})
</script><style scoped lang="scss">/* 页面容器 */.page-container {height: 100vh;display: flex;flex-direction: column;}/* 顶部 Tab 样式 */.tabs-container {position: sticky;top: 0;background: #fff;z-index: 999;box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.1);/* 确保 tabs 容器高度固定 */height: 90rpx;}/* swiper 容器 */.content-swiper {flex: 1;width: 100%;}/* swiper-item 内部滚动区域 */.content-scroll {height: 100%;padding: 30rpx;box-sizing: border-box;}/* 内容区域样式 */.content-box {background: #f5f5f5;border-radius: 16rpx;padding: 30rpx;}.title {font-size: 32rpx;font-weight: bold;margin-bottom: 30rpx;display: block;}.demo-item {padding: 20rpx;margin-bottom: 20rpx;background: #fff;border-radius: 8rpx;box-shadow: 0 2rpx 6rpx rgba(0, 0, 0, 0.05);}/* Tab 样式 */.tabs-scroll {white-space: nowrap;width: 100%;}.tabs {display: inline-flex;padding: 20rpx 30rpx;}.tab-item {position: relative;padding: 20rpx 40rpx;font-size: 28rpx;color: #666;transition: all 0.3s;}.tab-item.active {color: #007AFF;font-weight: 500;}.underline {position: absolute;bottom: 0;left: 50%;transform: translateX(-50%);width: 60%;height: 6rpx;background: #007AFF;border-radius: 3rpx;animation: underlineShow 0.3s ease-out;}@keyframes underlineShow {from {width: 0;opacity: 0;}to {width: 60%;opacity: 1;}}
</style>
http://www.dtcms.com/a/352307.html

相关文章:

  • 低空经济的中枢神经:实时视频链路如何支撑通信、导航、监视与气象
  • C/C++---浮点数与整形的转换,为什么使用sqrt函数时,要给参数加上一个极小的小数(如1e-6)
  • “喵汪联盟”宠物领养系统的设计与实现(代码+数据库+LW)
  • LangGraph
  • 研究4:海外休闲游戏,如何给主角做萌化处理
  • 基于SpringBoot的摄影跟拍约拍预约系统【2026最新】
  • C/C++---memset()初始化
  • 31.Encoder-Decoder(Seq2Seq)
  • MySQL8 排名窗口函数实战
  • 面试:Spring
  • 30.LSTM-长短时记忆单元
  • 抢红包案例加强版
  • 并行多核体系结构基础——共享存储并行编程(笔记)
  • 网络编程close学习
  • Java大厂面试实录:从Spring Boot到Kubernetes的全链路技术突围
  • python命名规则(PEP 8 速查表),以及自定义属性
  • 深度感知卷积和深度感知平均池化
  • python自动测试 crictl 可以从哪些国内镜像源成功拉取镜像
  • pulsar、rocketmq常用命令
  • C#由Dictionary不正确释放造成的内存泄漏问题与GC代系
  • Text to Speech技术详解与实战:GPT-4o Mini TTS API应用指南
  • 从“脚本语言”到“企业级引擎”——PHP 在 2025 年技术栈中的再定位
  • Linux服务器安全配置与NTP时间同步
  • 记录一下,qt问题:qt ui文件的改动无法更新到cpp
  • 疯狂星期四文案网第51天运营日记
  • Typescript入门-interface讲解
  • 类型签名,位置参数,关键字参数
  • open webui源码分析8—管道
  • 域名常见问题集(十一)——为什么要进行域名管理?
  • 【实时Linux实战系列】基于实时Linux的音频实时监控系统