应无所住,而生其心
排名
1
文章
860
粉丝
112
评论
163
net core webapi post传递参数
庸人 : 确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

uni-app tab切换,tab样式。可以滑动实现页面切换的。tab菜单滑动切换。下方内容在剩下的高度滚动,上方的tab菜单固定不跟随滚动。左右切换 滑动菜单

1133人阅读 2020/11/9 13:49 总访问:5182825 评论:0 收藏:0 手机
分类: 前端

一:需要内容页可以滑动切换,tab菜单会跟着选中。但tab菜单固定显示,不需要滑动切换的

代码:

  1. <template>
  2. <div class="body-view">
  3. <view class="top-menu-view" scroll-x="true" :scroll-into-view="tabCurrent">
  4. <view class="menu-topic-view" v-for="item in tabs" :id="'tabNum' + item.id" :key="(item.id - 1)" @click="switchMenu(item.id - 1)">
  5. <view :class="currentTab === (item.id - 1) ? 'menu-topic-act' : 'menu-topic'">
  6. <span class="menu-topic-text">{{ item.name }}</span>
  7. <view class="menu-topic-bottom">
  8. <view class="menu-topic-bottom-color"></view>
  9. </view>
  10. </view>
  11. </view>
  12. </view>
  13. <!-- 内容 -->
  14. <swiper class="swiper-box-list" :current="currentTab" @change="swiperChange">
  15. <swiper-item class="swiper-topic-list" v-for="item in swiperDateList" :key="item.id">
  16. <div class="swiper-item">
  17. {{ item.content }}
  18. </div>
  19. </swiper-item>
  20. </swiper>
  21. </div>
  22. </template>
  23. <script setup lang="ts">
  24. import { ref, onMounted } from 'vue';
  25. const tabs = ref<any>([
  26. { id: 1, name: '日数据' },
  27. { id: 2, name: '周数据' },
  28. { id: 3, name: '月数据' },
  29. ])
  30. const currentTab = ref(0);
  31. const tabCurrent = ref('tabNum1');
  32. const swiperDateList = ref<any>([
  33. { id: 1, content: '日数据内容' },
  34. { id: 2, content: '周数据内容' },
  35. { id: 3, content: '月数据内容' },
  36. ])
  37. const switchMenu = (id: number) => {
  38. currentTab.value = id;
  39. console.log(11, id);
  40. tabCurrent.value = 'tabNum' + id;
  41. };
  42. const swiperChange = (e: { detail: { current: number } }) => {
  43. console.log(22, e.detail.current);
  44. const index = e.detail.current;
  45. switchMenu(index);
  46. };
  47. onMounted(() => {
  48. // Perform any initialization logic here
  49. });
  50. </script>
  51. <style scoped lang="scss">
  52. .body-view {
  53. height: 100vh;
  54. width: 100%;
  55. display: flex;
  56. flex: 1;
  57. flex-direction: column;
  58. overflow: hidden;
  59. align-items: flex-start;
  60. justify-content: center;
  61. }
  62. .top-menu-view {
  63. display: flex;
  64. white-space: nowrap;
  65. width: 100%;
  66. background-color: #FFFFFF;
  67. height: 86rpx;
  68. line-height: 86rpx;
  69. justify-content: space-between;
  70. .menu-topic-view {
  71. display: inline-block;
  72. white-space: nowrap;
  73. height: 86rpx;
  74. position: relative;
  75. .menu-topic-text {
  76. font-size: 30rpx;
  77. color: #303133;
  78. padding: 10rpx 40rpx;
  79. }
  80. .menu-topic-bottom {
  81. position: absolute;
  82. bottom: 0;
  83. width: 100%;
  84. .menu-topic-bottom-color {
  85. width: 40rpx;
  86. height: 4rpx;
  87. }
  88. }
  89. .menu-topic-act .menu-topic-bottom {
  90. display: flex;
  91. justify-content: center;
  92. }
  93. .menu-topic-act .menu-topic-bottom-color {
  94. background: #3d7eff;
  95. }
  96. }
  97. }
  98. .swiper-box-list {
  99. width: 100%;
  100. padding-top: 200rpx;
  101. flex: 1;
  102. .swiper-topic-list {
  103. width: 100%;
  104. }
  105. }
  106. </style>

