Skip to content

Commit c378497

Browse files
authored
fix: 修复swiper在item数量太少时无法循环的问题 (#16737)
* fix: 修复swiper在item数量太少时无法循环的问题 * test: update sanpshoot
1 parent acaf859 commit c378497

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

packages/taro-components-react/src/components/swiper/index.tsx

+2-6
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class SwiperInner extends React.Component<SwiperProps, SwiperState> {
172172
}
173173

174174
const loopAdditionalSlides = this.getLoopAdditionalSlides()
175-
const centeredSlides = parseFloat(String(displayMultipleItems)) === 1
175+
const centeredSlides = displayMultipleItems === 1 && this.getNeedFixLoop()
176176
const slidesPerView = parseFloat(String(displayMultipleItems)) === 1 ? 'auto' : displayMultipleItems
177177
// eslint-disable-next-line @typescript-eslint/no-this-alias
178178
const that = this
@@ -461,11 +461,7 @@ class SwiperInner extends React.Component<SwiperProps, SwiperState> {
461461
const defaultIndicatorActiveColor = indicatorActiveColor || '#000'
462462
const [pM, nM] = this.parseMargin()
463463
const cls = classNames(`taro-swiper-${this._id}`, className)
464-
const sty = Object.assign({}, style)
465-
const hasMargin = pM || nM
466-
if (hasMargin) {
467-
sty.overflow = 'hidden'
468-
}
464+
const sty = Object.assign({ overflow: 'hidden' }, style)
469465
if (this.props.full) {
470466
sty.height = '100%'
471467
}

packages/taro-components/__tests__/__snapshots__/swiper.spec.tsx.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`Swiper events 1`] = `
4-
<taro-swiper-core class="taro-swiper-6">
4+
<taro-swiper-core class="taro-swiper-6" style="overflow: hidden;">
55
<!---->
66
<div class="swiper-container">
77
<style type="text/css">
@@ -30,7 +30,7 @@ exports[`Swiper events 1`] = `
3030
`;
3131

3232
exports[`Swiper props 1`] = `
33-
<taro-swiper-core class="taro-swiper-0">
33+
<taro-swiper-core class="taro-swiper-0" style="overflow: hidden;">
3434
<!---->
3535
<div class="swiper-container">
3636
<style type="text/css">
@@ -59,7 +59,7 @@ exports[`Swiper props 1`] = `
5959
`;
6060

6161
exports[`Swiper should autoplay 1`] = `
62-
<taro-swiper-core class="taro-swiper-1">
62+
<taro-swiper-core class="taro-swiper-1" style="overflow: hidden;">
6363
<!---->
6464
<div class="swiper-container">
6565
<style type="text/css">
@@ -88,7 +88,7 @@ exports[`Swiper should autoplay 1`] = `
8888
`;
8989

9090
exports[`Swiper should be circular 1`] = `
91-
<taro-swiper-core class="taro-swiper-2">
91+
<taro-swiper-core class="taro-swiper-2" style="overflow: hidden;">
9292
<!---->
9393
<div class="swiper-container">
9494
<style type="text/css">
@@ -117,7 +117,7 @@ exports[`Swiper should be circular 1`] = `
117117
`;
118118

119119
exports[`Swiper should be vertical 1`] = `
120-
<taro-swiper-core class="taro-swiper-3">
120+
<taro-swiper-core class="taro-swiper-3" style="overflow: hidden;">
121121
<!---->
122122
<div class="swiper-container">
123123
<style type="text/css">
@@ -146,7 +146,7 @@ exports[`Swiper should be vertical 1`] = `
146146
`;
147147

148148
exports[`Swiper should display multi items within screen width 1`] = `
149-
<taro-swiper-core class="taro-swiper-5">
149+
<taro-swiper-core class="taro-swiper-5" style="overflow: hidden;">
150150
<!---->
151151
<div class="swiper-container">
152152
<style type="text/css">

packages/taro-components/src/components/swiper/swiper.tsx

+8-12
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export class Swiper implements ComponentInterface {
308308
}
309309
}
310310
const loopAdditionalSlides = this.getLoopAdditionalSlides()
311-
const centeredSlides = displayMultipleItems === 1
311+
const centeredSlides = displayMultipleItems === 1 && this.getNeedFixLoop()
312312
const slidesPerView = displayMultipleItems === 1 ? 'auto' : displayMultipleItems
313313
// eslint-disable-next-line @typescript-eslint/no-this-alias
314314
const that = this
@@ -458,26 +458,22 @@ export class Swiper implements ComponentInterface {
458458
indicatorActiveColor
459459
} = this
460460

461-
const [pM, nM] = this.parseMargin()
462-
const hasMargin = pM || nM
463-
const hostStyle: Record<string, string> = {}
464-
if(hasMargin) {
465-
hostStyle.overflow = 'hidden'
466-
}
467-
if (this.full) {
468-
hostStyle.height = '100%'
469-
}
470-
461+
const [pM, nM] = this.parseMargin()
471462
const swiperContainerStyleList: string [] = [
472463
'overflow: visible;',
473464
vertical ? `margin-top: ${pM}px; margin-bottom: ${nM}px;` : `margin-right: ${nM}px; margin-left: ${pM}px;`,
474465
this.full ? 'height: 100%;' : '',
475466
]
476-
477467
const swiperPaginationStyleList: string [] = [
478468
indicatorDots ? 'opacity: 1;' : 'display: none;',
479469
'font-size: 0;'
480470
]
471+
const hostStyle: Record<string, string> = { overflow: 'hidden' }
472+
473+
if (this.full) {
474+
hostStyle.height = '100%'
475+
}
476+
481477

482478
return (
483479
<Host class={`taro-swiper-${this.#id}`} style={hostStyle}>

0 commit comments

Comments
 (0)