<view> <view class="navbar"> <block wx:for="{{body}}" wx:key="index"> <view class="navbar-item {{activeIndex === index ? 'active':''}}" style="position: relative;"> <text> {{ item.text }} </text> <view data-value="{{index}}" style="position: absolute;top: 0;left: 0;right: 0;bottom:0;z-index: 999" bindtap="bodyChange"></view> </view> </block> </view> <swiper class="body-data" duration="500" bindchange="bodySwiper" current="{{activeIndex}}"> <block wx:for="{{body}}" wx:key="index"> <swiper-item> <block wx:if="{{body[index].data.length!=0}}"> <scroll-view scroll-y bindscroll="bodyscroll"> <block wx:for="{{body[index].data}}" wx:key="index"> <view>{{item.name}}</view> </block> </scroll-view> </block> <block wx:else> <view>暂无数据</view> </block> </swiper-item> </block> </swiper> </view>
// pages/swipertab/swipertab.js Page({ /** * 页面的初始数据 */ data: { activeIndex:0, body:[ { value:'',text:'首页', data:[ {name:'最新消息0'}, {name:'最新消息'} ] }, { value:'',text:'首页1', data:[ {name:'最新消息1'}, {name:'最新消息'} ] }, { value:'',text:'首页2', data:[ {name:'最新消息2'}, {name:'最新消息'} ] }, { value:'',text:'首页3', data:[ {name:'最新消息3'}, {name:'最新消息'} ] }, { value:'',text:'首页4', data:[ {name:'最新消息4'}, {name:'最新消息'} ] }, { value:'',text:'首页5', data:[ {name:'最新消息5'}, {name:'最新消息'} ] }, { value:'',text:'首页6', data:[] }, { value:'',text:'首页7', data:[] }, { value:'',text:'首页8', data:[] }, { value:'',text:'首页9', data:[] }, { value:'',text:'首页10', data:[] } ] }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, bodyChange(e){ this.setData({ activeIndex:e.target.dataset.value }) }, bodySwiper(e){ this.setData({ activeIndex:e.detail.current }) }, bodyScroll(e){ console.log(e) }, } })
page{ min-height: 100vh; background: #ccc; } .active{ background: red; transition: 0.3s; }