开始

封装这个组件。在后面的话题分类也会用到这个组件。
image.png
swiper-tab-head
image.png
image.png
输入default
image.png
之前写的内容整个的复制过来
image.png

  1. <view class="uni-tab-bar">
  2. <scroll-view scroll-x class="uni-swiper-tab">
  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. {{tab.name}}
  6. <view class="swiper-tab-line"></view>
  7. </view>
  8. </block>
  9. </scroll-view>
  10. </view>

复制点击事件
image.png

image.png

  1. methods:{
  2. // tabbar点击事件
  3. tabtap(index) {
  4. this.tabIndex = index;
  5. }
  6. }

所有的css都复制过来

定义props传入的数据
image.png

  1. props:{
  2. tabBars: Array,
  3. tabIndex: Number
  4. },

父子组件的通讯

引入组件
image.png

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

注册一下这个组件
image.png

  1. components: {
  2. indexList,
  3. swiperTabHead
  4. },

使用组件
image.png

  1. <swiper-tab-head :tabBars="tabBars" :tabIndex="tabIndex"></swiper-tab-head>

当前索引传递给父组件。
image.png

  1. tabtap(index) {
  2. this.$emit('tabtap',index);
  3. }

父组件监听事件
image.png

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

image.png

  1. tabtap(index){
  2. console.log(index);
  3. this.tabIndex=index;
  4. }

image.png

本节代码

swiper-tab-head组件

  1. <template>
  2. <view class="uni-tab-bar">
  3. <scroll-view scroll-x class="uni-swiper-tab">
  4. <block v-for="(tab,index) in tabBars" :key="tab.id">
  5. <view class="swiper-tab-list" :class="{'active':tabIndex==index}" @tap="tabtap(index)">
  6. {{tab.name}}
  7. <view class="swiper-tab-line"></view>
  8. </view>
  9. </block>
  10. </scroll-view>
  11. </view>
  12. </template>
  13. <script>
  14. export default {
  15. props:{
  16. tabBars: Array,
  17. tabIndex: Number
  18. },
  19. methods:{
  20. // tabbar点击事件
  21. tabtap(index) {
  22. console.log('点击事件');
  23. this.$emit('tabtap',index);
  24. }
  25. }
  26. }
  27. </script>
  28. <style scoped>
  29. .uni-swiper-tab {
  30. border-bottom: 1upx solid #EEEEEE;
  31. }
  32. .swiper-tab-list {
  33. color: #969696;
  34. font-weight: bold;
  35. }
  36. .uni-tab-bar .active {
  37. color: #343434;
  38. }
  39. .active .swiper-tab-line {
  40. /* border-bottom: 6upx solid #FEDE33; */
  41. width: 70upx;
  42. margin-top: -30upx;
  43. /* margin: auto; */
  44. margin-left: 40upx;
  45. border-top: 12upx solid #FEDE33;
  46. border-radius: 20upx;
  47. }
  48. </style>