二:需要tab可以滑动切换的,内容页不需要左右滑动切换。下方内容在剩下的高度滚动,上方的tab菜单固定不跟随滚动

代码:

  1. <template>
  2. <!-- 解决滚动穿透 -->
  3. <page-meta :page-style="'overflow:' + (state.popupShow ? 'hidden' : 'visible')"></page-meta>
  4. <view class="body-view">
  5. <scroll-view class="top-menu-view" scroll-x="true" :scroll-into-view="tabCurrent">
  6. <div class="menu-topic-view" v-for="item in tabs" :id="'tabNum' + item.id" :key="(item.id - 1)"
  7. @click="switchMenu(item.id - 1)">
  8. <div :class="currentTab === (item.id - 1) ? 'menu-topic-act' : 'menu-topic'">
  9. <span class="menu-topic-text">{{ item.name }}</span>
  10. <div class="menu-topic-bottom">
  11. <div class="menu-topic-bottom-color"></div>
  12. </div>
  13. </div>
  14. </div>
  15. </scroll-view>
  16. <view class="class-info">
  17. <view class="ci-className">23级移动互联应用技术1班</view>
  18. <image :src="`${config.imgBaseUrl}/mp-weixin/smartedu-growing/teacher-icon/arrow.png`"
  19. class="class-info-icon-arrow" />
  20. </view>
  21. <view style="">
  22. <scroll-view scroll-y="true" style="height:calc(100vh - 160rpx);">
  23. <view class="do-item-wrap">
  24. <view class="do-item" v-for="(initem, index) in 13" :key="index">
  25. <view class="di-task-desc">
  26. <view class="di-td-index">{{ index + 1 }}</view>
  27. <view class="di-td-title">体能测试—跑步(女子800米/男子1000米)</view>
  28. </view>
  29. <view class="di-task-spline"></view>
  30. <view class="di-task-statictics">
  31. <view class="di-ts-count">
  32. <view class="di-tst-desc">完成次数:</view>
  33. <view class="di-tst-value">2次</view>
  34. </view>
  35. <view class="di-ts-buttons">
  36. <view class="di-tst-details" @tap="popupMethods.showMonthTaskDetails">详情</view>
  37. <view class="di-tst-tocomplete" @tap="methods.toCompletePage" v-if="index < 5">去完成</view>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </scroll-view>
  43. </view>
  44. <uni-popup ref="monthTaskDetailsPopup" background-color="#fff" border-radius="60rpx 60rpx 0rpx 0rpx;" type="bottom"
  45. @change="methods.change">
  46. <monthTaskDetails @closePopup="popupMethods.closeMonthTaskDetails" title="参加班级扫除道"> </monthTaskDetails>
  47. </uni-popup>
  48. </view>
  49. </template>
  50. <script setup lang="ts">
  51. import { ref, onMounted, reactive } from 'vue';
  52. import config from '@/common/config.ts'
  53. import monthTaskDetails from './component/monthTaskDetails.vue'
  54. const tabs = ref<any>([
  55. { id: 1, name: '全部' },
  56. { id: 2, name: '德' },
  57. { id: 3, name: '智' },
  58. { id: 4, name: '体' },
  59. { id: 5, name: '美' },
  60. { id: 6, name: '劳' },
  61. { id: 7, name: '技术' },
  62. { id: 8, name: '健康' },
  63. { id: 9, name: '思维' },
  64. ])
  65. const currentTab = ref(0);
  66. const tabCurrent = ref('tabNum1');
  67. const state = reactive({
  68. popupShow: false,
  69. })
  70. const switchMenu = (id: number) => {
  71. currentTab.value = id;
  72. // console.log(11, id);
  73. tabCurrent.value = 'tabNum' + id;
  74. };
  75. onMounted(() => {
  76. });
  77. const methods = {
  78. change: function (e: any) {
  79. state.popupShow = e.show
  80. },
  81. toCompletePage: function () {
  82. uni.navigateTo({
  83. url: "/pages/student/toDoMonthTask"
  84. })
  85. }
  86. }
  87. let monthTaskDetailsPopup = ref()
  88. const popupMethods = {
  89. // 打开弹窗
  90. showMonthTaskDetails: () => {
  91. monthTaskDetailsPopup.value.open()
  92. },
  93. // 关闭弹窗
  94. closeMonthTaskDetails: () => {
  95. monthTaskDetailsPopup.value.close()
  96. }
  97. }
  98. </script>
  99. <style scoped lang="scss">
  100. .body-view {
  101. // min-height: 100vh;
  102. // width: 100%;
  103. display: flex;
  104. height: 100vh;
  105. flex-direction: column;
  106. // overflow: hidden;
  107. // align-items: flex-start;
  108. // justify-content: center;
  109. }
  110. .top-menu-view {
  111. display: flex;
  112. position: fixed;
  113. top: 0rpx;
  114. left: 0;
  115. white-space: nowrap;
  116. width: 100%;
  117. background-color: #FFFFFF;
  118. height: 86rpx;
  119. line-height: 86rpx;
  120. // border-top: 1rpx solid #d8dbe6;
  121. .menu-topic-view {
  122. display: inline-block;
  123. white-space: nowrap;
  124. height: 86rpx;
  125. position: relative;
  126. .menu-topic-text {
  127. font-size: 30rpx;
  128. color: #303133;
  129. padding: 10rpx 40rpx;
  130. }
  131. .menu-topic-bottom {
  132. position: absolute;
  133. bottom: 0;
  134. width: 100%;
  135. .menu-topic-bottom-color {
  136. width: 40rpx;
  137. height: 4rpx;
  138. }
  139. }
  140. .menu-topic-act .menu-topic-bottom {
  141. display: flex;
  142. justify-content: center;
  143. }
  144. .menu-topic-act .menu-topic-bottom-color {
  145. background: #3d7eff;
  146. }
  147. }
  148. }
  149. .class-info {
  150. margin-top: 100rpx;
  151. // top: 100rpx;
  152. // position: fixed;
  153. width: 100%;
  154. height: 77rpx;
  155. background: #FFFFFF;
  156. font-size: 26rpx;
  157. color: #313960;
  158. display: flex;
  159. align-items: center;
  160. justify-content: space-between;
  161. .ci-className {
  162. margin-left: 20rpx;
  163. }
  164. .class-info-icon-arrow {
  165. width: 24rpx;
  166. height: 13rpx;
  167. margin-right: 20rpx;
  168. }
  169. }
  170. .do-item-wrap {
  171. // width: 100%;
  172. padding-left: 20rpx;
  173. padding-right: 20rpx;
  174. padding-bottom: 20rpx;
  175. .di-task-spline {
  176. height: 1rpx;
  177. background: #000000;
  178. opacity: 0.1;
  179. margin-top: 20rpx;
  180. margin-left: 20rpx;
  181. margin-right: 20rpx;
  182. }
  183. .do-item {
  184. width: 100%;
  185. height: 161rpx;
  186. background: #FFFFFF;
  187. margin-top: 20rpx;
  188. .di-task-desc {
  189. display: flex;
  190. padding-left: 20rpx;
  191. padding-top: 20rpx;
  192. .di-td-index {
  193. width: 34rpx;
  194. height: 34rpx;
  195. background: #4D9DF5;
  196. border-radius: 10rpx 10rpx 10rpx 10rpx;
  197. color: #FFFFFF;
  198. font-size: 26rpx;
  199. text-align: center;
  200. line-height: 34rpx;
  201. }
  202. .di-td-title {
  203. font-size: 28rpx;
  204. color: #313960;
  205. margin-left: 20rpx;
  206. }
  207. }
  208. .di-task-statictics {
  209. display: flex;
  210. align-items: center;
  211. justify-content: space-between;
  212. margin-top: 26rpx;
  213. padding-left: 20rpx;
  214. padding-right: 20rpx;
  215. .di-ts-count {
  216. display: flex;
  217. font-size: 24rpx;
  218. .di-tst-desc {
  219. color: rgba(0, 0, 0, 0.5)
  220. }
  221. .di-tst-value {
  222. color: #313960;
  223. }
  224. }
  225. .di-ts-buttons {
  226. display: flex;
  227. .di-tst-details {
  228. width: 90rpx;
  229. height: 40rpx;
  230. border-radius: 35rpx 35rpx 35rpx 35rpx;
  231. border: 2rpx solid #4D9DF5;
  232. font-size: 24rpx;
  233. color: #4D9DF5;
  234. text-align: center;
  235. line-height: 40rpx;
  236. }
  237. .di-tst-tocomplete {
  238. text-align: center;
  239. line-height: 40rpx;
  240. color: #67C23A;
  241. font-size: 24rpx;
  242. width: 114rpx;
  243. height: 40rpx;
  244. border-radius: 35rpx 35rpx 35rpx 35rpx;
  245. border: 2rpx solid #67C23A;
  246. margin-left: 15rpx;
  247. }
  248. }
  249. }
  250. }
  251. }
  252. </style>

