微信小程序滑動切換導航 導航欄跟隨滾動導航 頂部tab導航

前言

不少App都有這種設計,例如淘寶、愛奇藝、今日頭條、知乎等都有用到,大部分的App都會用到這種設計,很是主流。json

image.png

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',
        //發送的數據
        data: {},
        //請求的數據時JSON格式
        header: {
          'Content-Type':'application/json'
        },
        //請求成功
        success: function (res) {
          //控制檯打印(開發調試用)
          console.log(res.data)
          //把全部結果存進一個名爲yxlist的數組
          that.setData({
            yxlist: res.data.yuanxiaolist,
          })
        }
      })
      wx.getSystemInfo({
          success: (res) => {
              this.setData({
                  pixelRatio: res.pixelRatio,
                  windowHeight: res.windowHeight,
                  windowWidth: res.windowWidth
              })
          },
      })       
    },
    switchNav(event){
        var cur = event.currentTarget.dataset.current; 
        //每一個tab選項寬度佔1/5
        var singleNavWidth = this.data.windowWidth / 5;
        //tab選項居中                            
        this.setData({
            navScrollLeft: (cur - 2) * singleNavWidth
        })      
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }
    },
    switchTab(event){
        var cur = event.detail.current;
        var singleNavWidth = this.data.windowWidth / 5;
        this.setData({
            currentTab: cur,
            navScrollLeft: (cur - 2) * singleNavWidth
        });
    }
})

效果演示

OK了,是否是很簡單!api

Json數據格式

爲了方便你們本身寫後端,我把Json數據格式貼出來,本身寫後端的時候須要輸出這樣的格式才能夠被我上面的代碼所渲染。數組

