如何在微信小程序中实现今日头条App那样的Topbar

今日头条App的Topbar是一个典型的频道管理和切换组件,自己前段时间研究了一番,在微信小程序上也实现了类似的效果。

http://biebu.xin/2017/03/07/%E5%A6%82%E4%BD%95%E5%9C%A8%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E4%B8%AD%E5%AE%9E%E7%8E%B0%E4%BB%8A%E6%97%A5%E5%A4%B4%E6%9D%A1App%E9%82%A3%E6%A0%B7%E7%9A%84Topbar/

我们先看具体效果好了 ↓↓↓

wx-topbar-screenshot-1

wx-topbar-screenshot-2

wx-topbar-screenshot-3

这个项目(wx-topbar)已经放在GitHub上了——点此前往,欢迎学习交流。

接下来,简要说一下实现思路。

先看视图层,Topbar横向滚动对应的WXML代码如下:

<scroll-view class="navbar" scroll-x="true" scroll-left="{{scrollNavbarLeft}}">
    <view class="navbar-item {{ navbarArray[item].type }}" id="{{ item }}" wx:for="{{ navbarShowIndexArray }}" catchtap="onTapNavbar">
        <view class="navbar-item-wrap">{{ navbarArray[item].text }}</view>
    </view>
    <view class="navbar-item visibility-hidden">
        <view class="navbar-item-wrap">空白</view>
    </view>
</scroll-view>
<view class="navbar-arrow-down" catchtap="showChannelSettingModal">
    <view class="navbar-arrow-down-wrap">
        <image class="navbar-arrow-icon" src="/images/index/icon_arrow_down.png"></image>
    </view>
</view>

scroll-view负责Topbar中各个频道的呈现,所有频道的相关数据都存储在navbarArray这个对象数组里,而数组navbarShowIndexArray里存储了要显示频道在数组navbarArray中的索引。

不难猜测,频道是否选中高亮,与数组navbarArray有关;频道是否显示,与数组navbarShowIndexArray有关。

点击某个频道名称,就会触发对应频道的切换操作。

view.navbar-arrow-down对应的是右上角的向下箭头,可采用fixed定位类型,点击后弹出管理频道的Modal.

<view class="channel-setting-modal {{ channelSettingModalShow }}" hidden="{{ channelSettingModalHide }}">
    <view class="channel-show-text">
        <view class="channel-show-text-wrap">显示频道</view>
    </view>
    <view class="channel-item" wx:for="{{ navbarShowIndexArray }}">
        <view class="channel-item-wrap">
            <view class="channel-item-left">
                <image class="channel-item-icon-minus {{ !index || navbarShowIndexArray.length < 4 ? 'visibility-hidden' : '' }}" id="{{ item }}.0" src="/images/index/icon_minus.png" catchtap="hideChannel"></image>
                <view class="channel-item-text">{{ navbarArray[item].text }}</view>
            </view>
            <view class="channel-item-up {{ index < 2 ? 'visibility-hidden' : '' }}" id="{{ item }}.00" catchtap="upChannel">上移</view>
        </view>
    </view>
    <view class="channel-hide-text">
        <view class="channel-hide-text-wrap">隐藏频道</view>
    </view>
    <view class="channel-item" wx:for="{{ navbarHideIndexArray }}">
        <view class="channel-item-wrap">
            <view class="channel-item-left">
                <image class="channel-item-icon-plus" id="{{ item }}.0" src="/images/index/icon_plus.png" catchtap="showChannel"></image>
                <view class="channel-item-text">{{ navbarArray[item].text }}</view>
            </view>
            <view class="channel-item-up visibility-hidden">上移</view>
        </view>
    </view>
</view>

在这个管理频道的Modal里,通过改变数组navbarShowIndexArray来控制频道是否显示和显示顺序,同时,需要另外一个数组navbarHideIndexArray来存储隐藏的频道。

Modal显示的时候,Topbar需要被另一个写有“频道设置”字样的Bar覆盖。

<view class="channel-setting {{ channelSettingShow }}">
    <view class="channel-setting-text">频道设置</view>
    <view class="navbar-arrow-up" catchtap="hideChannelSettingModal">
        <image class="navbar-arrow-icon navbar-arrow-icon-up" src="/images/index/icon_arrow_up.png"></image>
    </view>
</view>

然后,我们来看逻辑层的实现。初始化的部分data如下:

data: {
    navbarArray: [{
        text: '推荐',
        type: 'navbar-item-active'
    }, {
        text: '热点',
        type: ''
    }, {
        text: '视频',
        type: ''
    }, {
        text: '图片',
        type: ''
    }, {
        text: '段子',
        type: ''
    }, {
        text: '社会',
        type: ''
    }, {
        text: '娱乐',
        type: ''
    }, {
        text: '科技',
        type: ''
    }, {
        text: '体育',
        type: ''
    }, {
        text: '汽车',
        type: ''
    }, {
        text: '财经',
        type: ''
    }, {
        text: '搞笑',
        type: ''
    }],
    navbarShowIndexArray: Array.from(Array(12).keys()),
    navbarHideIndexArray: [],
    channelSettingShow: '',
    channelSettingModalShow: '',
    channelSettingModalHide: true
}

navbar-item-active是一个可使频道高亮的ClassnavbarShowIndexArray初始化的结果是一个0到11的数组,刚好是数组navbarArray的所有元素的索引。显然,初始化的结果是所有频道都将显示。

为了实现频道个性化配置的保存,navbarShowIndexArray还需要通过小程序的数据缓存API储存起来。

