uni-app實現滑動切換效果

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

 

 

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

tab頂部導航欄小程序

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

<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> 

樣式app

<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.使用組件ui

<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切換的效果,能夠使用任何平臺,已經測試幾個平臺,都沒有問題,

相關文章
相關標籤/搜索