当前位置:   article > 正文

uni tabs组件,tab选项卡滑动切换

uni tabs

效果展示:

效果描述:

1.tabs点击和左右切换滑动,

2.uni框架,思路是找寻存在的tab组件,发现uni组件库没有

参考链接:uniapp实现tab选项卡 

核心思路:

技术背景:

uni框架,基于vue2语法

完整代码:

  1. <template>
  2. <view class="content">
  3. <view class="head">
  4. <view class="my_information">
  5. <view class="name">
  6. <view class="toBack"></view>
  7. <view class="title_name">设备状态监控</view>
  8. <view class="ship_change">
  9. <uni-data-select v-model="shipGuid" :localdata="shipList" @change="changeShip"
  10. placeholder="请选择船舶"></uni-data-select>
  11. </view>
  12. </view>
  13. <scroll-view class="scroll-view_H" scroll-x="true" scroll-with-animation>
  14. <view class="modules_titles">
  15. <view class="lists" v-for="(item, index) in modelList" :key="index"
  16. :class="currentIndex == index ? 'module_active' : ''" @click="changeModules(index)">
  17. {{ item.name }}
  18. </view>
  19. </view>
  20. </scroll-view>
  21. </view>
  22. </view>
  23. <!-- <view class="modules_content">
  24. <view class="modules_components">
  25. <component :is="currentComponents"></component>
  26. </view>
  27. </view> -->
  28. <!-- 选项卡内容轮播滑动显示,current为当前第几个swiper子项 -->
  29. <swiper @change="change" :current="isActive" class="modules_content">
  30. <swiper-item class="swiperitem-content">
  31. <scroll-view scroll-y style="height: 100%;">
  32. <view class="nav_item">
  33. <block>
  34. <view class="echart_box">
  35. <pieChart :option="hostOpt1" echartsId="IndexA" height="300rpx"></pieChart>
  36. </view>
  37. <view class="echart_box"
  38. style="width: 100%;display: flex;justify-content: space-evenly;align-items: center;">
  39. <view class="termp_home">
  40. <view class="title">左滑油温度</view>
  41. <pieChart :option="hostOpt2" echartsId="IndexB" height="300rpx"></pieChart>
  42. </view>
  43. <view class="termp_home">
  44. <view class="title">右滑油温度</view>
  45. <pieChart :option="hostOpt22" echartsId="Indexb" height="300rpx"></pieChart>
  46. </view>
  47. </view>
  48. <view class="echart_box" style="margin-top: 30rpx;">
  49. <pieChart :option="hostOpt3" echartsId="IndexC" height="300rpx"></pieChart>
  50. </view>
  51. </block>
  52. </view>
  53. </scroll-view>
  54. </swiper-item>
  55. <swiper-item class="swiperitem-content">
  56. <scroll-view scroll-y style="height: 100%;">
  57. <view class="nav_item">
  58. <block>
  59. <view class="echart_box"
  60. style="width: 100%;display: flex;justify-content: space-evenly;align-items: center;">
  61. <view class="termp_home">
  62. <view class="title">左淡水排温温度</view>
  63. <pieChart :option="hostOpt2" echartsId="IndexD" height="300rpx"></pieChart>
  64. </view>
  65. <view class="termp_home">
  66. <view class="title">右淡水排温温度</view>
  67. <pieChart :option="hostOpt22" echartsId="Indexd" height="300rpx"></pieChart>
  68. </view>
  69. </view>
  70. <view class="echart_box"
  71. style="width: 100%;display: flex;justify-content: space-evenly;align-items: center;">
  72. <view class="termp_home">
  73. <view class="title">左淡水进温温度</view>
  74. <pieChart :option="hostOpt22" echartsId="IndexE" height="300rpx"></pieChart>
  75. </view>
  76. <view class="termp_home">
  77. <view class="title">右淡水进温温度</view>
  78. <pieChart :option="hostOpt22" echartsId="Indexe" height="300rpx"></pieChart>
  79. </view>
  80. </view>
  81. <view class="echart_box"
  82. style="width: 100%;display: flex;justify-content: space-evenly;align-items: center;">
  83. <view class="termp_home">
  84. <view class="title">左排烟管排气温度</view>
  85. <view class="container">
  86. <block v-for="(item, index) in 2" :key="index">
  87. <view class="item">
  88. <div class="item_xuhao">#{{ index + 1 }}</div>
  89. <div class="item_right">
  90. <div class="num">50.5</div>
  91. <div class="unit">゜C</div>
  92. </div>
  93. </view>
  94. </block>
  95. </view>
  96. </view>
  97. <view class="termp_home">
  98. <view class="title">右排烟管排气温度</view>
  99. <view class="container">
  100. <block v-for="(item, index) in 2" :key="index">
  101. <view class="item">
  102. <div class="item_xuhao">#{{ index + 1 }}</div>
  103. <div class="item_right">
  104. <div class="num">50.5</div>
  105. <div class="unit">゜C</div>
  106. </div>
  107. </view>
  108. </block>
  109. </view>
  110. </view>
  111. </view>
  112. </block>
  113. </view>
  114. </scroll-view>
  115. </swiper-item>
  116. <swiper-item class="swiperitem-content">
  117. <scroll-view scroll-y style="height: 100%;">
  118. <view class="nav_item">
  119. <block>
  120. <view class="echart_box">
  121. <pieChart :option="altOpt1" echartsId="AltA" height="300rpx"></pieChart>
  122. </view>
  123. <view class="echart_box"
  124. style="width: 100%;display: flex;justify-content: space-evenly;align-items: center;">
  125. <view class="termp_home">
  126. <view class="title">左排温温度</view>
  127. <pieChart :option="altOpt2" echartsId="AltB" height="300rpx"></pieChart>
  128. </view>
  129. <view class="termp_home">
  130. <view class="title">右排温温度</view>
  131. <pieChart :option="altOpt22" echartsId="Altb" height="300rpx"></pieChart>
  132. </view>
  133. </view>
  134. <view class="echart_box" style="margin-top: 30rpx;">
  135. <pieChart :option="altOpt3" echartsId="AltC" height="300rpx"></pieChart>
  136. </view>
  137. </block>
  138. </view>
  139. </scroll-view>
  140. </swiper-item>
  141. <swiper-item class="swiperitem-content">
  142. <scroll-view scroll-y style="height: 100%;">
  143. <view class="nav_item">
  144. <block>
  145. <view class="echart_box">
  146. <pieChart :option="rudOpt1" echartsId="RudderA" height="300rpx"></pieChart>
  147. </view>
  148. <view class="echart_box"
  149. style="width: 100%;display: flex;justify-content: space-evenly;align-items: center;">
  150. <view class="termp_home">
  151. <view class="title">左液压油温度</view>
  152. <pieChart :option="rudOpt2" echartsId="RudderB" height="300rpx"></pieChart>
  153. </view>
  154. <view class="termp_home">
  155. <view class="title">右液压油温度</view>
  156. <pieChart :option="rudOpt22" echartsId="Rudderb" height="300rpx"></pieChart>
  157. </view>
  158. </view>
  159. <view class="echart_box" style="margin-top: 30rpx;">
  160. <pieChart :option="rudOpt3" echartsId="RudderC" height="300rpx"></pieChart>
  161. </view>
  162. </block>
  163. </view>
  164. </scroll-view>
  165. </swiper-item>
  166. <swiper-item class="swiperitem-content">
  167. <scroll-view scroll-y style="height: 100%;">
  168. <view class="nav_item">
  169. <block>
  170. <view class="echart_box">
  171. <pieChart :option="rudOpt4" echartsId="RudderD" height="300rpx"></pieChart>
  172. </view>
  173. <view class="echart_box"
  174. style="width: 100%;display: flex;justify-content: space-evenly;align-items: center;">
  175. <view class="termp_home">
  176. <view class="title">左滑油温度</view>
  177. <pieChart :option="rudOpt5" echartsId="RudderE" height="300rpx"></pieChart>
  178. </view>
  179. <view class="termp_home">
  180. <view class="title">右滑油温度</view>
  181. <pieChart :option="rudOpt55" echartsId="Ruddere" height="300rpx"></pieChart>
  182. </view>
  183. </view>
  184. <view class="echart_box" style="margin-top: 30rpx;">
  185. <pieChart :option="rudOpt6" echartsId="RudderF" height="300rpx"></pieChart>
  186. </view>
  187. </block>
  188. </view>
  189. </scroll-view>
  190. </swiper-item>
  191. <!-- <swiper-item class="swiperitem-content">
  192. <scroll-view scroll-y style="height: 100%;">
  193. <view class="nav_item">
  194. 另一个页面
  195. </view>
  196. </scroll-view>
  197. </swiper-item> -->
  198. </swiper>
  199. </view>
  200. </template>
  201. <script>
  202. import host from "@/components/IndexComponents/host.vue"
  203. import alternator from "@/components/IndexComponents/alternator.vue"
  204. import rudder from "@/components/IndexComponents/rudder.vue"
  205. import store from '@/store/index.js'
  206. import pieChart from '@/components/graph/pieChart.vue';
  207. import { pieSpeed, tempBar } from "@/utils/chart.js"
  208. export default {
  209. components: {
  210. host,
  211. alternator,
  212. rudder,
  213. pieChart,
  214. },
  215. data() {
  216. return {
  217. currentIndex: 0,//选中的设备类型
  218. isActive: 0,//选中的swiper
  219. // currentComponents: "host",
  220. modelList: [
  221. {
  222. name: "主机状态",
  223. components: "host",
  224. },
  225. {
  226. name: "发电机状态",
  227. components: "alternator",
  228. },
  229. {
  230. name: "舵桨轴系",
  231. components: "rudder",
  232. },
  233. ],
  234. shipGuid: "",
  235. shipList: [],
  236. //主机
  237. hostOpt1: {},
  238. hostOpt2: {},
  239. hostOpt22: {},
  240. hostOpt3: {},
  241. //发电机
  242. altOpt1: {},
  243. altOpt2: {},
  244. altOpt22: {},
  245. altOpt3: {},
  246. //舵桨轴系
  247. rudOpt1: {},
  248. rudOpt2: {},
  249. rudOpt22: {},
  250. rudOpt3: {},
  251. rudOpt4: {},
  252. rudOpt5: {},
  253. rudOpt55: {},
  254. rudOpt6: {},
  255. }
  256. },
  257. created() {
  258. this.getApiData()
  259. },
  260. methods: {
  261. // 当前点击子元素靠左留一个选项展示,子元素宽度不相同也可实现
  262. changeModules(index) {
  263. // this.currentIndex = index;
  264. // this.currentComponents = this.modelList[index].components
  265. switch (index) {
  266. case 0:
  267. this.isActive = 0
  268. break;
  269. case 1:
  270. this.isActive = 2
  271. break;
  272. case 2:
  273. this.isActive = 3
  274. break;
  275. default:
  276. this.isActive = 0
  277. break;
  278. }
  279. },
  280. changeShip() {
  281. console.log(11, this.shipGuid)
  282. this.$store.commit('setShipGuids', this.shipGuid);
  283. },
  284. // swiper滑动时,获取其索引,也就是第几个
  285. change(e) {
  286. const { current } = e.detail;
  287. this.isActive = current;
  288. },
  289. getApiData() {
  290. console.log('api parseInt(1000/10)>>>', parseInt(3000 / 10));
  291. let host1 = [
  292. { title: '左主机转速', name: 'r/min', value: 890, unit: '', pos: ['25%', '50%'], rangeMax: 1200 },
  293. { title: '右主机转速', name: 'r/min', value: 890, unit: '', pos: ['75%', '50%'], rangeMax: 1200 },
  294. ]
  295. let host3 = [
  296. { title: '左增压器转速', name: 'r/min', value: 890, unit: '', pos: ['25%', '50%'], rangeMax: 1200 },
  297. { title: '右增压器转速', name: 'r/min', value: 890, unit: '', pos: ['75%', '50%'], rangeMax: 1200 },
  298. ]
  299. let luProps = {
  300. title: '31.2゜C',
  301. TP_max: 80,
  302. TP_min: 20,
  303. TP_value: 30,
  304. kdtop: 135,
  305. grids: {
  306. xgrid: "40%",
  307. chartGrid: -17,
  308. kdGrid: -7,
  309. }
  310. }
  311. this.hostOpt1 = pieSpeed(host1)
  312. this.hostOpt2 = tempBar(luProps)
  313. this.hostOpt22 = tempBar(luProps)
  314. this.hostOpt3 = pieSpeed(host3)
  315. let alt1 = [
  316. { title: '左发电机转速', name: 'r/min', value: 890, unit: '', pos: ['25%', '50%'], rangeMax: 1200 },
  317. { title: '右发电机转速', name: 'r/min', value: 890, unit: '', pos: ['75%', '50%'], rangeMax: 1200 },
  318. ]
  319. let alt2 = [
  320. { title: '左启动空气压力', name: 'MPa', value: 0.60, unit: '', pos: ['25%', '50%'], rangeMax: 1 },
  321. { title: '右启动空气压力', name: 'MPa', value: 0.65, unit: '', pos: ['75%', '50%'], rangeMax: 1 },
  322. ]
  323. this.altOpt1 = pieSpeed(alt1)
  324. this.altOpt2 = tempBar(luProps)
  325. this.altOpt22 = tempBar(luProps)
  326. this.altOpt3 = pieSpeed(alt2)
  327. let rud1 = [
  328. { title: '左桨角', name: 'r/min', value: 890, unit: '', pos: ['25%', '50%'], rangeMax: 1200 },
  329. { title: '右桨角', name: 'r/min', value: 890, unit: '', pos: ['75%', '50%'], rangeMax: 1200 },
  330. ]
  331. let rud3 = [
  332. { title: '左舵桨转速', name: 'r/min', value: 890, unit: '', pos: ['25%', '50%'], rangeMax: 1200 },
  333. { title: '右舵桨转速', name: 'r/min', value: 890, unit: '', pos: ['75%', '50%'], rangeMax: 1200 },
  334. ]
  335. this.rudOpt1 = pieSpeed(rud1)
  336. this.rudOpt2 = tempBar(luProps)
  337. this.rudOpt22 = tempBar(luProps)
  338. this.rudOpt3 = pieSpeed(rud3)
  339. let rud4 = [
  340. { title: '左舵桨中间轴转速', name: 'r/min', value: 890, unit: '', pos: ['25%', '50%'], rangeMax: 1200 },
  341. { title: '右舵桨中间轴转速', name: 'r/min', value: 890, unit: '', pos: ['75%', '50%'], rangeMax: 1200 },
  342. ]
  343. let rud6 = [
  344. { title: '左滑油压力', name: 'r/min', value: 0.62, unit: '', pos: ['25%', '50%'], rangeMax: 1 },
  345. { title: '右滑油压力', name: 'r/min', value: 0.62, unit: '', pos: ['75%', '50%'], rangeMax: 1 },
  346. ]
  347. this.rudOpt4 = pieSpeed(rud1)
  348. this.rudOpt5 = tempBar(luProps)
  349. this.rudOpt55 = tempBar(luProps)
  350. this.rudOpt6 = pieSpeed(rud3)
  351. },
  352. },
  353. mounted() {
  354. this.shipGuid = store.state.shipGuids
  355. this.shipList = store.state.shipLists
  356. },
  357. watch: {
  358. // swiper与上面选项卡联动
  359. isActive(newval) {
  360. // this.currentIndex = newval;
  361. switch (newval) {
  362. case 0:
  363. this.currentIndex = 0
  364. break;
  365. case 1:
  366. this.currentIndex = 0
  367. break;
  368. case 2:
  369. this.currentIndex = 1
  370. break;
  371. case 3:
  372. this.currentIndex = 2
  373. break;
  374. case 4:
  375. this.currentIndex = 2
  376. break;
  377. default:
  378. this.currentIndex = 0
  379. break;
  380. }
  381. },
  382. },
  383. }
  384. </script>
  385. <style lang="scss" scoped>
  386. page {
  387. height: 100%;
  388. display: flex;
  389. background-color: #FFFFFF;
  390. }
  391. .content {
  392. width: 100%;
  393. // height: calc(100vh - 50px);
  394. flex: 1;
  395. // background-image: url("@/static/head_bg.png");
  396. // background-size: 100% 388rpx;
  397. // background-repeat: no-repeat;
  398. display: flex;
  399. flex-direction: column;
  400. .head {
  401. width: 100%;
  402. height: 230rpx;
  403. // height: 21%;
  404. padding: 0 40rpx 0;
  405. color: #FFFFFF;
  406. box-sizing: border-box;
  407. background-image: url("@/static/head_bg.png");
  408. background-size: 100% 388rpx;
  409. background-repeat: no-repeat;
  410. .my_information {
  411. width: 100%;
  412. height: 230rpx;
  413. font-size: 36rpx;
  414. text-align: center;
  415. padding-top: 50rpx;
  416. left: 0;
  417. background: none;
  418. z-index: 200;
  419. .name {
  420. width: 100%;
  421. display: flex;
  422. justify-content: space-between;
  423. align-items: center;
  424. .ship_change {
  425. width: 200rpx;
  426. /deep/.uni-select {
  427. border: none;
  428. .uni-select__input-placeholder {
  429. color: #fff;
  430. }
  431. .uni-icons::before {
  432. // display: none;
  433. color: #fff;
  434. }
  435. .uni-select__selector-item {
  436. color: #000;
  437. }
  438. .uni-select__input-text {
  439. color: #fff;
  440. }
  441. .uni-select__selector {
  442. z-index: 1000;
  443. }
  444. }
  445. }
  446. .toBack {
  447. width: 200rpx;
  448. }
  449. }
  450. .scroll-view_H {
  451. white-space: nowrap;
  452. width: 100%;
  453. }
  454. .modules_titles {
  455. display: flex;
  456. justify-content: space-between;
  457. margin-top: 30rpx;
  458. font-weight: 500;
  459. font-size: 28rpx;
  460. .lists {
  461. width: 172rpx;
  462. height: 60rpx;
  463. line-height: 60rpx;
  464. text-align: center;
  465. }
  466. .module_active {
  467. background: #FFFFFF;
  468. border-radius: 98rpx;
  469. color: #0C274F;
  470. }
  471. }
  472. }
  473. }
  474. .modules_content {
  475. width: 100%;
  476. flex: 1;
  477. .modules_components {
  478. height: 100%;
  479. background: #FFFFFF;
  480. min-height: 1000rpx;
  481. padding: 38rpx 20rpx 0rpx;
  482. box-sizing: border-box;
  483. }
  484. .swiperitem-content {
  485. background-color: #ffffff;
  486. .nav_item {
  487. background-color: #FFFFFF;
  488. padding: 20rpx 40rpx 0rpx 40rpx;
  489. height: 100%;
  490. display: flex;
  491. flex-direction: column;
  492. justify-content: space-around;
  493. align-items: center;
  494. }
  495. }
  496. }
  497. }
  498. </style>

写在最后,实现思路总结:

uni组件库没有tabs选项卡组件,只有自己封装,实现的思路:
考虑分为上下两块,上部是类型切换绑定点击事件,下部之前用动态组件,下部改为swiper组件,并绑定change事件。
swiper组件的属性current绑定第几个swiper-item子项,并且watch监听绑定值,对这个绑定值处理和上部的类型对应上(也可不处理)。

不处理的情况是:一个swiper-item对应一个tabs上部的类型选项。 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/312410
推荐阅读
相关标签
  

闽ICP备14008679号