1. Component encapsulation
Swiper and Swiper-item components provided by wechat are used to realize monitoring the change of swiper to change options, and also monitoring the change of options to change the current swiper, two-way monitoring two-way change
wxml
<view class="weui-tabs"> <! - options - ><view class="weui-tabs-bar__wrp" id="weui-tabs-bar__wrp">
<scroll-view scroll-x scroll-into-view="item_{{currentView}}" scroll-with-animation="{{animation}}">
<view class="weui-tabs-bar__content">
<block wx:for="{{tabs}}" wx:key="title">
<view id="item_{{index}}" class="weui-tabs-bar__item" style="background-color: {{tabBackgroundColor}}; color: {{activeTab === index ? tabActiveTextColor : tabInactiveTextColor}};" bindtap="handleTabClick" data-index="{{index}}">
<view class="weui-tabs-bar__title {{tabClass}} {{activeTab === index ? activeClass : ''}}" style="border-bottom-color: {{activeTab === index ? tabUnderlineColor : 'transparent'}}">
<text class="">{{item.title}}</text>
</view>
</view>
</block>
</view>
</scroll-view>
</view><! - content - ><swiper class="{{swiperClass}}" current="{{activeTab}}" duration="{{duration}}" bindchange="handleSwiperChange">
<swiper-item wx:for="{{tabs}}" wx:key="title">
<slot name="tab-content-{{index}}"></slot>
</swiper-item>
</swiper>
</view>
Copy the code
js
Component({
options: {
addGlobalClass: true.pureDataPattern: / / ^ _,
multipleSlots: true
},
properties: {
tabs: { type: Array.value: []},// Data item format {title:" option 1 "}
tabClass: { type: String.value: ' ' },// Tabbed style
swiperClass: { type: String.value: ' ' },// The content area swiper style
activeClass: { type: String.value: ' ' },// Class when the option is enabled
tabUnderlineColor: { type: String.value: '#07c160' },// Activate the underline color
tabActiveTextColor: { type: String.value: '# 000000' },// Activate text color
tabInactiveTextColor: { type: String.value: '# 000000' },// Unselected text color
tabBackgroundColor: { type: String.value: '#ffffff' },// TAB background color
activeTab: { type: Number.value: 0 },// The TAB is currently activated
animation: { type: Boolean.value: true },// Whether to use animation
duration: { type: Number.value: 500 }// Animation transition time
},
data: {
currentView: 0
},
observers: {
activeTab(_activeTab) {
var len = this.data.tabs.length;
if (len === 0) return;
var currentView = _activeTab - 1;
if (currentView < 0) currentView = 0;
if (currentView > len - 1) currentView = len - 1;
this.setData({ currentView: currentView }); }},methods: {
handleTabClick(e) {
var index = e.currentTarget.dataset.index;
this.setData({ activeTab: index });
this.triggerEvent('tabclick', { index: index });
},
handleSwiperChange(e) {
var index = e.detail.current;
this.setData({ activeTab: index });
this.triggerEvent('change', { index: index }); }}});Copy the code
wxss
.weui-tabs{
width:100%
}
.weui-tabs-bar__wrp{
width:100%;
background:#fff;
}
.weui-tabs-bar__content{
width:100%;
white-space:nowrap;
}
.weui-tabs-bar__item{
display:inline-block;
}
.weui-tabs-bar__title{
display:inline-block;
border-bottom-width:2px;
border-bottom-style:solid;
border-bottom-color:transparent;
padding-bottom: 4px;
margin: 0 10px;
color: rgba(0.0.0.0.9);
}
.tab-bar-title__selected{
font-size: 20px;
font-weight: bold;
}
Copy the code
json
{
"component": true."usingComponents": {}}Copy the code
2, use,
json
{
"usingComponents": {
"tab":"/com/tabs/index"}}Copy the code
wxml
<view class="page">
<tab tabs="{{tabs}}" activeTab="{{activeTab}}" swiperClass="weui-tabs-swiper" bindtabclick="onTabClick" bindchange="onChange" activeClass="tab-bar-title__selected">
<view class="tab-content" slot="tab-content-0">Option a</view>
<view class="tab-content" slot="tab-content-1">Option 2</view>
<view class="tab-content" slot="tab-content-2">Option three</view>
<view class="tab-content" slot="tab-content-3">Four options</view>
<view class="tab-content" slot="tab-content-4">Five options</view>
<view class="tab-content" slot="tab-content-5">Option six</view>
<view class="tab-content" slot="tab-content-6">Seven options</view>
</tab>
</view>
Copy the code
js
Page({
data: {
tabs: [].activeTab: 0,
},
onLoad () {
const tabs = [
{ title: 'Option one'}, {title: 'Option two'}, {title: 'Option three'}, {title: 'Option four'}, {title: 'Option five'}, {title: 'Option six'}, {title: 'Option seven',},]this.setData({ tabs })
},
onTabClick (e) {
this.setData({ activeTab: e.detail.index })
},
onChange (e) {
this.setData({ activeTab: e.detail.index })
},
})
Copy the code
wxss
.weui-tabs-swiper{
height: calc(100vh - 76rpx);
}
.tab-content{
width: 100%;
height: 100%;
overflow: scroll;
}
Copy the code