说明:本案例的样式基于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>