W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Swiper組件提供滑動輪播顯示的能力。Swiper本身是一個容器組件,當(dāng)設(shè)置了多個子組件后,可以對這些子組件進行輪播顯示。通常,在一些應(yīng)用首頁顯示推薦的內(nèi)容時,需要用到輪播顯示的能力。
Swiper作為一個容器組件,在自身尺寸屬性未被設(shè)置時,會自動根據(jù)子組件的大小設(shè)置自身的尺寸。如果開發(fā)者對Swiper組件設(shè)置了固定的尺寸,則在輪播顯示過程中均以該尺寸生效;否則,在輪播過程中,會根據(jù)子組件的大小自動調(diào)整自身的尺寸。
通過loop屬性控制是否循環(huán)播放,該屬性默認(rèn)值為true。
當(dāng)loop為true時,在顯示第一頁或最后一頁時,可以繼續(xù)往前切換到前一頁或者往后切換到后一頁。如果loop為false,則在第一頁或最后一頁時,無法繼續(xù)向前或者向后切換頁面。
- ...
- private swiperController: SwiperController = new SwiperController()
- ...
- Swiper(this.swiperController) {
- Text("0")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Gray)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("1")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Green)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("2")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Blue)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- }
- .loop(true)
- Swiper(this.swiperController) {
- Text("0")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Gray)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("1")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Green)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("2")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Blue)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- }
- .loop(false)
Swiper通過設(shè)置autoPlay屬性,控制是否自動輪播子組件。該屬性默認(rèn)值為false。
autoPlay為true時,會自動切換播放子組件,子組件與子組件之間的播放間隔通過interval屬性設(shè)置。interval屬性默認(rèn)值為3000,單位毫秒。
- Swiper(this.swiperController) {
- Text("0")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Gray)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("1")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Green)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("2")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Pink)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- }
- .loop(true)
- .autoPlay(true)
- .interval(1000)
Swiper提供了默認(rèn)的導(dǎo)航點樣式,導(dǎo)航點默認(rèn)顯示在Swiper下方居中位置,開發(fā)者也可以通過indicatorStyle屬性自定義導(dǎo)航點的位置和樣式。
通過indicatorStyle屬性,開發(fā)者可以設(shè)置導(dǎo)航點相對于Swiper組件上下左右四個方位的位置,同時也可以設(shè)置每個導(dǎo)航點的尺寸、顏色、蒙層和被選中導(dǎo)航點的顏色。
- Swiper(this.swiperController) {
- Text("0")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Gray)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("1")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Green)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("2")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Pink)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- }
- Swiper(this.swiperController) {
- Text("0")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Gray)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("1")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Green)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("2")
- .width('90%')
- .height('100%')
- .backgroundColor(Color.Pink)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- }
- .indicatorStyle({
- size: 30,
- left: 0,
- color: Color.Red
- })
Swiper支持三種頁面切換方式:手指滑動、點擊導(dǎo)航點和通過控制器。
- @Entry
- @Component
- struct SwiperDemo {
- private swiperController: SwiperController = new SwiperController();
- build() {
- Column({ space: 5 }) {
- Swiper(this.swiperController) {
- Text("0")
- .width(250)
- .height(250)
- .backgroundColor(Color.Gray)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("1")
- .width(250)
- .height(250)
- .backgroundColor(Color.Green)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("2")
- .width(250)
- .height(250)
- .backgroundColor(Color.Pink)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- }
- .indicator(true)
- Row({ space: 12 }) {
- Button('showNext')
- .onClick(() => {
- this.swiperController.showNext(); // 通過controller切換到后一頁
- })
- Button('showPrevious')
- .onClick(() => {
- this.swiperController.showPrevious(); // 通過controller切換到前一頁
- })
- }.margin(5)
- }.width('100%')
- .margin({ top: 5 })
- }
- }
Swiper支持水平和垂直方向上進行輪播,主要通過vertical屬性控制。
當(dāng)vertical為true時,表示在垂直方向上進行輪播;為false時,表示在水平方向上進行輪播。vertical默認(rèn)值為false。
- Swiper(this.swiperController) {
- ...
- }
- .indicator(true)
- .vertical(false)
- Swiper(this.swiperController) {
- ...
- }
- .indicator(true)
- .vertical(true)
Swiper支持在一個頁面內(nèi)同時顯示多個子組件,通過displayCount屬性設(shè)置。
- Swiper(this.swiperController) {
- Text("0")
- .width(250)
- .height(250)
- .backgroundColor(Color.Gray)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("1")
- .width(250)
- .height(250)
- .backgroundColor(Color.Green)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("2")
- .width(250)
- .height(250)
- .backgroundColor(Color.Pink)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- Text("3")
- .width(250)
- .height(250)
- .backgroundColor(Color.Blue)
- .textAlign(TextAlign.Center)
- .fontSize(30)
- }
- .indicator(true)
- .displayCount(2)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: