微信小程序记录-实现顶部可切换导航栏

这里只为实现个简单好用的顶部导航栏,重点是为了好看,因为小程序它本身没有啊!!

1.首先就是效果图

效果先不管,好看总没错。


1.gif

2.关键的代码

.wxml
<!--index.wxml-->
<!--导航条-->
<view class="navbar">
  <text wx:for="{{navbar}}" data-idx="{{index}}" class="item {{currentTab==index ? 'active' : ''}}" wx:key="unique" bindtap="navbarTap">{{item}}</text>
</view>
<!--内容-->
<view wx:if="{{currentTab==0}}">
  <view class='info'>
    <image src='/img/icon.png'></image>
    <text>没有已关注</text>
  </view>
</view>

<view wx:elif="{{currentTab==1}}">
  <view class='info'>
    <image src='/img/icon.png'></image>
    <text >没有未关注</text>
  </view>
</view>
.wxss
/**index.wxss**/
page{
  display: flex;
  flex-direction: column;
  height: 100%;
}
.navbar{
  flex: none;
  display: flex;
  background: #fff;
  font-size: 30rpx;
  color: #bbbbbb;
}
.navbar .item{
  position: relative;
  flex: auto;
  text-align: center;
  line-height: 90rpx;
}
.navbar .item.active{
  color: #908df5;
}
.navbar .item.active:after{
  content: "";
  display: block;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 5rpx;
  background: #908df5;
}
.info{
 display:flex;
 margin-top: 250rpx;
 justify-content: center;
 align-items:center;
 flex-direction: column;
}
.info image { 
  width:300rpx;
  height:300rpx;
}
.info text {
  margin-top: 10rpx;
  font-size: 30rpx;
  color: #908df5;
}

.js
//index.js
const app = getApp()
Page({
  data: {
    navbar: ['已关注', '未关注'],
    currentTab: 0,
  },
  //切换bar
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
    //全局变量
    app.globalData.currentTab = e.currentTarget.dataset.idx;
  },
  onShow() {
    this.setData({
      currentTab: app.globalData.currentTab
    })
  },

})

3.说下值得注意的几个问题

1.由于wx.switchTab不能携带参数,如果需要使用在tabBar 页面,跳转至指定分页,配置currentTab在全局变量中,页面切换和页面跳转时操作currentTab达到效果。

//app.js
  globalData: {
    currentTab: 0
  }

2.需要更润滑的拖动切换效果,记得用swiper!

4.加入swiper,实现可拖拽切换的顶部导航栏

2.gif
.代码部分
.wxml
<!--index.wxml-->
<!--导航条-->
<view class="navbar">
  <text wx:for="{{navbar}}" data-idx="{{index}}" class="item {{currentTab==index ? 'active' : ''}}" wx:key="unique" bindtap="navbarTap">{{item}}</text>
</view>
<!--内容-->
<!-- <view wx:if="{{currentTab==0}}">
  <view class='info'>
    <image src='/img/icon.png'></image>
    <text>没有已关注</text>
  </view>
</view>

<view wx:elif="{{currentTab==1}}">
  <view class='info'>
    <image src='/img/icon.png'></image>
    <text>没有未关注</text>
  </view>
</view> -->

<swiper class="swiper" current="{{currentTab}}" duration="200" bindchange="swiperChange">
  <swiper-item>
    <view class='info'>
      <image src='/img/icon.png'></image>
      <text>没有未关注</text>
    </view>
  </swiper-item>
  <swiper-item>
    <view class='info'>
      <image src='/img/icon.png'></image>
      <text>没有未关注</text>
    </view>
  </swiper-item>
</swiper>
.wxss
/**index.wxss**/
page{
  display: flex;
  flex-direction: column;
  height: 100%;
}
.navbar{
  flex: none;
  display: flex;
  background: #fff;
  font-size: 30rpx;
  color: #bbbbbb;
}
.navbar .item{
  position: relative;
  flex: auto;
  text-align: center;
  line-height: 90rpx;
}
.navbar .item.active{
  color: #908df5;
}
.navbar .item.active:after{
  content: "";
  display: block;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 5rpx;
  background: #908df5;
}
.swiper{
  height: 100%
}
.info{
 display:flex;
 margin-top: 250rpx;
 justify-content: center;
 align-items:center;
 flex-direction: column;
}
.info image { 
  width:300rpx;
  height:300rpx;
}
.info text {
  margin-top: 10rpx;
  font-size: 30rpx;
  color: #908df5;
}

.js
//index.js
const app = getApp()
Page({
  data: {
    navbar: ['已关注', '未关注'],
    currentTab: 0,
  },
  //切换bar
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
    //全局变量
    app.globalData.currentTab = e.currentTarget.dataset.idx;
  },
  onShow() {
    this.setData({
      currentTab: app.globalData.currentTab
    })
  },

  swiperChange: function (e) {
    this.setData({
      currentTab: e.detail.current,
    })
    //全局变量
    app.globalData.currentTab = e.detail.current;
  },
})

5.简单又紧张刺激的学习就到此为止

休息

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • create by jsliang on 2018-9-17 17:58:56 Recently revised...
    梁_飘阅读 1,673评论 0 6
  • 因新工作主要负责微信小程序这一块,最近的重心就移到这一块,该博客是对微信小程序整体的整理归纳以及标明一些细节点,初...
    majun00阅读 7,624评论 0 9
  • 微信小程序由于适用性强、逻辑简要、开发迅速的特性,叠加具有海量活跃用户的腾讯公司背景,逐渐成为了轻量级单一功能应用...
    纯文笔记阅读 4,183评论 1 9
  • 当你倒下时你在思考什么? 人生从不一本正经,按部就班,假如那般,如此平淡的人生也无意义!也许,当你濒临死亡,处在生...
    雷声镜像阅读 755评论 0 1
  • 躺在床上,耳机里传出喜爱的音乐,闭上眼,本想静静的让音乐伴随着入睡。但随之脑海里就像放电影似的,把这一年的情景...
    亮神阅读 311评论 0 1

友情链接更多精彩内容