index.vue

  1. <template>
  2. <view>
  3. <swiper-tab-head
  4. :tabBars="tabBars"
  5. :tabIndex="tabIndex"
  6. @tabtap="tabtap">
  7. </swiper-tab-head>
  8. <!-- class="uni-tab-bar" -->
  9. <view>
  10. <swiper class="swiper-box"
  11. :style="{height: swiperheight+'px'}"
  12. :current="tabIndex"
  13. :change="tabChange">
  14. <swiper-item v-for="(items,index) in newslist" :key="index">
  15. <scroll-view scroll-y class="list">
  16. <block v-for="(item,index1) in items.list" :key="index1">
  17. <index-list :item="item" :index="index1"></index-list>
  18. </block>
  19. </scroll-view>
  20. </swiper-item>
  21. </swiper>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import indexList from '@/components/index/index-list.vue';
  27. import swiperTabHead from '@/components/index/swiper-tab-head.vue';
  28. export default {
  29. components: {
  30. indexList,
  31. swiperTabHead
  32. },
  33. data() {
  34. return {
  35. swiperheight:500,
  36. tabIndex: 0,
  37. tabBars: [{
  38. name: "关注",
  39. id: "guanzhu"
  40. },
  41. {
  42. name: "推荐",
  43. id: "tuijian"
  44. },
  45. {
  46. name: "体育",
  47. id: "tiyu"
  48. },
  49. {
  50. name: "热点",
  51. id: "redian"
  52. },
  53. {
  54. name: "财经",
  55. id: "caijing"
  56. },
  57. {
  58. name: "娱乐",
  59. id: "yule"
  60. },
  61. ],
  62. newslist: [{
  63. list: [{
  64. userpic: "../../static/demo/userpic/12.jpg",
  65. username: "昵称",
  66. isguanzhu: false,
  67. title: "我是标题",
  68. type: "img", // img:图文,video:视频
  69. titlepic: "../../static/demo/datapic/11.jpg",
  70. infonum: {
  71. index: 0, //0:没有操作,1:顶,2:踩;
  72. dingnum: 11,
  73. cainum: 11,
  74. },
  75. commentnum: 10,
  76. sharenum: 10,
  77. },
  78. {
  79. userpic: "../../static/demo/userpic/12.jpg",
  80. username: "昵称",
  81. isguanzhu: true,
  82. title: "我是标题",
  83. type: "video", // img:图文,video:视频
  84. titlepic: "../../static/demo/datapic/11.jpg",
  85. playnum: "20w",
  86. long: "2:47",
  87. infonum: {
  88. index: 1, //0:没有操作,1:顶,2:踩;
  89. dingnum: 11,
  90. cainum: 11,
  91. },
  92. commentnum: 10,
  93. sharenum: 10,
  94. }
  95. ]
  96. },
  97. {
  98. list: [{
  99. userpic: "../../static/demo/userpic/12.jpg",
  100. username: "昵称",
  101. isguanzhu: false,
  102. title: "我是标题",
  103. type: "img", // img:图文,video:视频
  104. titlepic: "../../static/demo/datapic/11.jpg",
  105. infonum: {
  106. index: 0, //0:没有操作,1:顶,2:踩;
  107. dingnum: 11,
  108. cainum: 11,
  109. },
  110. commentnum: 10,
  111. sharenum: 10,
  112. },
  113. {
  114. userpic: "../../static/demo/userpic/12.jpg",
  115. username: "昵称",
  116. isguanzhu: true,
  117. title: "我是标题",
  118. type: "video", // img:图文,video:视频
  119. titlepic: "../../static/demo/datapic/11.jpg",
  120. playnum: "20w",
  121. long: "2:47",
  122. infonum: {
  123. index: 1, //0:没有操作,1:顶,2:踩;
  124. dingnum: 11,
  125. cainum: 11,
  126. },
  127. commentnum: 10,
  128. sharenum: 10,
  129. },
  130. {
  131. userpic: "../../static/demo/userpic/12.jpg",
  132. username: "昵称",
  133. isguanzhu: false,
  134. title: "我是标题",
  135. type: "img", // img:图文,video:视频
  136. titlepic: "../../static/demo/datapic/11.jpg",
  137. infonum: {
  138. index: 0, //0:没有操作,1:顶,2:踩;
  139. dingnum: 11,
  140. cainum: 11,
  141. },
  142. commentnum: 10,
  143. sharenum: 10,
  144. },
  145. {
  146. userpic: "../../static/demo/userpic/12.jpg",
  147. username: "昵称",
  148. isguanzhu: true,
  149. title: "我是标题",
  150. type: "video", // img:图文,video:视频
  151. titlepic: "../../static/demo/datapic/11.jpg",
  152. playnum: "20w",
  153. long: "2:47",
  154. infonum: {
  155. index: 1, //0:没有操作,1:顶,2:踩;
  156. dingnum: 11,
  157. cainum: 11,
  158. },
  159. commentnum: 10,
  160. sharenum: 10,
  161. }
  162. ]
  163. },
  164. {
  165. list: [{
  166. userpic: "../../static/demo/userpic/12.jpg",
  167. username: "昵称",
  168. isguanzhu: false,
  169. title: "我是标题",
  170. type: "img", // img:图文,video:视频
  171. titlepic: "../../static/demo/datapic/11.jpg",
  172. infonum: {
  173. index: 0, //0:没有操作,1:顶,2:踩;
  174. dingnum: 11,
  175. cainum: 11,
  176. },
  177. commentnum: 10,
  178. sharenum: 10,
  179. },
  180. {
  181. userpic: "../../static/demo/userpic/12.jpg",
  182. username: "昵称",
  183. isguanzhu: true,
  184. title: "我是标题",
  185. type: "video", // img:图文,video:视频
  186. titlepic: "../../static/demo/datapic/11.jpg",
  187. playnum: "20w",
  188. long: "2:47",
  189. infonum: {
  190. index: 1, //0:没有操作,1:顶,2:踩;
  191. dingnum: 11,
  192. cainum: 11,
  193. },
  194. commentnum: 10,
  195. sharenum: 10,
  196. },
  197. {
  198. userpic: "../../static/demo/userpic/12.jpg",
  199. username: "昵称",
  200. isguanzhu: false,
  201. title: "我是标题",
  202. type: "img", // img:图文,video:视频
  203. titlepic: "../../static/demo/datapic/11.jpg",
  204. infonum: {
  205. index: 0, //0:没有操作,1:顶,2:踩;
  206. dingnum: 11,
  207. cainum: 11,
  208. },
  209. commentnum: 10,
  210. sharenum: 10,
  211. },
  212. {
  213. userpic: "../../static/demo/userpic/12.jpg",
  214. username: "昵称",
  215. isguanzhu: true,
  216. title: "我是标题",
  217. type: "video", // img:图文,video:视频
  218. titlepic: "../../static/demo/datapic/11.jpg",
  219. playnum: "20w",
  220. long: "2:47",
  221. infonum: {
  222. index: 1, //0:没有操作,1:顶,2:踩;
  223. dingnum: 11,
  224. cainum: 11,
  225. },
  226. commentnum: 10,
  227. sharenum: 10,
  228. }
  229. ]
  230. },
  231. {
  232. list: []
  233. },
  234. {
  235. list: []
  236. },
  237. {
  238. list: []
  239. }
  240. ]
  241. }
  242. },
  243. onLoad() {
  244. uni.getSystemInfo({
  245. success: (res) => {
  246. console.log('当前window高度和窗口高度');
  247. console.log(res.windowHeight);
  248. console.log(res.screenHeight);
  249. let height = res.windowHeight - uni.upx2px(100)
  250. this.swiperheight = height+100;
  251. }
  252. });
  253. },
  254. methods: {
  255. // 滑动事件
  256. tabChange(e) {
  257. this.tabIndex = e.details.current
  258. },
  259. tabtap(index){
  260. console.log(index);
  261. this.tabIndex=index;
  262. }
  263. }
  264. }
  265. </script>
  266. <style>
  267. /* .swiper-box{
  268. height: 700upx!important;
  269. } */
  270. </style>

结束