{
    "yuanxiaolist":[
        {
            "id":"1",
            "yxname":"廣東科技學院",
            "yxsx":"民辦",
            "city":"東莞",
            "yxlogo":"http://pic.iask.cn/fimg/327765034238.jpg",
            "zynum":"25"
        },
        {
            "id":"2",
            "yxname":"廣東理工學院",
            "yxsx":"民辦",
            "city":"肇慶",
            "yxlogo":"http://pic.iask.cn/fimg/240805034233.jpg",
            "zynum":"27"
        },
        {
            "id":"3",
            "yxname":"廣東石油化工學院",
            "yxsx":"公辦",
            "city":"茂名",
            "yxlogo":"http://pic.iask.cn/fimg/9335034245.jpg",
            "zynum":"32"
        },
        {
            "id":"4",
            "yxname":"廣東工商職業技術大學",
            "yxsx":"民辦",
            "city":"肇慶",
            "yxlogo":"http://pic.iask.cn/fimg/709495034235.jpg",
            "zynum":"13"
        },
        {
            "id":"5",
            "yxname":"廣州科技職業技術大學",
            "yxsx":"民辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/335015034247.jpg",
            "zynum":"10"
        },
        {
            "id":"6",
            "yxname":"廣東白雲學院",
            "yxsx":"民辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/80225034222.jpg",
            "zynum":"26"
        },
        {
            "id":"7",
            "yxname":"肇慶學院",
            "yxsx":"公辦",
            "city":"肇慶",
            "yxlogo":"http://pic.iask.cn/fimg/489565034270.jpg",
            "zynum":"24"
        },
        {
            "id":"8",
            "yxname":"嘉應學院",
            "yxsx":"公辦",
            "city":"梅州",
            "yxlogo":"http://pic.iask.cn/fimg/297705034258.jpg",
            "zynum":"41"
        },
        {
            "id":"9",
            "yxname":"廣東財經大學華商學院",
            "yxsx":"民辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/700785034254.jpg",
            "zynum":"22"
        },
        {
            "id":"10",
            "yxname":"韶關學院",
            "yxsx":"公辦",
            "city":"韶關",
            "yxlogo":"http://pic.iask.cn/fimg/847955034266.jpg",
            "zynum":"41"
        },
        {
            "id":"11",
            "yxname":"廣東工業大學華立學院",
            "yxsx":"民辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/569205034253.jpg",
            "zynum":"17"
        },
        {
            "id":"12",
            "yxname":"五邑大學",
            "yxsx":"公辦",
            "city":"江門",
            "yxlogo":"http://pic.iask.cn/fimg/169985034268.jpg",
            "zynum":"10"
        },
        {
            "id":"13",
            "yxname":"吉林大學珠海學院",
            "yxsx":"民辦",
            "city":"珠海",
            "yxlogo":"http://pic.iask.cn/fimg/398695034261.jpg",
            "zynum":"29"
        },
        {
            "id":"14",
            "yxname":"仲愷農業工程學院",
            "yxsx":"公辦",
            "city":"廣州",
            "yxlogo":"https://p.pstatp.com/origin/ff690001b267aab6cf6d",
            "zynum":"21"
        },
        {
            "id":"15",
            "yxname":"廣東海洋大學寸金學院",
            "yxsx":"民辦",
            "city":"湛江",
            "yxlogo":"http://pic.iask.cn/fimg/621215034228.jpg",
            "zynum":"16"
        },
        {
            "id":"16",
            "yxname":"廣州大學松田學院",
            "yxsx":"民辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/86305034267.jpg",
            "zynum":"21"
        },
        {
            "id":"17",
            "yxname":"華南農業大學珠江學院",
            "yxsx":"民辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/67915034256.jpg",
            "zynum":"17"
        },
        {
            "id":"18",
            "yxname":"嶺南師範學院",
            "yxsx":"公辦",
            "city":"湛江",
            "yxlogo":"http://pic.iask.cn/fimg/495655034263.jpg",
            "zynum":"20"
        },
        {
            "id":"19",
            "yxname":"韓山師範學院",
            "yxsx":"公辦",
            "city":"潮州",
            "yxlogo":"http://pic.iask.cn/fimg/350335034252.jpg",
            "zynum":"24"
        },
        {
            "id":"20",
            "yxname":"廣東技術師範大學天河學院",
            "yxsx":"民辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/716045034248.jpg",
            "zynum":"9"
        },
        {
            "id":"21",
            "yxname":"中山大學新華學院",
            "yxsx":"民辦",
            "city":"東莞",
            "yxlogo":"http://pic.iask.cn/fimg/369325034269.jpg",
            "zynum":"13"
        },
        {
            "id":"22",
            "yxname":"廣東技術師範大學",
            "yxsx":"公辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/128555034237.jpg",
            "zynum":"13"
        },
        {
            "id":"23",
            "yxname":"廣東金融學院",
            "yxsx":"公辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/25165034236.jpg",
            "zynum":"8"
        },
        {
            "id":"24",
            "yxname":"東莞理工學院城市學院",
            "yxsx":"民辦",
            "city":"東莞",
            "yxlogo":"http://pic.iask.cn/fimg/349695034227.jpg",
            "zynum":"14"
        },
        {
            "id":"25",
            "yxname":"廣東培正學院",
            "yxsx":"民辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/704575034265.jpg",
            "zynum":"15"
        },
        {
            "id":"26",
            "yxname":"惠州學院",
            "yxsx":"公辦",
            "city":"惠州",
            "yxlogo":"http://pic.iask.cn/fimg/195935034257.jpg",
            "zynum":"11"
        },
        {
            "id":"27",
            "yxname":"廣東第二師範學院",
            "yxsx":"公辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/594495034234.jpg",
            "zynum":"5"
        },
        {
            "id":"28",
            "yxname":"廣東醫科大學",
            "yxsx":"公辦",
            "city":"湛江",
            "yxlogo":"http://pic.iask.cn/fimg/190995034246.jpg",
            "zynum":"8"
        },
        {
            "id":"29",
            "yxname":"廣州商學院",
            "yxsx":"民辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/759085034243.jpg",
            "zynum":"11"
        },
        {
            "id":"30",
            "yxname":"華南師範大學",
            "yxsx":"公辦",
            "city":"佛山",
            "yxlogo":"http://pic.iask.cn/fimg/828825034255.jpg",
            "zynum":"2"
        },
        {
            "id":"31",
            "yxname":"廣州航海學院",
            "yxsx":"公辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/142945034251.jpg",
            "zynum":"5"
        },
        {
            "id":"32",
            "yxname":"東莞理工學院",
            "yxsx":"公辦",
            "city":"東莞",
            "yxlogo":"http://pic.iask.cn/fimg/640645034241.jpg",
            "zynum":"6"
        },
        {
            "id":"33",
            "yxname":"佛山科學技術學院",
            "yxsx":"公辦",
            "city":"佛山",
            "yxlogo":"http://pic.iask.cn/fimg/65475034232.jpg",
            "zynum":"3"
        },
        {
            "id":"34",
            "yxname":"中山大學南方學院",
            "yxsx":"民辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/562915034264.jpg",
            "zynum":"4"
        },
        {
            "id":"35",
            "yxname":"電子科技大學中山學院",
            "yxsx":"民辦",
            "city":"中山",
            "yxlogo":"http://pic.iask.cn/fimg/719895034230.jpg",
            "zynum":"3"
        },
        {
            "id":"36",
            "yxname":"廣州工商學院",
            "yxsx":"民辦",
            "city":"佛山",
            "yxlogo":"http://pic.iask.cn/fimg/815125034249.jpg",
            "zynum":"7"
        },
        {
            "id":"37",
            "yxname":"廣東財經大學",
            "yxsx":"公辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/815985034226.jpg",
            "zynum":"2"
        },
        {
            "id":"38",
            "yxname":"廣州美術學院",
            "yxsx":"公辦",
            "city":"廣州",
            "yxlogo":"http://pic.iask.cn/fimg/498595034240.jpg",
            "zynum":"5"
        },
        {
            "id":"39",
            "yxname":"北京理工大學珠海學院",
            "yxsx":"民辦",
            "city":"珠海",
            "yxlogo":"http://pic.iask.cn/fimg/675775034225.jpg",
            "zynum":"1"
        }
    ]
}

Author:TANKING
Date:2020-07-12
Web:https://www.likeyun.cn
WeChat:face6009服務器

相關文章
相關標籤/搜索