uni-app 顶部选项卡吸附效果

1、页面滚动到该导航位置吸顶

2、tab部分可滑动切换页面

html部分

banner图

 <view class="ding">banner</view>

tab选项卡、滑动

<view :class="{'st':true,'sticky-fixed':isF}">
			<!-- tab部分 -->
			<swiper class="ct_tab">
				<swiper-item  :class="{ 'ct_active': index == tabCur }" v-for="(item, index) in tabList" :key="index" class="ct_item"  @click="clickCtTab(index)">
					<text  v-text="item.title"></text>
				</swiper-item>
			</swiper>
		</view>

展示内容部分,根据选项卡下标进行展示

<view class="xiala">
			<!-- 内容信息 -->
			<view v-if="tabCur===0">
				<view>全部信息</view>
				<view>111111111111111111111111111</view>
				<view>222222222222222222222222222</view>
				<view>333333333333333333333333333</view>
			</view>
			<view v-if="tabCur===1">
				<view>水果</view>
				<view>111111111111111111111111111</view>
				<view>222222222222222222222222222</view>
				<view>333333333333333333333333333</view>
			</view>
			<view v-if="tabCur===2">
				<view>蔬菜</view>
			</view>
			<view v-if="tabCur===3">
				<view>调味品</view>
			</view>
			<view v-if="tabCur===4">
				<view>肉类</view>
			</view>
			<view v-if="tabCur===5">
				<view>油类</view>
			</view>
			<view v-if="tabCur===6">
				<view>米类</view>
			</view>
			<view v-if="tabCur===7">
				<view>海鲜</view>
			</view>
					
		</view>

JavaScript部分

注意事项:动态绑定的 style 不支持直接使用 upx,使用时需要使用 uni.upx2px(Number) 转换为 px 后再赋值

<script>
    export default {
        data() {
            return {
				// 下拉固定
                yuanH: uni.upx2px(200),
                isF: false,
				// 滑动tab
				tabCur:0,
				tabList:[
					{
						title:'全部',
					},{
						title:'水果',
					},{
						title:'蔬菜',
					},{
						title:'调味品',
					},{
						title:'肉类',
					},{
						title:'油类',
					},{
						title:'米类',
					},{
						title:'海鲜',
					}
				],
				
            };
        },
        onPageScroll(e) {
            //#ifdef H5
            this.isF = true
            // #endif
            // #ifndef H5
            if (this.yuanH > e.scrollTop) {
                this.isF = false
            } else {
                this.isF = true
            }
            // #endif
        },
		methods:{
			clickCtTab(ctCur){
				this.tabCur = ctCur
				console.log('点击了--->'+this.tabCur)
			}
		},
		
    };
</script>

CSS部分

<style lang='scss' scoped>
	/* 顶部 banner */
    .ding {
        height: 200rpx;
        background-color: aquamarine;
    }

    .st {
        height: 90rpx;
        width: 750rpx;
        background-color: #fff;
        z-index: 999;
        /* top: 300upx; */
    }
    .sticky-fixed {
        /* #ifdef H5 */
        position: sticky;
        top: 44px;
        /* #endif */
        /* #ifndef H5 */
        position: fixed;
        top: 0;
        /* #endif */
        z-index: 999;
    }
    .xiala {
        height: 1500px;
        background-color: #eee;
        padding-top: 100rpx;
    }
	
	
	/* 滑动tab */
	.ct_tab{
		width: 698upx;
		height: 90rpx;
		margin: 0 auto;
		/* padding: 30upx 0; */
		font-size: 28upx;
		/* font-weight: bold; */
		color: #c0c8d0;
		white-space: nowrap;
		display: flex;
		overflow: hidden;
	}
	.ct_item{
		width: 150upx;    
		padding: 20upx 0;
		display: inline-block;
	}
	.ct_item text{
		padding: 20upx 0;
	}
	.ct_active{
		color:#007AFF;
		font-weight: bold;
	}
	.ct_active text{
		border-bottom: 2px solid #007AFF;
	}
	swiper{
		width:100%;
	}
	
	swiper-item{
		width: 150upx !important;
	}
	