三:tab菜单和内容页都可以左右滑动切换的

非常基础版本的代码,需要改造

  1. <template>
  2. <div class="body-view">
  3. <!-- 使用scroll-view实现tabs滑动切换 -->
  4. <scroll-view class="top-menu-view" scroll-x="true" :scroll-into-view="tabCurrent">
  5. <div class="menu-topic-view" v-for="item in tabs" :id="'tabNum' + item.id" :key="(item.id - 1)" @click="switchMenu(item.id - 1)">
  6. <div :class="currentTab === (item.id - 1) ? 'menu-topic-act' : 'menu-topic'">
  7. <span class="menu-topic-text">{{ item.name }}</span>
  8. <div class="menu-topic-bottom">
  9. <div class="menu-topic-bottom-color"></div>
  10. </div>
  11. </div>
  12. </div>
  13. </scroll-view>
  14. <!-- 内容 -->
  15. <swiper class="swiper-box-list" :current="currentTab" @change="swiperChange">
  16. <swiper-item class="swiper-topic-list" v-for="item in swiperDateList" :key="item.id">
  17. <div class="swiper-item">
  18. {{ item.content }}
  19. </div>
  20. </swiper-item>
  21. </swiper>
  22. </div>
  23. </template>
  24. <script setup lang="ts">
  25. import { ref, onMounted } from 'vue';
  26. const tabs = ref<any>([
  27. { id: 1, name: '推荐' },
  28. { id: 2, name: '交通交通' },
  29. { id: 3, name: '住房' },
  30. { id: 4, name: '社会保障' },
  31. { id: 5, name: '民生热点' },
  32. { id: 6, name: '即日头条' },
  33. { id: 7, name: '新闻联播' },
  34. ])
  35. const currentTab = ref(0);
  36. const tabCurrent = ref('tabNum1');
  37. const swiperDateList = ref<any>([
  38. { id: 1, content: '推荐' },
  39. { id: 2, content: '交通交通' },
  40. { id: 3, content: '住房' },
  41. { id: 4, content: '社会保障' },
  42. { id: 5, content: '民生热点' },
  43. { id: 6, content: '即日头条' },
  44. { id: 7, content: '新闻联播' },
  45. ])
  46. const switchMenu = (id: number) => {
  47. currentTab.value = id;
  48. console.log(11, id);
  49. tabCurrent.value = 'tabNum' + id;
  50. };
  51. const swiperChange = (e: { detail: { current: number } }) => {
  52. console.log(22, e.detail.current);
  53. const index = e.detail.current;
  54. switchMenu(index);
  55. };
  56. onMounted(() => {
  57. // Perform any initialization logic here
  58. });
  59. </script>
  60. <style scoped lang="scss">
  61. .body-view {
  62. height: 100vh;
  63. width: 100%;
  64. display: flex;
  65. flex: 1;
  66. flex-direction: column;
  67. overflow: hidden;
  68. align-items: flex-start;
  69. justify-content: center;
  70. }
  71. .top-menu-view {
  72. display: flex;
  73. position: fixed;
  74. top: 100rpx;
  75. left: 0;
  76. white-space: nowrap;
  77. width: 100%;
  78. background-color: #FFFFFF;
  79. height: 86rpx;
  80. line-height: 86rpx;
  81. border-top: 1rpx solid #d8dbe6;
  82. .menu-topic-view {
  83. display: inline-block;
  84. white-space: nowrap;
  85. height: 86rpx;
  86. position: relative;
  87. .menu-topic-text {
  88. font-size: 30rpx;
  89. color: #303133;
  90. padding: 10rpx 40rpx;
  91. }
  92. .menu-topic-bottom {
  93. position: absolute;
  94. bottom: 0;
  95. width: 100%;
  96. .menu-topic-bottom-color {
  97. width: 40rpx;
  98. height: 4rpx;
  99. }
  100. }
  101. .menu-topic-act .menu-topic-bottom {
  102. display: flex;
  103. justify-content: center;
  104. }
  105. .menu-topic-act .menu-topic-bottom-color {
  106. background: #3d7eff;
  107. }
  108. }
  109. }
  110. .swiper-box-list {
  111. width: 100%;
  112. padding-top: 200rpx;
  113. flex: 1;
  114. .swiper-topic-list {
  115. width: 100%;
  116. }
  117. }
  118. </style>

