Skip to content

Commit 443fd8a

Browse files
jasper-opsMoonofweisheng
authored andcommitted
feat: ✨ swiper组件增加current属性控制轮播项功能
Closes: #211
1 parent 6a4af41 commit 443fd8a

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

docs/component/swiper.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,33 @@ function onChange(e) {
184184
}
185185
```
186186

187+
## 属性控制切换
188+
```html
189+
<wd-swiper :loop="isLoop" :autoplay="false" :list="swiperList" v-model:current="current" />
190+
<wd-gap />
191+
<wd-cell-group>
192+
<wd-cell title="loop">
193+
<wd-switch v-model="isLoop" size="24px" />
194+
</wd-cell>
195+
<wd-cell title="current" :value="current.toString()" />
196+
</wd-cell-group>
197+
<view style="display: flex; justify-content: space-between">
198+
<wd-button @click="current--">prev</wd-button>
199+
<wd-button type="success" @click="current++">next</wd-button>
200+
</view>
201+
```
202+
203+
```javascript
204+
const current = ref<number>(0)
205+
const isLoop = ref(false)
206+
```
207+
187208
## Attributes
188209

189210
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
190211
| -------------------- | ------------------------------------------------------ | ------------------------- | ---------- | ---------- | -------- |
191212
| autoplay | 是否自动播放 | `boolean` | - | true | 0.1.22 |
192-
| current | 当前轮播在哪一项(下标) | `number` | - | 0 | 0.1.22 |
213+
| v-model:current | 控制当前轮播在哪一项(下标) | `number` | - | 0 | 0.1.22 |
193214
| direction | 轮播滑动方向 | `DirectionType` | `horizontal, vertical` | horizontal | 0.1.22 |
194215
| displayMultipleItems | 同时显示的滑块数量 | `number` | - | 1 | 0.1.22 |
195216
| duration | 滑动动画时长 | `number` | - | 300 | 0.1.22 |
@@ -226,15 +247,15 @@ function onChange(e) {
226247
### SwiperIndicatorProps
227248

228249
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
229-
| ------------------- | -------------------------- | ---------------------- | ------------------------------------------------------------------------ | ---------- | -------- |
250+
| ------------------- | -------------------------- | ---------------------- | ------------------------------------------------------------------------ | ---------- | -------- |
230251
| current | 当前轮播在哪一项(下标) | Number | - | 0 | 0.1.22 |
231252
| direction | 轮播滑动方向 | DirectionType | `horizontal, vertical` | horizontal | 0.1.22 |
232253
| min-show-num | 小于这个数字不会显示导航器 | Number | - | 2 | 0.1.22 |
233254
| pagination-position | 页码信息展示位置 | IndicatorPositionType | `left, top-left, top, top-right, bottom-left, bottom, bottom-right, right` | bottom | 0.1.22 |
234255
| show-controls | 是否显示控制按钮 | Boolean | - | false | 0.1.22 |
235256
| total | 总共的项数 | Number | - | 0 | 0.1.22 |
236257
| type | 导航器类型 | SwiperIndicatorType | `dots, dots-bar, fraction ` | dots | 0.1.22 |
237-
| autoplay | 是否自动播放 | boolean | - | true | 0.1.22 |
258+
| autoplay | 是否自动播放 | boolean | - | true | 0.1.22 |
238259

239260
## Events
240261

src/pages/swiper/Index.vue

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@
8787
</template>
8888
</wd-swiper>
8989
</demo-block>
90+
91+
<demo-block title="属性控制切换">
92+
<wd-swiper :loop="isLoop" :autoplay="false" :list="swiperList" v-model:current="current" />
93+
<wd-gap />
94+
<wd-cell-group>
95+
<wd-cell title="loop">
96+
<wd-switch v-model="isLoop" size="24px" />
97+
</wd-cell>
98+
<wd-cell title="current" :value="current.toString()" />
99+
</wd-cell-group>
100+
<view style="display: flex; justify-content: space-between">
101+
<wd-button @click="current--">prev</wd-button>
102+
103+
<wd-button type="success" @click="current++">next</wd-button>
104+
</view>
105+
</demo-block>
90106
</page-wraper>
91107
</template>
92108
<script lang="ts" setup>
@@ -99,6 +115,10 @@ const swiperList = ref([
99115
'https://img.yzcdn.cn/vant/cat.jpeg',
100116
'https://unpkg.com/wot-design-uni-assets/meng.jpg'
101117
])
118+
119+
const current = ref<number>(0)
120+
const isLoop = ref(false)
121+
102122
function handleClick(e: any) {
103123
console.log(e)
104124
}

src/uni_modules/wot-design-uni/components/wd-swiper/wd-swiper.vue

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,29 @@
4646
</template>
4747

4848
<script lang="ts" setup>
49-
import { computed, onBeforeMount, ref } from 'vue'
49+
import { computed, watch, ref } from 'vue'
5050
import { addUnit, isObj } from '../common/util'
5151
import { swiperProps, type SwiperList } from './types'
5252
import type { SwiperNavProps } from '../wd-swiper-nav/types'
5353
54+
const emit = defineEmits(['click', 'change', 'animationfinish', 'update:current'])
5455
const props = defineProps(swiperProps)
5556
const navCurrent = ref<number>(0) // 当前滑块
5657
57-
onBeforeMount(() => {
58-
navCurrent.value = props.current
59-
})
58+
watch(
59+
() => props.current,
60+
(val) => {
61+
if (val < 0) {
62+
props.loop ? goToEnd() : goToStart()
63+
} else if (val >= props.list.length) {
64+
props.loop ? goToStart() : goToEnd()
65+
} else {
66+
go(val)
67+
}
68+
emit('update:current', navCurrent.value)
69+
},
70+
{ immediate: true }
71+
)
6072
6173
const swiperIndicator = computed(() => {
6274
const { list, direction, indicatorPosition, indicator } = props
@@ -74,7 +86,17 @@ const swiperIndicator = computed(() => {
7486
return swiperIndicator
7587
})
7688
77-
const emit = defineEmits(['click', 'change', 'animationfinish'])
89+
function go(index: number) {
90+
navCurrent.value = index
91+
}
92+
93+
function goToStart() {
94+
navCurrent.value = 0
95+
}
96+
97+
function goToEnd() {
98+
navCurrent.value = props.list.length - 1
99+
}
78100
79101
/**
80102
* 是否为当前滑块的前一个滑块

0 commit comments

Comments
 (0)