开始

tab切换和下面的列表。
image.png
tab切换和首页的是类似的。首页的可以左右的滚动。
image.png
我们对首页的tab切换进行二次开发,让它兼容话题详情页的。

列表页也和动态的页面类似的。
image.png

tab切换

首页是引入的这个组件
image.png

image.png

  1. import swiperTabHead from '@/components/index/swiper-tab-head.vue';
  2. swiperTabHead

首页的代码复制过来
image.png

  1. <!-- tabbar切换 -->
  2. <swiper-tab-head
  3. :tabBars="tabBars"
  4. :tabIndex="tabIndex"
  5. @tabtap="tabtap">
  6. </swiper-tab-head>

默认选中第一个tab
image.png

  1. tabBars: [{
  2. name: "默认",
  3. id: "moren"
  4. },
  5. {
  6. name: "最新",
  7. id: "zuixin"
  8. },
  9. ]

tab切换的事件
image.png

  1. // tabbar点击事件
  2. tabtap(index) {
  3. // console.log(index);
  4. this.tabIndex = index;
  5. },

tab下面有一条线,要删掉,让里面每一个元素占50%。
image.png
单独改组件。swiper-tab-head.vue
image.png

  1. style="border-bottom: 0;"
  2. style="width: 50%;"

image.png
组件内自定义这个style
image.png

  1. <view class="uni-tab-bar">
  2. <scroll-view scroll-x class="uni-swiper-tab" :style="scrollStyle">
  3. <block v-for="(tab,index) in tabBars" :key="tab.id">
  4. <view class="swiper-tab-list" :class="{'active':tabIndex==index}" @tap="tabtap(index)"
  5. :style="scrollItemStyle">
  6. {{tab.name}}
  7. <view class="swiper-tab-line u-f-ajc">
  8. <view class="swiper-tab-line-child"></view>
  9. </view>
  10. </view>
  11. </block>
  12. </scroll-view>
  13. </view>

定义这两个props属性
image.png

  1. scrollStyle:{
  2. type: String,
  3. default: ''
  4. },
  5. scrollItemStyle:{
  6. type: String,
  7. default: ''
  8. }

调用组件的时候传这两个属性
image.png

  1. <swiper-tab-head
  2. :tabBars="tabBars"
  3. :tabIndex="tabIndex"
  4. @tabtap="tabtap"
  5. scrollStyle="border-bottom:0;"
  6. scrollItemStyle="width:50%;">
  7. </swiper-tab-head>

首页tab正常
image.png

话题详情页
image.png

列表

和关注列表页样式相同,所以参考
image.png

image.png
tablist里面有两个对象,分别对应两个tab
image.png
直接复制news页面的
image.png

image.png

  1. tablist:[
  2. {
  3. loadtext:"上拉加载更多",
  4. list:[
  5. // 文字
  6. {
  7. userpic:"../../static/demo/userpic/12.jpg",
  8. username:"哈哈",
  9. sex:0, //0 男 1 女
  10. age:25,
  11. isguanzhu:false,
  12. title:"我是标题",
  13. titlepic:"",
  14. video:false,
  15. share:false,
  16. path:"深圳 龙岗",
  17. sharenum:20,
  18. commentnum:30,
  19. goodnum:20
  20. },
  21. // 图文
  22. {
  23. userpic:"../../static/demo/userpic/12.jpg",
  24. username:"哈哈",
  25. sex:0, //0 男 1 女
  26. age:25,
  27. isguanzhu:false,
  28. title:"我是标题",
  29. titlepic:"../../static/demo/datapic/13.jpg",
  30. video:false,
  31. share:false,
  32. path:"深圳 龙岗",
  33. sharenum:20,
  34. commentnum:30,
  35. goodnum:20
  36. },
  37. // 视频
  38. {
  39. userpic:"../../static/demo/userpic/12.jpg",
  40. username:"哈哈",
  41. sex:0, //0 男 1 女
  42. age:25,
  43. isguanzhu:false,
  44. title:"我是标题",
  45. titlepic:"../../static/demo/datapic/13.jpg",
  46. video:{
  47. looknum:"20w",
  48. long:"2:47"
  49. },
  50. share:false,
  51. path:"深圳 龙岗",
  52. sharenum:20,
  53. commentnum:30,
  54. goodnum:20
  55. },
  56. ]
  57. },
  58. {
  59. loadtext:"上拉加载更多",
  60. list:[
  61. // 文字
  62. {
  63. userpic:"../../static/demo/userpic/12.jpg",
  64. username:"哈哈",
  65. sex:0, //0 男 1 女
  66. age:25,
  67. isguanzhu:false,
  68. title:"我是标题",
  69. titlepic:"",
  70. video:false,
  71. share:false,
  72. path:"深圳 龙岗",
  73. sharenum:20,
  74. commentnum:30,
  75. goodnum:20
  76. },
  77. // 图文
  78. {
  79. userpic:"../../static/demo/userpic/12.jpg",
  80. username:"哈哈",
  81. sex:0, //0 男 1 女
  82. age:25,
  83. isguanzhu:false,
  84. title:"我是标题",
  85. titlepic:"../../static/demo/datapic/13.jpg",
  86. video:false,
  87. share:false,
  88. path:"深圳 龙岗",
  89. sharenum:20,
  90. commentnum:30,
  91. goodnum:20
  92. },
  93. // 视频
  94. {
  95. userpic:"../../static/demo/userpic/12.jpg",
  96. username:"哈哈",
  97. sex:0, //0 男 1 女
  98. age:25,
  99. isguanzhu:false,
  100. title:"我是标题",
  101. titlepic:"../../static/demo/datapic/13.jpg",
  102. video:{
  103. looknum:"20w",
  104. long:"2:47"
  105. },
  106. share:false,
  107. path:"深圳 龙岗",
  108. sharenum:20,
  109. commentnum:30,
  110. goodnum:20
  111. },
  112. ]
  113. },
  114. ]

