开始

先完善下面内容的切换。

首先来看官方给的模板是怎么切换的。下面用的是swiper
image.png
先来看下官方给的组件
https://uniapp.dcloud.io/component/swiper.html
image.png
image.png
里面嵌套的就是scroll-view
image.png
image.png
生成这一堆
image.png
滚动的时候 ,底部的点 滚动到第几个了。
image.png
这里我们就设置为false,然后自动播放也设置为false

image.png
都是默认false的 所以就取消掉了。
image.png

  1. <swiper>
  2. <swiper-item>
  3. <view class="swiper-item"></view>
  4. </swiper-item>
  5. <swiper-item>
  6. <view class="swiper-item"></view>
  7. </swiper-item>
  8. </swiper>

里面就嵌套scroll-view
image.png
把官方例子的class复制过来。
image.png

image.png
连就放我们的列表数据
image.png

  1. <swiper class="swiper-box">
  2. <swiper-item>
  3. <scroll-view scroll-y class="list">
  4. <block v-for="(item,index) in list" :key="index">
  5. <index-list :item="item" :index="index"></index-list>
  6. </block>
  7. </scroll-view>
  8. </swiper-item>
  9. </swiper>

有多少个tab就写多少个list数据
image.png
有几个tab就造几个list对象
image.png

image.png

  1. <swiper class="swiper-box">
  2. <swiper-item v-for="(items,index) in newslist" :key="index">
  3. <scroll-view scroll-y class="list">
  4. <block v-for="(item,index1) in items.list" :key="index1">
  5. <index-list :item="item" :index="index1"></index-list>
  6. </block>
  7. </scroll-view>
  8. </swiper-item>
  9. </swiper>

显示不全,样式有问题。
image.png
官方默认的样式
image.png
外层嵌套一个view
image.png

  1. <view class="uni-tab-bar">
  2. <swiper class="swiper-box">
  3. <swiper-item v-for="(items,index) in newslist" :key="index">
  4. <scroll-view scroll-y class="list">
  5. <block v-for="(item,index1) in items.list" :key="index1">
  6. <index-list :item="item" :index="index1"></index-list>
  7. </block>
  8. </scroll-view>
  9. </swiper-item>
  10. </swiper>
  11. </view>

直接啥也不显示了。肯定就是我们的高度没有起效果。
image.png
整个窗口的可视区域减去tabbar的高度
image.png
搜索到tab的高度
image.png
用uni-app提供的接口获取窗口的高度,减去tab的高度。
先来给box随便设置一个高度试试。
image.png
有效果,但是高度不足,空白部分,高度没有到这里,所以我们要把高度赋值给swiper-box
image.png
设置高度的变量。
image.png

  1. swiperheight:500,

这里为什么用px呢?
image.png

  1. <swiper class="swiper-box" :style="{height: swiperheight+'px'}">

image.png
直接在dom元素上写style的高度智能用px ,不支持upx
image.png
image.png
获取手机的可视窗口https://uniapp.dcloud.io/api/system/info.html#getsysteminfo
image.png

image.png

image.png
复制代码
image.png

  1. uni.getSystemInfo({
  2. success: function (res) {
  3. console.log(res.model);
  4. console.log(res.pixelRatio);
  5. console.log(res.windowWidth);
  6. console.log(res.windowHeight);
  7. console.log(res.language);
  8. console.log(res.version);
  9. console.log(res.platform);
  10. }
  11. });

image.png
改成es6写法,箭头函数
image.png

image.png

image.png

  1. onLoad() {
  2. uni.getSystemInfo({
  3. success: (res) => {
  4. console.log('当前window高度和窗口高度');
  5. console.log(res.windowHeight);
  6. console.log(res.screenHeight);
  7. }
  8. });
  9. },

516才是我们想要的。
image.png
减去tab的高度100
image.png

  1. onLoad() {
  2. uni.getSystemInfo({
  3. success: (res) => {
  4. console.log('当前window高度和窗口高度');
  5. console.log(res.windowHeight);
  6. console.log(res.screenHeight);
  7. let height = res.windowHeight - uni.upx2px(100)
  8. this.swiperheight = height+100;
  9. }
  10. });
  11. },

得到468
image.png
计算出来的高度赋值给变量。
image.png
我本机在H5上测试,这里如果减去了100 最后一条数据无法显示全,所以我在最后又加上了100.这样2条数据正好显示出来了。 带滚动条的。
image.png
这个时候高度就刚刚好了
image.png

tab数据切换

第二个tab也造上数据。还有第三个
image.png
当前选中的
image.png

image.png
滚动了
image.png

image.png
当左右滑动的时候,上面不会变
image.png

增加一个监听事件

image.png

image.png

  1. <swiper class="swiper-box"
  2. :style="{height: swiperheight+'px'}"
  3. :current="tabIndex"
  4. :change="tabChange">

image.png
image.png

image.png

image.png

image.png

image.png

最终效果

向左滑动 切换了tab
image.png

本节最终代码

这里的class=”swiper-box” 我没有嵌套在带uni-tab-bar的view下面,如果嵌套了页面数据会全都不显示。自己在做的时候发现和视频中不一样

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

结束