W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
解釋:滑塊視圖容器。內(nèi)部只允許使用 <swiper-item> 組件描述滑塊內(nèi)容,否則會導致未定義的行為。
屬性名 | 類型 | 默認值 | 必填 | 說明 | 最低版本 |
---|---|---|---|---|---|
indicator-dots | Boolean | false | 否 | 是否顯示面板指示點 | - |
indicator-color | Color | rgba(0, 0, 0, .3) | 否 | 指示點顏色 | - |
indicator-active-color | Color | #333 | 否 | 當前選中的指示點顏色 | - |
autoplay | Boolean | false | 否 | 是否自動切換 | - |
current | Number | 0 | 否 | 當前所在頁面的 index | - |
current-item-id | String | 否 | 當前所在滑塊的 item-id,不能與 current 被同時指定 | 1.11 | |
interval | Number | 5000 | 否 | 自動切換時間間隔,單位 ms | - |
duration | Number | 500 | 否 | 滑動動畫時長,單位 ms | - |
circular | Boolean | false | 否 | 是否采用銜接滑動 | - |
vertical | Boolean | false | 否 | 滑動方向是否為縱向 | - |
previous-margin | String |
| 否 | 前邊距,可用于露出前一項的一小部分,支持 px 和 rpx | 1.11 |
next-margin | String |
| 否 | 后邊距,可用于露出后一項的一小部分,支持 px 和 rpx | 1.11 |
display-multiple-items | Number | 1 | 否 | 同時顯示的滑塊數(shù)量 | 1.11 |
bindchange | EventHandle | 否 | current 改變時會觸發(fā) change 事件,event.detail = {current: current, source: source} | - | |
bindanimationfinish | EventHandle | 否 | 動畫結(jié)束時會觸發(fā) animationfinish 事件,event.detail 同上 | 1.11 |
change 事件中的 source 字段,表示觸發(fā) change 事件的原因,可能值如下:
值 | 說明 |
---|---|
autoplay | 自動播放導致的 swiper 切換 |
touch | 用戶劃動導致的 swiper 切換 |
“” | 其他原因?qū)⒎祷乜兆址?/p> |
代碼示例 :滑塊滑動<view class="wrap">
<view class="card-area">
<swiper
class="swiper"
indicator-dots="{{switchIndicateStatus}}"
indicator-color="rgba(0,0,0,0.30)"
indicator-active-color="#fff"
autoplay="{{switchAutoPlayStatus}}"
current="0"
current-item-id="0"
interval="{{autoPlayInterval}}"
duration="{{switchDuration}}"
circular="true"
vertical="{{switchVerticalStatus}}"
previous-margin="0px"
next-margin="0px"
display-multiple-items="1"
bind:change="swiperChange"
bind:animationfinish="animationfinish">
<swiper-item
s-for="item in items"
item-id="{{itemId}}"
class="{{item.className}}">
<view class="item">{{item.value}}</view>
</swiper-item>
</swiper>
<view class="item-scroll border-bottom">
<text class="switch-text-before">指示點</text>
<switch
class="init-switch"
checked="{{switchIndicateStatus}}"
bind:change="switchIndicate">
</switch>
</view>
<view class="item-scroll border-bottom">
<text class="switch-text-before">自動切換</text>
<switch
checked="{{switchAutoPlayStatus}}"
bind:change="switchAutoPlay"
class="init-switch">
</switch>
</view>
<view class="item-scroll">
<text class="switch-text-before">縱向滑動</text>
<switch
checked="{{switchVerticalStatus}}"
bind:change="switchVertical"
class="init-switch">
</switch>
</view>
</view>
<view class="card-area">
<view class="top-description border-bottom">
<view>滑塊切換時長</view>
<view>{{switchDuration}}ms</view>
</view>
<slider
class="slider"
min="300"
max="1500"
value="{{switchDuration}}"
bind:change="changeSwitchDuration">
</slider>
</view>
<view class="card-area">
<view class="top-description border-bottom">
<view>自動切換時間間隔</view>
<view>{{autoPlayInterval}}ms</view>
</view>
<slider
class="slider"
min="1000"
max="5000"
value="{{autoPlayInterval}}"
bind:change="changeAutoPlayInterval">
</slider>
</view>
</view>
Page({
data: {
items: [
{
className: 'color-a',
value: 'A'
}, {
className: 'color-b',
value: 'B'
}, {
className: 'color-c',
value: 'C'
}
],
current: 0,
itemId: 0,
switchIndicateStatus: true,
switchAutoPlayStatus: false,
switchVerticalStatus: false,
switchDuration: 500,
autoPlayInterval: 2000
},
swiperChange(e) {
console.log('swiperChange:', e.detail);
this.setData({
itemId: e.detail.current
});
},
switchIndicate() {
this.setData({
switchIndicateStatus: !this.getData('switchIndicateStatus')
});
},
switchVertical() {
this.setData({
switchVerticalStatus: !this.getData('switchVerticalStatus')
});
},
switchAutoPlay() {
this.setData({
switchAutoPlayStatus: !this.getData('switchAutoPlayStatus')
});
},
changeSwitchDuration(e) {
this.setData({
switchDuration: e.detail.value
});
},
changeAutoPlayInterval(e) {
this.setData({
autoPlayInterval: e.detail.value
});
},
animationfinish(e) {
console.log(e.type);
}
});
.swiper {
height: 2.18rem;
border-radius: 8px 8px 0 0;
}
.item {
width: 100%;
height: 2.18rem;
font-size: .18rem;
color: #fff;
text-align: center;
line-height: 2.18rem;
}
.slider {
margin: .3rem .23rem;
}
.switch-text-before {
margin-top: .17rem;
}
.init-switch {
vertical-align: middle;
margin: .14rem 0 .14rem .17rem;
}
.color-a {
background-color: #5B9FFF;
}
.color-b {
background-color: #85B8FF;
}
.color-c {
background-color: #ADCFFF;
}
.switch {
position: absolute;
top: 50%;
right: 0;
transform: translateY(-50%);
}
設(shè)計指南
建議滑塊視圖數(shù)量控制在 2-5 個。
建議滑塊切換時長不低于 500ms,自動切換時間間隔不高于 5000ms。
參考示例 1:用于實現(xiàn)頂部標簽切換
<view class="container">
<!-- 頂部導航 -->
<view class="swiper-tab">
<view class="tab-item {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swiperNav">Tab1</view>
<view class="tab-item {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swiperNav">Tab2</view>
<view class="tab-item {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swiperNav">Tab3</view>
</view>
<!-- 頂部導航對應(yīng)的內(nèi)容 -->
<swiper class="swiper" current="{{currentTab}}" duration="200" bindchange="swiperChange">
<swiper-item>
<!-- 頁面可使用自定義組件編寫 -->
<view>我是tab1</view>
</swiper-item>
<swiper-item>
<view>我是tab2</view>
</swiper-item>
<swiper-item>
<view>我是tab3</view>
</swiper-item>
</swiper>
</view>
Page({
data: {
currentTab: 0,
},
swiperNav(e) {
console.log(e);
if (this.data.currentTab === e.target.dataset.current) {
return false;
} else {
this.setData({
currentTab: e.target.dataset.current,
})
}
},
swiperChange: function (e) {
console.log(e);
this.setData({
currentTab: e.detail.current,
})
}
})
.swiper-tab {
display: flex;
flex-direction: row;
line-height: 80rpx;
border-bottom: 1rpx solid #f5f5f5;
}
.tab-item {
width: 33.3%;
text-align: center;
font-size: .16rem;
color: rgb(116, 113, 113);
}
.swiper {
height: 1100px;
background: #dfdfdf;
}
.on {
color: #5B9FFF;
border-bottom: 1px solid #5B9FFF;
padding-bottom: 2px
}
參考示例 2: 自定義底部切換圓點
<view class="swiper-wrap">
<swiper autoplay="auto" interval="5000" duration="500" current="{{swiperCurrent}}" bindchange="swiperChange" class="swiper">
<swiper-item s-for="{{slider}}">
<image src="{{item.imageUrl}}" class="img"></image>
</swiper-item>
</swiper>
<view class="dots">
<view s-for="{{slider}}" class="dot {{index == swiperCurrent ? ' active' : ''}}"></view>
</view>
</view>
Page({
data: {
slider: [{
imageUrl: 'https://b.bdstatic.com/miniapp/image/hypnosis.jpeg'
}, {
imageUrl: 'https://b.bdstatic.com/miniapp/images/hypnosis.jpeg'
}, {
imageUrl: 'https://b.bdstatic.com/miniapp/image/airCaptain.jpeg'
}],
swiperCurrent: 0
},
swiperChange(e) {
this.setData({
swiperCurrent: e.detail.current
})
}
})
.swiper-wrap{
position: relative;
}
.swiper-wrap .swiper{
height: 300rpx;
}
.swiper-wrap .swiper .img{
width: 100%;
height: 100%;
}
.swiper-wrap .dots{
position: absolute;
right: 20rpx;
bottom: 20rpx;
display: flex;
justify-content: center;
}
.swiper-wrap .dots .dot{
margin: 0 8rpx;
width: 14rpx;
height: 14rpx;
background: #fff;
border-radius: 8rpx;
transition: all .6s;
}
.swiper-wrap .dots .dot.active{
width: 24rpx;
background: #38f;
}
A: 參見上面屬性說明,可以去 dot 顯示之后,自己定義 dot 樣式。
代碼示例: 參考示例 2
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: