小程序 - tab左右切换效果

分析

1、设置data-current属性用于:点击当前项时,通过点击事件swichNav中处理e.dataset.current取到点击的目标值。
2、swiper组件的current组件用于控制当前显示哪一页
3、swiper组件绑定change事件switchTab,通过e.detail.current拿到当前页
wxml:
<view class="record-box">
    <view class="nav">
        <scroll-view scroll-x="true" class="navbar-box" srcoll-left="{{navScrollLeft}}"
            srcoll-width-animation="{{true}}">
            <block wx:for="{{recordMain}}" wx:for-index="idx" wx:for-item="navItem" wx:key="idx">
                <view class="nav-item {{currentTab == idx ? 'text' : ''}}" data-current="{{idx}}" 
                    bindtap="switchNav">
                    <label>{{navItem.text}}</label>
                </view>
            </block>
        </scroll-view>
    </view>
</view>
---------------------------------------------------------------------------------------------
<view class="record-ft">
    <swiper class="tab-box" current="{{currentTab}}" duration="300" bindchange="switchTab">
        <swiper-item wx:for="{{[0,1,2,3]}}" wx:for-item="tabItem" wx:for-index="idx" wx:key="idx"
             class="tab-cnetent">
                <block wx:for="{{tabContent}}" wx:for-item="tabItem" wx:key=" ">
                    <view class="padding-box" bindtap="navigateUrl" data-url="/pages/index/index">
                        <view class="weui-list-box weui-main">
                            <view class="weui-list-bd">
                                <view class="img-box">
                                    <image src="{{tabItem}}" class="img" mode="widthFix"></image>
                                </view>
                            </view>
                            <view class="weui-list-ft">
                                <view class="weui-title">
                                    <view class="text-flow clamp-xs font-sm">{{tabItem.title}}</view>
                                    <view class="text">
                                        <laber>{{tabItem.text}}</laber>
                                        <laber>{{tabItem.text}}</laber>
                                    </view>
                                </view>
                                <view class="weui-cost">
                                    <view><laber>{{tanItem.cost}}</laber></view>
                                </view>
                            </view>
                        </view>
                    </view>
                </block>
        </swiper-item>
    </swiper>
</view>
JS
Page ({
    const app = getApp()
    data: {
        recordMian: [
            {
                title: "插画艺术"
            },
            {
                title: "工艺作品"
            },
            {
                title: "服装艺术"
            },
            {
                title: "三维建模"
            },
        ],
        tabContent: [
            {
              title: "台灯卧室床头 简约现代书房 北欧宜家创意装饰个性圆球台灯床头灯",
              text: "台灯",
              cost: "1255",
              imgUrl: "http://img.weiye.me/zcimgdir/album/file_59b8f7b66ba7f.jpg",
            },
            {
              title: "雪域冰雪天地8寸牛乳芝士蛋糕",
              text: "蛋糕",
              cost: "15",
              imgUrl: "http://img.weiye.me/zcimgdir/album/file_59b8f7b66ba7f.jpg",
            },
        ],
        currentTab: 0,
        navScrollLeft: 0
    },
    // 事件处理函数
    onLoad: function() {
        // 控制record-box随鼠标切换调整位置
        if (app.globalData.userInfo) {
            this.setData({
                userInfo: app.globalData.userInfo,
                hasUserInfo: true
            })
         }  else if (this.data.canIUse) {
                app.userInfoReadyCallback = res => {
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            }
        else {
            wx.getUserInfo({
                success: res => {
                    app.globalData.userInfo = res.userInfo
                    this.setData({
                        userInfo: res.userInfo,
                        hasUserInfo: true
                    })
                }
            })
        }
        wx.getSystemInfo({
            success: (res) => {
                this.setData({
                    pixelRatio: res.pixelRatio,
                    windowHeight: res.windowHeight,
                    windowWidth: res.windowWidth
                })
            }
        })
    },
    // 滑动事件
    // 点击标题切换当前页时改变样式
    switchNav(event) {
        var cur = event.currentTarget.dataset.current;
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }
    },
    // 滚动切换标签样式 
    switchTab(event) {
        var cur = evnet.detail.current;
        var singeNavWidth = this.data.windowWidth / 5;
        this.setData({
            currentTab: cur,
            navScrollLeft: (cur - 2) * singleNavWidth
        });
    }
})

这里写图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
微信小程序中的tab切换下划线滑动效果是指在用户切换不同tab(标签)时,底部的指示线会随着当前选中的tab移动,产生滑动效果。这样的效果通常用于给用户提供视觉上的反馈,表明当前所在的页面或选项。 要实现这种效果,可以通过微信小程序提供的组件和API来完成。具体来说,可以通过以下步骤实现: 1. 使用`<view>`标签创建tab栏,并为每个tab设置相应的数据绑定和点击事件处理函数。 2. 使用`wx.createAnimation`创建一个动画实例,并通过动画方法设置下划线的移动效果。 3. 在tab点击事件的回调函数中,根据当前选中的tab位置,更新下划线的样式,使下划线移动到相应的位置。 这里是一个简化的代码示例: ```javascript Page({ data: { activeTab: 0, tabWidth: 250, // 假设每个tab的宽度是250px tabHeight: 40 // 假设下划线的高度是40px }, changeTab(e) { const newActiveTab = e.detail.index; this.setData({ activeTab: newActiveTab }); this.updateUnderline(newActiveTab); }, updateUnderline(index) { const animation = wx.createAnimation({ duration: 300, timingFunction: 'ease', }); const x = index * this.data.tabWidth; animation.move({ x: x, y: 0, duration: 300, timingFunction: 'ease', }).step(); this.setData({ underlineStyle: animation.export() }); } }); ``` 在wxml中定义tab栏和下划线: ```xml <view class="tab-bar"> <block wx:for="{{tabs}}" wx:key="unique"> <view class="tab-item" bindtap="changeTab" data-index="{{index}}" wx:if="{{index === activeTab}}"> {{item.title}} </view> </block> <view class="tab-underline" style="{{underlineStyle}}"></view> </view> ``` 在wxss中设置tab栏和下划线的样式: ```css .tab-bar { display: flex; } .tab-item { height: 40px; line-height: 40px; /* 其他样式 */ } .tab-underline { height: 4px; width: 250px; background-color: #1AAD19; position: absolute; bottom: 0; } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值