storeNavbarShowIndexArray: function() {
    const that = this;
    wx.setStorage({
        key: 'navbarShowIndexArray',
        data: that.data.navbarShowIndexArray
    });
}

切换频道的函数如下:

switchChannel: function(targetChannelIndex) {
    this.getArticles(targetChannelIndex);

    let navbarArray = this.data.navbarArray;
    navbarArray.forEach((item, index, array) => {
        item.type = '';
        if (index === targetChannelIndex) {
            item.type = 'navbar-item-active';
        }
    });
    this.setData({
        navbarArray: navbarArray,
        currentChannelIndex: targetChannelIndex
    });
}

这样,频道的管理和简单切换我们就实现了。

但是,到此为止,频道的切换只能通过点击对应Topbar中频道那一小块区域来实现,要是在正文区域左滑和右滑也能切换频道就好了。

一个容易想到的思路是,在正文区域绑定touch事件,通过坐标判断滑动方向,然后使Topbar中当前频道的上一个或下一个频道高亮,同时,控制Topbar横向滚动合适的偏移长度,以确保切换后的频道能出现在视图区域。

onTouchstartArticles: function(e) {
    this.setData({
        'startTouchs.x': e.changedTouches[0].clientX,
        'startTouchs.y': e.changedTouches[0].clientY
    });
},
onTouchendArticles: function(e) {
    let deltaX = e.changedTouches[0].clientX - this.data.startTouchs.x;
    let deltaY = e.changedTouches[0].clientY - this.data.startTouchs.y;
    if (Math.abs(deltaX) > Math.abs(deltaY) && Math.abs(deltaX) > 10) {
        let deltaNavbarIndex = deltaX > 0 ? -1 : 1;
        let currentChannelIndex = this.data.currentChannelIndex;
        let navbarShowIndexArray = this.data.navbarShowIndexArray;
        let targetChannelIndexOfNavbarShowIndexArray = navbarShowIndexArray.indexOf(currentChannelIndex) + deltaNavbarIndex;
        let navbarShowIndexArrayLength = navbarShowIndexArray.length;
        if (targetChannelIndexOfNavbarShowIndexArray >= 0 && targetChannelIndexOfNavbarShowIndexArray <= navbarShowIndexArrayLength - 1) {
            let targetChannelIndex = navbarShowIndexArray[targetChannelIndexOfNavbarShowIndexArray];
            if (navbarShowIndexArrayLength > 6) {
                let scrollNavbarLeft;
                if (targetChannelIndexOfNavbarShowIndexArray < 5) {
                    scrollNavbarLeft = 0;
                } else if (targetChannelIndexOfNavbarShowIndexArray === navbarShowIndexArrayLength - 1) {
                    scrollNavbarLeft = this.rpx2px(110 * (navbarShowIndexArrayLength - 6));
                } else {
                    scrollNavbarLeft = this.rpx2px(110 * (targetChannelIndexOfNavbarShowIndexArray - 4));
                }
                this.setData({
                    scrollNavbarLeft: scrollNavbarLeft
                });
            }
            this.switchChannel(targetChannelIndex);
        }
    }
}

更多的技术细节,请移步GitHub——点此前往

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在QWidget实现顶部导航栏,可以使用QTabBar 或QToolBar,这两个控件都可以实现类似的效果。 使用QTabBar: QTabBar是一个选项卡控件,可以在其添加多个选项卡,并且可以自定义选项卡的样式。在QWidget使用QTabBar可以实现顶部导航栏的效果。 示例代码: ```python from PyQt5.QtWidgets import QApplication, QWidget, QTabBar, QVBoxLayout class TopBar(QWidget): def __init__(self): super().__init__() # 创建一个QTabBar对象 self.tab_bar = QTabBar() # 设置选项卡的样式 self.tab_bar.setExpanding(True) self.tab_bar.setStyleSheet("QTabBar::tab { height: 35px; width: 100px; }") # 添加选项卡 self.tab_bar.addTab("首页") self.tab_bar.addTab("消息") self.tab_bar.addTab("设置") # 创建一个垂直布局,并将QTabBar添加到布局 layout = QVBoxLayout() layout.addWidget(self.tab_bar) # 设置QWidget的布局 self.setLayout(layout) if __name__ == '__main__': app = QApplication([]) bar = TopBar() bar.show() app.exec_() ``` 使用QToolBar: QToolBar是一个工具栏控件,可以在其添加多个工具按钮,并且可以自定义工具按钮的样式。在QWidget使用QToolBar可以实现顶部导航栏的效果。 示例代码: ```python from PyQt5.QtWidgets import QApplication, QWidget, QToolBar, QAction class TopBar(QWidget): def __init__(self): super().__init__() # 创建一个QToolBar对象 self.tool_bar = QToolBar() # 设置工具按钮的样式 self.tool_bar.setIconSize(QSize(32, 32)) self.tool_bar.setStyleSheet("QToolButton { border: none; }") # 添加工具按钮 home_action = QAction(QIcon("home.png"), "首页", self) message_action = QAction(QIcon("message.png"), "消息", self) setting_action = QAction(QIcon("setting.png"), "设置", self) self.tool_bar.addAction(home_action) self.tool_bar.addAction(message_action) self.tool_bar.addAction(setting_action) # 将QToolBar添加到QWidget self.addToolBar(self.tool_bar) if __name__ == '__main__': app = QApplication([]) bar = TopBar() bar.show() app.exec_() ``` 注意:以上代码的图片文件需要自己准备。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值