uniapp横向选项卡(水平导航栏)滚动效果

11 篇文章 0 订阅
4 篇文章 0 订阅

可直接复制

<template>
    <view>
        <!-- 顶部导航栏 -->
        <view class="horizonal-tab">
            <scroll-view scroll-x="true" scroll-with-animation :throttle="false" class="scroll-tab" >
                <block v-for="(item,index) in tabBars" :key="index">
                    <view class="scroll-tab-item" :class="{'active': tabIndex==index}" @tap="toggleTab(index)">
                        {{item.name}}
                        <view class="scroll-tab-line"></view>
                    </view>
                </block>
            </scroll-view>
        </view>

        <!-- 内容区 -->
        <view class="content">
            <!-- 滑块视图 -->
            <swiper :current="tabIndex" @change="tabChange">
                <!-- current:当前所在滑块的index -->
                <swiper-item v-for="(content,index) in contentList" :key="index" class="con">
                    <view class="info">
                        <view class="">
                            {{content}}
                        </view>
                    </view>
                </swiper-item>
            </swiper>
        </view>


    </view>
</template>

<script>
    export default {
        data() {
            return {
                scrollAreaLeft:0,
                scrollAreaWidth:0,
                tabItemList:[],
                scrollToLeft: 0,
                tabIndex: 0,
                /* 选中标签栏的序列,默认显示第一个 */
                contentList: [
                    "分类标题",
                    "分类标题",
                    "分类标题",
                    "分类标题",
                    '分类标题',
                    '分类标题',
                    '分类标题'
                ],
                tabBars: [{
                        name: '分类标题',
                        id: '0'
                    },
                    {
                        name: '分类标题',
                        id: '1'
                    },
                    {
                        name: '分类标题',
                        id: '2'
                    },
                    {
                        name: '分类标题',
                        id: '3'
                    },
                    {
                        name: '分类标题',
                        id: '4'
                    },
                    {
                        name: '分类标题',
                        id: '5'
                    },
                    {
                        name: '分类标题',
                        id: '6'
                    }
                ]
            }
        },
        methods: {
            //切换选项卡
            toggleTab(index) {
                this.tabIndex = index;
                this.scrollDom(index);
            },
            //滑动切换swiper
            tabChange(e) {
                // console.log(e);
                this.tabIndex = e.detail.current;
            },
        }
    }
</script>
<style>
    page{
        background-color: #f7f7f7;
    }
</style>
<style>
    .horizonal-tab {}

    .horizonal-tab .active {
        /* color: red; */
        height: 100%;
    }

    .scroll-tab {
        background-color: #fff;
        white-space: nowrap;
        /* 必要,导航栏才能横向*/
        text-align: center;
    }

    .scroll-tab-item {
        height: 45px;
        line-height: 45px;
        display: inline-block;
        /* 必要,导航栏才能横向*/
        margin: 0rpx 30rpx 0 30rpx;
    }

    .active .scroll-tab-line {
        border-bottom: 5rpx solid #30c4cc;
        border-top: 5rpx solid #30c4cc;
        border-radius: 20rpx;
        width: 60%;
        margin: 0 auto;
    }
    .con {
        background-color: #fff;
        height: 300px;
        margin-top: 20px;
        border-radius: 20px;
    }
    .info {
        padding: 20px;
    }
</style>

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是uniapp实现横向选项卡的代码示例: ```vue <template> <view class="tab-control"> <view class="tab-control-top"> <view class="tab-control-item" :class="{ 'active': currentIndex === index }" v-for="(item, index) in titles" :key="index" @click="handleItemClick(index)" > {{ item }} </view> </view> <view class="tab-control-content"> <swiper :current="currentIndex" @change="handleSwiperChange"> <swiper-item v-for="(item, index) in titles" :key="index"> <view class="tab-control-panel">{{ item }} 内容区域</view> </swiper-item> </swiper> </view> </view> </template> <script> export default { data() { return { currentIndex: 0, titles: ['选项卡1', '选项卡2', '选项卡3'] }; }, methods: { handleItemClick(index) { this.currentIndex = index; }, handleSwiperChange(event) { this.currentIndex = event.detail.current; } } }; </script> <style> .tab-control { height: 100%; display: flex; flex-direction: column; } .tab-control-top { display: flex; height: 80rpx; background-color: #fff; box-shadow: 0 2rpx 5rpx rgba(0, 0, 0, 0.1); } .tab-control-item { flex: 1; display: flex; justify-content: center; align-items: center; font-size: 32rpx; color: #666; } .tab-control-item.active { color: #f00; } .tab-control-content { flex: 1; } .tab-control-panel { height: 100%; display: flex; justify-content: center; align-items: center; font-size: 32rpx; color: #666; } </style> ``` 以上代码实现了一个简单的横向选项卡,包括选项卡标题和内容区域。选项卡标题使用flex布局实现,点击标题可以切换内容区域。内容区域使用swiper组件实现,可以左右滑动切换选项卡

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值