小程序自定义导航栏滑动和不滑动

 可滑动导航栏!

1.我使用的是view加上轮播图,为什么我们点击导航下面的页面就会发生改变?为什么滑动页面导航栏也会发生改变呢?其实它的关键就是用一个值将它们联系起来。我自己定义了一个currenttab.

    ①使用data-current绑定值监听值的变化当值发生改变时,导航栏就改变。我们绑定点击事件swichnav.这样你点击导航栏,导航栏就会在你点击的那里。

  swichNav(e){
        console.log(e.target.dataset.current);
          this.currenttab=e.target.dataset.current
      },

 ②但是你滑动页面没有发生改变啊。因为轮播图没有监听变化啊。所以先使用:current="currenttab"绑定值(表示当前页面的值)。在绑定change事件switchtab.让轮播图监听的值等于上面导航的值。

   switchTab(e){
        console.log(e);
        this.currenttab=e.detail.current
      },

 所有代码

<view class="tab">
          <view :class="['tab-item' ,currenttab==0 ? 'active' : '']" data-current="0" @click="swichNav">游记</view>
          <view :class="['tab-item' ,currenttab==1 ? 'active' : '']" data-current="1" @click="swichNav">粉丝</view>
          <view :class="['tab-item' ,currenttab==2 ? 'active' : '']" data-current="2" @click="swichNav">收藏</view>
      </view>
     <swiper :duration="1000" :current="currenttab" @change="switchTab">
       <swiper-item>
         <view class="swiper-item">1</view>
       </swiper-item>
       <swiper-item>
         <view class="swiper-item">2</view>
       </swiper-item>
       <swiper-item>
         <view class="swiper-item">3</view>
       </swiper-item>
     </swiper>

不滑动只点击

他们之间的区别是只用监听导航栏的变化,故只需要导航栏绑定点击事件就可以。

<view :class="['left', current==1?'select':'']" @change="swithNav" data-code="1">
                 <text>登录</text>
               </view>
               <view :class="['right', current==0?'select':'']" @change="swithNav" data-code="0">
                 <text>注册</text>
               </view>
 <view class="input-box" :hidden="current==0">
                  
  </view>
             <!-- 注册 -->
 <view class="input-box" :hidden="current==1">
        </view>

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值