在里面还要循环
引入公共列表的组件。
image.png

image.png

  1. import commonList from '@/components/common/common-list.vue';
  2. components: {
  3. topicInfo,
  4. swiperTabHead,
  5. commonList
  6. },

image.png

  1. <view class="topic-detail-list">
  2. <block v-for="(item,index) in tablist" :key="index">
  3. <template v-if="tabIndex==index">
  4. <block v-for="(list,listindex) in item.list" :key="listindex">
  5. <common-list :item="list" :index="listindex"></common-list>
  6. </block>
  7. </template>
  8. </block>
  9. </view>

image.png

最终代码

组件swiper-tab-header.vue

  1. <template>
  2. <view class="uni-tab-bar">
  3. <scroll-view scroll-x class="uni-swiper-tab" :style="scrollStyle">
  4. <block v-for="(tab,index) in tabBars" :key="tab.id">
  5. <view class="swiper-tab-list" :class="{'active':tabIndex==index}"
  6. @tap="tabtap(index)"
  7. :style="scrollItemStyle">
  8. {{tab.name}}
  9. <view class="swiper-tab-line u-f-ajc">
  10. <view class="swiper-tab-line-child"></view>
  11. </view>
  12. </view>
  13. </block>
  14. </scroll-view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. props: {
  20. tabBars: Array,
  21. tabIndex: Number,
  22. scrollStyle:{
  23. type: String,
  24. default: ''
  25. },
  26. scrollItemStyle:{
  27. type: String,
  28. default: ''
  29. }
  30. },
  31. methods: {
  32. // tabbar点击事件
  33. tabtap(index) {
  34. console.log('点击事件');
  35. this.$emit('tabtap', index);
  36. }
  37. }
  38. }
  39. </script>
  40. <style scoped>
  41. .uni-swiper-tab {
  42. border-bottom: 1upx solid #EEEEEE;
  43. }
  44. .swiper-tab-list {
  45. color: #969696;
  46. font-weight: bold;
  47. }
  48. .uni-tab-bar .active {
  49. color: #343434;
  50. }
  51. .active .swiper-tab-line {
  52. /* border-bottom: 6upx solid #FEDE33; */
  53. /* width: 70upx;
  54. margin-top: -30upx;
  55. margin: auto;
  56. margin-left: 40upx;
  57. border-top: 12upx solid #FEDE33;
  58. border-radius: 20upx;*/
  59. /* 用相对定位,子元素用绝对定位再定位于一下 */
  60. position: relative;
  61. }
  62. .swiper-tab-line-child{
  63. width: 80upx;
  64. border-top: 15upx solid #FEDE33;
  65. border-radius: 20upx;
  66. position: absolute;
  67. top:-28upx;
  68. }
  69. </style>

