微信小程序滑动切换导航,导航栏跟随顶部tab导航滚动

本文介绍了如何在微信小程序中实现滑动切换导航,通过使用标签,配合js代码,实现在内容滚动时导航栏跟随顶部tab导航。示例代码包括wxml、wxss和js部分,并提供了Json数据格式的要求,便于后端数据渲染。对于想要进行小程序开发的读者,建议考虑借助专业开发平台如厦门在乎科技进行开发。
摘要由CSDN通过智能技术生成

前言

很多App都有这种设计,例如淘宝、爱奇艺、今日头条、知乎等都有用到,大部分的App都会用到这种设计,非常主流。

tab导航栏使用<scroll-view>标签,内容使用<swiper>,不多说,直接上代码,为了更真实展示实例的使用,我直接就配置了服务器来取数据进行渲染。

index.wxml

<view class="container">
    <!-- tab导航栏 -->
    <!-- scroll-left属性可以控制滚动条位置 -->
    <!-- scroll-with-animation滚动添加动画过渡 -->
    <scroll-view scroll-x="true" class="nav" scroll-left="{
  {navScrollLeft}}" scroll-with-animation="{
  {true}}">
        <block wx:for="{
  {navData}}" wx:for-index="idx" wx:for-item="navItem" wx:key="idx">
            <view class="nav-item {
  {currentTab == idx ?'active':''}}"  data-current="{
  {idx}}" bindtap="switchNav">{
  {navItem.text}}</view>
        </block>        
    </scroll-view>
    <!-- 页面内容 -->
    <swiper class="tab-box" current="{
  {currentTab}}" duration="300" bindchange="switchTab">        
        <swiper-item wx:for="{
  {yxlist}}" wx:for-item="tabItem" wx:for-index="idx" wx:key="idx" class="tab-content">
            <view>{
  {tabItem.yxname}}</view>
            <image src="{
  {tabItem.yxlogo}}" style="width:100px;height:100px;"></image>
        </swiper-item>
    </swiper>
</view>

index.wxss

/**index.wxss**/
page{
  width: 100%;
  height: 100%;
}
.container{
  width: 100%;
  height: 100%;
}
.nav {
  height: 80rpx;
  width: 100%;
  box-sizing: border-box;
  overflow: hidden;
  line-height: 80rpx;
  background: #f7f7f7;
  font-size: 16px;
  white-space: nowrap;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 99;
}
.nav-item {
  width: 20%;
  display: inline-block;
  text-align: center;
}
.nav-item.active{
  color: red;
  font-weight: bold;
}
.tab-box{
  background: #fff;
  padding-top: 80rpx;
  height: 100%;
  box-sizing: border-box;
}
.tab-content{
  overflow-y: scroll;
}

index.js

//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        motto: 'Hello World',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        navData:[
            {
                text: '广科'
            },
            {
                text: '广东理工'
            },
            {
                text: '广石油'
            },
            {
                text: '广东工商'
            },
            {
                text: '广州科技'
            },
            {
                text: '白云'
            },
            {
                text: '肇庆'
            },
            {
                text: '嘉应'
            },
            {
                text: '华商'
            }
        ],
        currentTab: 0,
        navScrollLeft: 0
    },
    //事件处理函数
    onLoad: function () {
      var that = this;
      //向后端服务器发起请求数据
      wx.request({
        //URL
        url: 'https://www.likeyun.cn/api/yxlist.json',
        //发送
  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值