四:备注

更多样式请参考,存储的模板


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

layui tab切换更改同一个内容

layui tab切换时如何更改同一个内容呢,不用一个tab对应一个内容,就相当于改变同一个内容,其实很简单就是只留layui-tab-c...

vue路由实现一个二级菜单vue页面切换vue子页面切换vue菜单选中

效果如下:这种二级菜单或者三级菜单如果我们使用.net 的mvc来实现就很简单,整两个布局页可以轻松搞定来试试使用vue的路由...

CPU上下文切换

CPU上下文切换[TOC] (0) CPU寄存器,是内置的容量小、但速度极快的内存。而程序计数器,再则是用来存储CPU正在执行的...

vue 动态切换class

vue动态切换class方式一:&lt;div :class=&quot;{ active: isActive }&quot;&gt;&lt;/div&gt;表示active 是否存在,取决于 ...

Vscode 切换工作目录

Vscode 切换工作目录[TOC] Ctrl+Shift+P 即可选择运行项目

div 点击事件 切换div class 和 style

$(&quot;.but&quot;).on(&quot;click&quot;,function(){vardiv=$(&quot;div[class=&#39;but&#39;]&quot;);//获取全部class...

Angular路由复用策略 (切换tabs页面内容不丢失保持原来状态)

一、引言 路由在执行过程中对组件无状态操作,即路由离退时组件状态也一并被删除;当然在绝大多数场景下这是合理的。进入...

dbvis切换数据库安装驱动与虚拟机交互

前言:hello 大家好我是小付 又和大家见面了,今天和大家分享一个踩坑日常dbvis 没有安装驱动导致连接不上数据库。dbvis是...

CPU上下文切换(下)

CPU上下文切换(下)[TOC] 查看系统的上下文切换情况 vmstat工具介绍 vmstat是一个常用的系统性能分析工具,主要用来...

spring boot 自动配置之切换内置服务web

1、修改pom.xml的配置就是把tomcat服务器换成其他服务器,总共有四种服务器2、直接启动

Git 的版本管理04——01切换版本

查看所有分支的所有记录(包括已经被删除的commit记录):git reflog切换版本:git reset --hard 唯一索引接着Git常用命令0...

Git 的版本管理04——02创建、切换、合并、删除分支

查看分支下的文件:ls查看所有分支:git branch创建分支:git branch 分支名切换分支:git checkout 分支名合并分支:git m...

\\访问文件访问共享文件夹 怎么切换账号

设置访问共享文件夹账户信息的位置在:控制面板—&gt;用户帐户—&gt;管理你的凭据—&gt;windows凭据 管理你的凭据在这里打...

vue切换菜单vue菜单选中跳转页面通过原生js设置选中样式vue中使用原生js方法js 找到当前对象的兄弟对象js获取子节点js获取父节点js获取当前元素的同级节点

vue切换菜单,跳转页面通过原生js设置选中样式可以利用上面那个js获取当前元素的同级节点后先设置默认的样式,然后在设置点...

vue 布局页vue模板页页面跳转子页面切换页面切换页面模板布局模板vue路由配置

vue里边点击菜单切换页面的效果,其实和mvc里边的布局页很类似。使用router-view来挖坑,和mvc里边的@RenderBody()挖坑一样...