Skip to content

Commit

Permalink
fix: 修复swiper在item数量太少时无法循环的问题 (#16737)
Browse files Browse the repository at this point in the history
* fix: 修复swiper在item数量太少时无法循环的问题

* test: update sanpshoot
  • Loading branch information
ZEJIA-LIU authored Oct 25, 2024
1 parent acaf859 commit c378497
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class SwiperInner extends React.Component<SwiperProps, SwiperState> {
}

const loopAdditionalSlides = this.getLoopAdditionalSlides()
const centeredSlides = parseFloat(String(displayMultipleItems)) === 1
const centeredSlides = displayMultipleItems === 1 && this.getNeedFixLoop()
const slidesPerView = parseFloat(String(displayMultipleItems)) === 1 ? 'auto' : displayMultipleItems
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this
Expand Down Expand Up @@ -461,11 +461,7 @@ class SwiperInner extends React.Component<SwiperProps, SwiperState> {
const defaultIndicatorActiveColor = indicatorActiveColor || '#000'
const [pM, nM] = this.parseMargin()
const cls = classNames(`taro-swiper-${this._id}`, className)
const sty = Object.assign({}, style)
const hasMargin = pM || nM
if (hasMargin) {
sty.overflow = 'hidden'
}
const sty = Object.assign({ overflow: 'hidden' }, style)
if (this.props.full) {
sty.height = '100%'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Swiper events 1`] = `
<taro-swiper-core class="taro-swiper-6">
<taro-swiper-core class="taro-swiper-6" style="overflow: hidden;">
<!---->
<div class="swiper-container">
<style type="text/css">
Expand Down Expand Up @@ -30,7 +30,7 @@ exports[`Swiper events 1`] = `
`;

exports[`Swiper props 1`] = `
<taro-swiper-core class="taro-swiper-0">
<taro-swiper-core class="taro-swiper-0" style="overflow: hidden;">
<!---->
<div class="swiper-container">
<style type="text/css">
Expand Down Expand Up @@ -59,7 +59,7 @@ exports[`Swiper props 1`] = `
`;

exports[`Swiper should autoplay 1`] = `
<taro-swiper-core class="taro-swiper-1">
<taro-swiper-core class="taro-swiper-1" style="overflow: hidden;">
<!---->
<div class="swiper-container">
<style type="text/css">
Expand Down Expand Up @@ -88,7 +88,7 @@ exports[`Swiper should autoplay 1`] = `
`;

exports[`Swiper should be circular 1`] = `
<taro-swiper-core class="taro-swiper-2">
<taro-swiper-core class="taro-swiper-2" style="overflow: hidden;">
<!---->
<div class="swiper-container">
<style type="text/css">
Expand Down Expand Up @@ -117,7 +117,7 @@ exports[`Swiper should be circular 1`] = `
`;

exports[`Swiper should be vertical 1`] = `
<taro-swiper-core class="taro-swiper-3">
<taro-swiper-core class="taro-swiper-3" style="overflow: hidden;">
<!---->
<div class="swiper-container">
<style type="text/css">
Expand Down Expand Up @@ -146,7 +146,7 @@ exports[`Swiper should be vertical 1`] = `
`;

exports[`Swiper should display multi items within screen width 1`] = `
<taro-swiper-core class="taro-swiper-5">
<taro-swiper-core class="taro-swiper-5" style="overflow: hidden;">
<!---->
<div class="swiper-container">
<style type="text/css">
Expand Down
20 changes: 8 additions & 12 deletions packages/taro-components/src/components/swiper/swiper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export class Swiper implements ComponentInterface {
}
}
const loopAdditionalSlides = this.getLoopAdditionalSlides()
const centeredSlides = displayMultipleItems === 1
const centeredSlides = displayMultipleItems === 1 && this.getNeedFixLoop()
const slidesPerView = displayMultipleItems === 1 ? 'auto' : displayMultipleItems
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this
Expand Down Expand Up @@ -458,26 +458,22 @@ export class Swiper implements ComponentInterface {
indicatorActiveColor
} = this

const [pM, nM] = this.parseMargin()
const hasMargin = pM || nM
const hostStyle: Record<string, string> = {}
if(hasMargin) {
hostStyle.overflow = 'hidden'
}
if (this.full) {
hostStyle.height = '100%'
}

const [pM, nM] = this.parseMargin()
const swiperContainerStyleList: string [] = [
'overflow: visible;',
vertical ? `margin-top: ${pM}px; margin-bottom: ${nM}px;` : `margin-right: ${nM}px; margin-left: ${pM}px;`,
this.full ? 'height: 100%;' : '',
]

const swiperPaginationStyleList: string [] = [
indicatorDots ? 'opacity: 1;' : 'display: none;',
'font-size: 0;'
]
const hostStyle: Record<string, string> = { overflow: 'hidden' }

if (this.full) {
hostStyle.height = '100%'
}


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

0 comments on commit c378497

Please sign in to comment.