說明:本案例的樣式基於colorui組件庫 感興趣的小夥伴能夠看下教程 colorui組件庫開發文檔或者csdn的文檔,順便再分享下 colorui的羣資源css
最近項目中須要用到滑動切換的效果,本身懶得寫就去網上找了下,發現網上的也不靠譜,不是樣式錯亂就是其餘bug,反正到你的電腦上就是各類bug般的存在,恰好趁着過年的時間把這個完整的效果給小夥伴們陳列下個人giteehtml
<template> <view class=""> <scroll-view scroll-x class="nav bg-white" style="scrollStyle"> <view class="flex text-center"> <view class="cu-item flex-sub" :style="scrollItemStyle" @tap="tabtap(index)" :class="tabIndex==index?'text-blue cur':''" v-for="(tab,index) in tabBars" :key="tab.id">{{tab.name}} {{tab.num?tab.num:''}}</view></view> </scroll-view> </view> </template>
<style> .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>
export default { props: { tabBars: Array, tabIndex: Number, scrollStyle: { type: String, default: "" }, scrollItemStyle: { type: String, default: "" } }, methods: { tabtap(index) { //點擊tab時把本身的index傳給父組件index.vue this.$emit('tabtap', index); }, } }
<script>import swiperTab from "../../components/swiper-tab/swiper-tab.vue" export default { components: { swiperTab }, onLoad() { uni.getSystemInfo({ success: (res) = >{ let height = res.windowHeight - uni.upx2px(100) this.swiperheight = height; } }) }, data() { return { tabIndex: 0, // 選中的 tabBars: [{ name: "關注", id: "guanzhu" }, { name: "推薦", id: "tuijian" }, { name: "體育", id: "tiyu" }, { name: "熱點", id: "redian" }, { name: "財經", id: "caijing" }, { name: "娛樂", id: "yule" }, ], swiperheight: 500, //高度 newslist: [{ list: [{ username: "暱稱", title: "我是標題", }, { username: "暱稱", title: "我是標題", }, { username: "暱稱", title: "我是標題", }, { username: "暱稱", title: "我是標題", }, { username: "暱稱", title: "我是標題", }, { username: "暱稱", title: "我是標題", }, { username: "暱稱", title: "我是標題", }, { username: "暱稱", title: "我是標題", }, ] }, { list: [{ username: "暱稱", title: "我是標題", }, ] }, { list: [{ username: "暱稱", title: "我是標題", }, ] }, { list: [{ username: "暱稱", title: "我是標題", }, ] }, { list: [{ username: "暱稱", title: "我是標題", }, ] }, { list: [{ username: "暱稱", title: "我是標題", }, ] }] } }, methods: { tabtap(index) { this.tabIndex = index; }, //滑動切換導航 tabChange(e) { this.tabIndex = e.detail.current; // console.log(this.tabIndex) }, } }</script>
<style> /* 解決不能滾動問題 */ /* uni-swiper .uni-swiper-wrapper, */ uni-swiper-item { overflow-y: scroll !important; } </style>