vue Tab切换带动画

<div class="follow_bc">
<div class="tab_two">
                    <div
                        @click="tabClick(item.id, i)"
                        v-for="(item, i) in tabsList"
                        :key="i"
                        ref="label"
                        :class="item.id === groupId ? 'active' : ''"
                        class="second_tab"
                    >
                        {{ item.name }}
                    </div>
                 
                </div>
                <div class="bottom_line" v-if="tabsList.length"></div>
           </div>
            <div v-show="groupId === 0">1内容</div>
            <div v-show="groupId === 1">2内容</div>
            <div v-show="groupId === 2">3内容</div>

以上是Html代码。

以下是css 代码

// tab切换
.follow_bc {
    width: 100%;
    margin-top: 10px;
    position: relative;
}
.first_tab {
    font-size: 14px;
    font-weight: 400;
    color: #666666;
    line-height: 20px;
    cursor: pointer;
}
.bottom_line {
    width: 93px;
    height: 2px;
    background: linear-gradient(137deg, #ff9696 0%, #e55653 100%);
    box-shadow: 0px 1px 4px 0px rgba(215, 6, 3, 0.19);
    border-radius: 1px;
    border: 0px solid rgba(151, 151, 151, 0);
    position: absolute;
    margin-top: -9px;
    margin-left: 1px;
   
}
.second_tab {
    font-size: 14px;
    font-weight: 400;
    color: #666666;
    line-height: 20px;
    cursor: pointer;
    width: 93px;
    display: flex;
    justify-content: center;
}



.tab_two {
    display: flex;
    padding: 20px 0px;
   
}
.tab_two div.active {
    color: #333;
    font-size: 20px;
    letter-spacing: 1px;
}
//tab切换

以下是js部分

data() {
        return {
           
            tabsList: [
                { name: 'tab1', id: 0 },
                { name: 'tab2', id: 1 },
                { name: 'tab3', id: 2 },
            ], //tabs的数组
            groupId: 0,
        };
    },



 //tab切换
        tabClick(id, i) {
            this.groupId = id;
            let doc = document.getElementsByClassName('bottom_line')[0];
            let left = this.$refs.label[i].offsetLeft + 'px'; //返回当前元素的相对水平偏移位置的偏移容器
           let width = this.$refs.label[i].clientWidth + 'px'; //	在页面上返回内容的可视宽度(不包括边框,边距或滚动条)
            doc.style.width = `${width}`;
            doc.style.transform = `translateX(${left})`;
            doc.style.transition = '.3s';
        },

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要优化uni-app的tab切换折叠效果,可以考虑使用动画库或自定义动画来实现更加流畅和灵活的效果。下面是一个示例代码,使用了`animate.css`动画库和自定义过渡效果来优化tab切换的折叠效果: 首先,安装`animate.css`库,可以通过npm或者yarn进行安装: ```bash npm install animate.css ``` 然后,在uni-app的项目中创建一个新的组件,比如`TabCollapse.vue`: ```html <template> <view class="container"> <view class="tab-bar"> <view class="tab-item" :class="{ active: activeTab === 'tab1' }" @click="toggleTab('tab1')" > Tab 1 </view> <view class="tab-item" :class="{ active: activeTab === 'tab2' }" @click="toggleTab('tab2')" > Tab 2 </view> </view> <transition-group name="fade"> <view class="content" v-for="tab in tabs" :key="tab.name" v-show="activeTab === tab.name" > {{ tab.content }} </view> </transition-group> </view> </template> <script> import 'animate.css'; export default { data() { return { activeTab: 'tab1', tabs: [ { name: 'tab1', content: 'Content 1', }, { name: 'tab2', content: 'Content 2', }, ], }; }, methods: { toggleTab(tab) { if (this.activeTab === tab) { this.activeTab = ''; // 折叠当前展开的tab } else { this.activeTab = tab; // 切换到选中的tab } }, }, }; </script> <style> .container { display: flex; flex-direction: column; height: 100vh; } .tab-bar { display: flex; justify-content: space-between; padding: 16px; background-color: #f0f0f0; } .tab-item { padding: 8px 16px; border-radius: 4px; cursor: pointer; } .tab-item.active { background-color: #e0e0e0; } .content { padding: 16px; animation-duration: 0.3s; } .fade-enter-active, .fade-leave-active { animation-fill-mode: both; animation-timing-function: ease-in-out; } .fade-enter, .fade-leave-to { animation-duration: 0.3s; animation-delay: 0.1s; } .fade-enter { animation-name: fadeInDown; } .fade-leave-to { animation-name: fadeOutUp; } </style> ``` 在上面的代码中,我们使用了`animate.css`库来提供丰富的动画效果,通过添加过渡类名和自定义动画,使tab切换效果更加流畅和优雅。在`<transition-group>`组件中使用`v-for`指令遍历tabs数组,并使用`v-show`指令根据activeTab的值来控制显示和隐藏。 然后,在`<style>`标签中定义了过渡的动画效果。你可以根据需要选择合适的动画效果,也可以自定义自己的动画。 希望这个优化方案对你有帮助!如果有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值