uni-app實現滑動切換效果

  • 2019 年 10 月 11 日
  • 筆記

在對於uni-app框架了解之後,今天就實現一個滾動切換tab效果,這個很常見的一個效果,最後封裝成一個組件,便於以後使用,寫這個需要引入uni官方提供的uni.css樣式,用到了寫好的樣式,就不需要自己寫了

 這種切換無論是在app端還是小程序或者H5頁面都是很常見的功能。對於這種功能,為單獨封裝成功組件,方便每個頁面都能用到,

tab頂部導航欄

頁面布局,使用uni-app提供的scroll-view組件。

<template>      <view class="uni-tab-bar">          <scroll-view class="uni-swiper-tab" scroll-x>              <block v-for="(tab,index) in tabBars" :key="tab.id" :style="scrollStyle">                  <view                      class="swiper-tab-list"                      :class="{'active' : tabIndex==index}"                      @tap="tabtap(index)"                      :style="scrollItemStyle"                  >                      {{tab.name}} {{tab.num?tab.num:""}}                  <view class="swiper-tab-line"></view>                  </view>              </block>          </scroll-view>      </view>  </template>

這個頁面相當於封裝一個組件,便於其他他們調用使用,tabIndex這個是tab內容,tabIndex對應的索引值,表示第幾個。scrollStyle父級樣式設置,scrollItemStyle每一個tab內容樣式設置

<script>  	export default {  		props:{  			tabBars:Array,  			tabIndex:Number,  			scrollStyle:{  				type:String,  				default:""  			},  			scrollItemStyle:{  				type:String,  				default:""  			}  		},  		methods:{  			//點擊切換導航  			tabtap(index){  				// this.tabIndex = index;  				this.$emit('tabtap',index)  			}  		}  	}  </script> 

樣式

<style scoped >  .uni-swiper-tab{  		border-bottom: 1upx solid #EEEEEE;  	}  	.swiper-tab-list{  		color: #969696;  		font-weight: bold;  	}  	.uni-tab-bar .active{  		color: #343434;  	}  	.active .swiper-tab-line{  		border-bottom: 6upx solid #FEDE33;  		width: 70upx;  		margin: auto;  		border-top: 6upx solid #FEDE33;  		border-radius: 20upx;  	}  </style>

tabtap點擊切換效果,自定義一個tabtap事件,傳遞給父級,和vue語法一樣

在父級組件

1.第一步先引入上面封裝好的組件,

import swiperTabHead from "../../components/index/swiper-tab-head.vue";  註冊組件  components:{       swiperTabHead,     }

2.使用組件

<swiperTabHead :tabBars="tabBars" :tabIndex="tabIndex" @tabtap="tabtap"></swiperTabHead>

3.在data定義好數據

 export default {       data(){           tabIndex:0,// 選中的  	tabBars:[            { name:"關注",id:"guanzhu" },  	  { name:"推薦",id:"tuijian" },  	  { name:"體育",id:"tiyu" },  	  { name:"熱點",id:"redian"  },  	  { name:"財經",id:"caijing" },  	  { name:"娛樂",id:"yule"    },         ]      }  }    

4.在方法中使用傳過來的事件

//接受子組件傳過來的值點擊切換導航              tabtap(index){                  this.tabIndex = index;              },

切換內容,直接在父組件寫

<view class="uni-tab-bar">              <swiper class="swiper-box" :style="{height:swiperheight+'px'}" :current="tabIndex" @change="tabChange">                  <swiper-item v-for="(items,index) in newslist" :key="index">                      <scroll-view scroll-y class="list">                          <template v-if="items.list.length > 0">                              <!-- 圖文列表 -->                              <block v-for="(item,index1) in items.list" :key="index1">                                  <view>{{item}}</view>                              </block>                          </template>                      </scroll-view>                  </swiper-item>              </swiper>          </view>

5.也是需要在data定義一下內容,newslist是循環遍歷的tab內容的內容,大概數據結構就是這樣的,swiperheight這個是需要動態計算可視區域內容的高度

swiperheight:500,//高度  newslist:[     {       list:[         數據內天         ]     },     {},     {},     {},     {},     {}  ]

6.在methods方法中寫手動切換的效果

//滑動切換導航  tabChange(e){     this.tabIndex = e.detail.current;  },

 7.動態計算高度,upx2x是吧px轉換成upx,調用這個api,需要在onLoad生命周期寫

onLoad() {     uni.getSystemInfo({  	success:(res)=>{  		let height = res.windowHeight-uni.upx2px(100)  		this.swiperheight = height;  	}      })  },

  以上就是用uni-app實現的一個tab切換的效果,可以使用任何平台,已經測試幾個平台,都沒有問題,