</style>

完整代码

<template>
    <view>
		<!-- 顶部banner图 -->
        <view class="ding">banner</view>
		
		<!-- tab 滑动 -->
        <view :class="{'st':true,'sticky-fixed':isF}">
			<!-- tab部分 -->
			<swiper class="ct_tab">
				<swiper-item  :class="{ 'ct_active': index == tabCur }" v-for="(item, index) in tabList" :key="index" class="ct_item"  @click="clickCtTab(index)">
					<text  v-text="item.title"></text>
				</swiper-item>
			</swiper>
		</view>
		
		<!-- 内容 -->
        <view class="xiala">
			<!-- 内容信息 -->
			<view v-if="tabCur===0">
				<view>全部信息</view>
				<view>111111111111111111111111111</view>
				<view>222222222222222222222222222</view>
				<view>333333333333333333333333333</view>
			</view>
			<view v-if="tabCur===1">
				<view>水果</view>
				<view>111111111111111111111111111</view>
				<view>222222222222222222222222222</view>
				<view>333333333333333333333333333</view>
			</view>
			<view v-if="tabCur===2">
				<view>蔬菜</view>
			</view>
			<view v-if="tabCur===3">
				<view>调味品</view>
			</view>
			<view v-if="tabCur===4">
				<view>肉类</view>
			</view>
			<view v-if="tabCur===5">
				<view>油类</view>
			</view>
			<view v-if="tabCur===6">
				<view>米类</view>
			</view>
			<view v-if="tabCur===7">
				<view>海鲜</view>
			</view>
					
		</view>
		
    </view>
</template>

<script>
    export default {
        data() {
            return {
				// 下拉固定
                yuanH: uni.upx2px(200),
                isF: false,
				// 滑动tab
				tabCur:0,
				tabList:[
					{
						title:'全部',
					},{
						title:'水果',
					},{
						title:'蔬菜',
					},{
						title:'调味品',
					},{
						title:'肉类',
					},{
						title:'油类',
					},{
						title:'米类',
					},{
						title:'海鲜',
					}
				],
				
            };
        },
        onPageScroll(e) {
            //#ifdef H5
            this.isF = true
            // #endif
            // #ifndef H5
            if (this.yuanH > e.scrollTop) {
                this.isF = false
            } else {
                this.isF = true
            }
            // #endif
        },
		methods:{
			clickCtTab(ctCur){
				this.tabCur = ctCur
				console.log('点击了--->'+this.tabCur)
			}
		},
		
    };
</script>
<style lang='scss' scoped>
	/* 顶部 banner */
    .ding {
        height: 200rpx;
        background-color: aquamarine;
    }

    .st {
        height: 90rpx;
        width: 750rpx;
        background-color: #fff;
        z-index: 999;
        /* top: 300upx; */
    }
    .sticky-fixed {
        /* #ifdef H5 */
        position: sticky;
        top: 44px;
        /* #endif */
        /* #ifndef H5 */
        position: fixed;
        top: 0;
        /* #endif */
        z-index: 999;
    }
    .xiala {
        height: 1500px;
        background-color: #eee;
        padding-top: 100rpx;
    }
	
	
	/* 滑动tab */
	.ct_tab{
		width: 698upx;
		height: 90rpx;
		margin: 0 auto;
		/* padding: 30upx 0; */
		font-size: 28upx;
		/* font-weight: bold; */
		color: #c0c8d0;
		white-space: nowrap;
		display: flex;
		overflow: hidden;
	}
	.ct_item{
		width: 150upx;    
		padding: 20upx 0;
		display: inline-block;
	}
	.ct_item text{
		padding: 20upx 0;
	}
	.ct_active{
		color:#007AFF;
		font-weight: bold;
	}
	.ct_active text{
		border-bottom: 2px solid #007AFF;
	}
	swiper{
		width:100%;
	}
	
	swiper-item{
		width: 150upx !important;
	}
	
</style>

写得不好请多多包涵(狗头保命)>_<

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值