topic-detail.vue页面

  1. <template>
  2. <view>
  3. <!-- 话题介绍 -->
  4. <topic-info :item="topicInfo"></topic-info>
  5. <!-- tabbar切换 -->
  6. <swiper-tab-head :tabBars="tabBars" :tabIndex="tabIndex" @tabtap="tabtap" scrollStyle="border-bottom:0;"
  7. scrollItemStyle="width:50%;">
  8. </swiper-tab-head>
  9. <view class="topic-detail-list">
  10. <block v-for="(item,index) in tablist" :key="index">
  11. <template v-if="tabIndex==index">
  12. <block v-for="(list,listindex) in item.list" :key="listindex">
  13. <common-list :item="list" :index="listindex"></common-list>
  14. </block>
  15. </template>
  16. </block>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. import topicInfo from '@/components/topic/topic-info.vue';
  22. import swiperTabHead from '@/components/index/swiper-tab-head.vue';
  23. import commonList from '@/components/common/common-list.vue';
  24. export default {
  25. components: {
  26. topicInfo,
  27. swiperTabHead,
  28. commonList
  29. },
  30. data() {
  31. return {
  32. topicInfo: {
  33. titlepic: "../../static/demo/topicpic/13.jpeg",
  34. title: "忆往事,敬余生",
  35. desc: "我是描述",
  36. totalnum: 1000,
  37. todaynum: 1000,
  38. },
  39. tabIndex: 0,
  40. tabBars: [{
  41. name: "默认",
  42. id: "moren"
  43. },
  44. {
  45. name: "最新",
  46. id: "zuixin"
  47. },
  48. ],
  49. tablist: [{
  50. loadtext: "上拉加载更多",
  51. list: [
  52. // 文字
  53. {
  54. userpic: "../../static/demo/userpic/12.jpg",
  55. username: "哈哈",
  56. sex: 0, //0 男 1 女
  57. age: 25,
  58. isguanzhu: false,
  59. title: "我是标题",
  60. titlepic: "",
  61. video: false,
  62. share: false,
  63. path: "深圳 龙岗",
  64. sharenum: 20,
  65. commentnum: 30,
  66. goodnum: 20
  67. },
  68. // 图文
  69. {
  70. userpic: "../../static/demo/userpic/12.jpg",
  71. username: "哈哈",
  72. sex: 0, //0 男 1 女
  73. age: 25,
  74. isguanzhu: false,
  75. title: "我是标题",
  76. titlepic: "../../static/demo/datapic/13.jpg",
  77. video: false,
  78. share: false,
  79. path: "深圳 龙岗",
  80. sharenum: 20,
  81. commentnum: 30,
  82. goodnum: 20
  83. },
  84. // 视频
  85. {
  86. userpic: "../../static/demo/userpic/12.jpg",
  87. username: "哈哈",
  88. sex: 0, //0 男 1 女
  89. age: 25,
  90. isguanzhu: false,
  91. title: "我是标题",
  92. titlepic: "../../static/demo/datapic/13.jpg",
  93. video: {
  94. looknum: "20w",
  95. long: "2:47"
  96. },
  97. share: false,
  98. path: "深圳 龙岗",
  99. sharenum: 20,
  100. commentnum: 30,
  101. goodnum: 20
  102. },
  103. ]
  104. },
  105. {
  106. loadtext: "上拉加载更多",
  107. list: [
  108. // 文字
  109. {
  110. userpic: "../../static/demo/userpic/12.jpg",
  111. username: "哈哈",
  112. sex: 0, //0 男 1 女
  113. age: 25,
  114. isguanzhu: false,
  115. title: "我是标题",
  116. titlepic: "",
  117. video: false,
  118. share: false,
  119. path: "深圳 龙岗",
  120. sharenum: 20,
  121. commentnum: 30,
  122. goodnum: 20
  123. },
  124. // 图文
  125. {
  126. userpic: "../../static/demo/userpic/12.jpg",
  127. username: "哈哈",
  128. sex: 0, //0 男 1 女
  129. age: 25,
  130. isguanzhu: false,
  131. title: "我是标题",
  132. titlepic: "../../static/demo/datapic/13.jpg",
  133. video: false,
  134. share: false,
  135. path: "深圳 龙岗",
  136. sharenum: 20,
  137. commentnum: 30,
  138. goodnum: 20
  139. },
  140. // 视频
  141. {
  142. userpic: "../../static/demo/userpic/12.jpg",
  143. username: "哈哈",
  144. sex: 0, //0 男 1 女
  145. age: 25,
  146. isguanzhu: false,
  147. title: "我是标题",
  148. titlepic: "../../static/demo/datapic/13.jpg",
  149. video: {
  150. looknum: "20w",
  151. long: "2:47"
  152. },
  153. share: false,
  154. path: "深圳 龙岗",
  155. sharenum: 20,
  156. commentnum: 30,
  157. goodnum: 20
  158. },
  159. ]
  160. },
  161. ]
  162. }
  163. },
  164. methods: {
  165. // tabbar点击事件
  166. tabtap(index) {
  167. // console.log(index);
  168. this.tabIndex = index;
  169. }
  170. }
  171. }
  172. </script>
  173. <style>
  174. </style>

结束