微信小程序实现navbar导航栏

一、效果图

 

 

 

二、涉及到组件

1.view组件

2.swiper组件

三、原理

 整体来讲是比较简单的,顶部的navbar是使用flex进行布局的;下面的内容区域则是使用到swiper组件,使用方式比较简单,可以查看微信小程序的文档。

四、代码

wxml:

 1 <view class="movie-container">
 2   <!-- 导航栏 -->
 3   <view class="navbar">
 4     <block wx:for="{{navbarTitle}}" wx:key="index">
 5       <view class="navbar-item {{navbarActiveIndex === index ? 'navbar-item-active' : ''}}" data-navbar-index="{{index}}" catchtap="onNavBarTap">
 6         <text>{{item}}</text>
 7       </view>
 8     </block>
 9   </view>
10   <view class="movie-content-wrapper">
11     <swiper current="{{navbarActiveIndex}}" bindanimationfinish="onBindAnimationFinish">
12       <swiper-item wx:for="{{navbarTitle}}" wx:key="index">
13         <scroll-view scroll-y="{{true}}">
14           {{item}}
15         </scroll-view>
16       </swiper-item>
17     </swiper>
18   </view>
19 </view>

wxss:

 1 .movie-container{
 2   display: flex;
 3   flex-direction: column;
 4 }
 5 .navbar{
 6   display: flex;
 7   position: absolute;
 8   left: 0;
 9   top: 0;
10   z-index: 500;
11   width: 100%;
12   height: 50px;
13   flex-direction: row;
14   text-align: center;
15   color: #A8A8A8;
16   font-size: 15px;
17   box-sizing: border-box;
18   background-color: #FFF;
19   border-bottom: 1rpx solid #DFDFDF;
20 }
21 .navbar-item{
22   flex: 1;
23   padding: 26rpx 0px;
24 }
25 .navbar-item-active{
26   transition: all 0.3s;
27   border-bottom: 10rpx solid #494949;
28   color: #494949;
29 }
30 
31 .movie-content-wrapper{
32   padding-top: 50px;
33 }

js:

 1 Page({
 2 
 3   /**
 4    * 页面的初始数据
 5    */
 6   data: {
 7     navbarActiveIndex: 0,
 8     navbarTitle: [
 9       "热映",
10       "Top250",
11       "口碑榜",
12       "新片榜"
13     ]
14   },
15 
16   /**
17    * 点击导航栏
18    */
19   onNavBarTap: function (event) {
20     // 获取点击的navbar的index
21     let navbarTapIndex = event.currentTarget.dataset.navbarIndex
22     // 设置data属性中的navbarActiveIndex为当前点击的navbar
23     this.setData({
24       navbarActiveIndex: navbarTapIndex      
25     })
26   },
27 
28   /**
29    * 
30    */
31   onBindAnimationFinish: function ({detail}) {
32     // 设置data属性中的navbarActiveIndex为当前点击的navbar
33     this.setData({
34       navbarActiveIndex: detail.current
35     })
36   }
37 })

代码比较简单,记录学习一下。

转载于:https://www.cnblogs.com/llcdxh/p/9390375.html

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序中可以通过自定义导航实现更加个性化的界面设计。以下是一个简单的实现步骤: 1. 在 app.json 文件中设置 "navigationStyle": "custom",表示使用自定义导航。 2. 在页面的 WXML 文件中编写自定义导航的 HTML 代码,例如: ``` <view class="navbar"> <view class="navbar-title">自定义导航</view> </view> ``` 其中,navbar 表示导航的样式,navbar-title 表示标题的样式,可以根据需要进行自定义。 3. 在页面的 wxss 文件中设置导航的样式,例如: ``` .navbar { height: 44px; background-color: #fff; border-bottom: 1px solid #ccc; display: flex; align-items: center; justify-content: center; } .navbar-title { font-size: 18px; font-weight: bold; color: #000; } ``` 其中,height 表示导航的高度,background-color 表示导航的背景颜色,border-bottom 表示导航底部的边框线,display、align-items 和 justify-content 表示导航标题的布局方式,navbar-title 表示标题的样式,可以根据需要进行自定义。 4. 在页面的 JS 文件中设置导航的返回按钮,例如: ``` wx.showModal({ title: '提示', content: '确认返回上一页?', success: function (res) { if (res.confirm) { wx.navigateBack({ delta: 1 }) } } }) ``` 其中,wx.showModal 表示显示一个模态框,title 表示模态框的标题,content 表示模态框的内容,success 表示点击确定按钮后执行的回调函数,wx.navigateBack 表示返回上一页。 通过以上步骤,就可以实现微信小程序的自定义导航

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值