Skip to content

Commit 174973e

Browse files
committed
fix(Swiper): offsetWidth error
1 parent 5073551 commit 174973e

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/swiper/swiper-item.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ const swiperItemProps = {
2323
type: Boolean,
2424
default: false,
2525
},
26-
getWrapAttribute: {
27-
type: Function,
26+
swiperWidth: {
27+
type: Number,
28+
default: 0,
2829
},
2930
swiperItemLength: {
3031
type: Number,
@@ -63,18 +64,19 @@ export default mixins(classPrefixMixins).extend({
6364
},
6465
translateX(): number {
6566
if (this.type !== 'card') return 0;
66-
const wrapWidth = this.getWrapAttribute('offsetWidth');
67+
const { swiperWidth } = this;
6768
const translateIndex = !this.active && this.swiperItemLength > 2 ? this.disposeIndex : this.index;
6869
const inStage = Math.abs(translateIndex - this.currentIndex) <= 1;
6970
if (inStage) {
7071
return (
71-
(wrapWidth * ((translateIndex - this.currentIndex) * (1 - ITEM_WIDTH * this.cardScale) - ITEM_WIDTH + 1)) / 2
72+
(swiperWidth * ((translateIndex - this.currentIndex) * (1 - ITEM_WIDTH * this.cardScale) - ITEM_WIDTH + 1))
73+
/ 2
7274
);
7375
}
7476
if (translateIndex < this.currentIndex) {
75-
return (-ITEM_WIDTH * (1 + this.cardScale) * wrapWidth) / 2;
77+
return (-ITEM_WIDTH * (1 + this.cardScale) * swiperWidth) / 2;
7678
}
77-
return ((2 + ITEM_WIDTH * (this.cardScale - 1)) * wrapWidth) / 2;
79+
return ((2 + ITEM_WIDTH * (this.cardScale - 1)) * swiperWidth) / 2;
7880
},
7981
zIndex(): number {
8082
if (this.type !== 'card') return 0;

src/swiper/swiper.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { kebabCase } from 'lodash-es';
44

55
import { getClassPrefixMixins, getGlobalIconMixins } from '../config-provider/config-receiver';
66
import { isVNode } from '../hooks/render-tnode';
7+
78
import { emitEvent } from '../utils/event';
89
import mixins from '../utils/mixins';
910
import { renderTNodeJSX } from '../utils/render-tnode';
@@ -43,6 +44,7 @@ export default mixins(Vue as VueConstructor<SwiperVue>, classPrefixMixins, getGl
4344
isHovering: false,
4445
isSwitching: false,
4546
swiperItemList: [] as Array<VNodeComponentOptions>,
47+
swiperOffset: { width: 0, height: 0 },
4648
showArrow: false,
4749
};
4850
},
@@ -77,7 +79,7 @@ export default mixins(Vue as VueConstructor<SwiperVue>, classPrefixMixins, getGl
7779
};
7880
},
7981
containerStyle(): any {
80-
const offsetHeight = this.height ? `${this.height}px` : `${this.getWrapAttribute('offsetHeight')}px`;
82+
const offsetHeight = this.height ? `${this.height}px` : `${this.swiperOffset.height}px`;
8183
if (this.type === 'card' || this.animation === 'fade') {
8284
return {
8385
height: offsetHeight,
@@ -107,8 +109,8 @@ export default mixins(Vue as VueConstructor<SwiperVue>, classPrefixMixins, getGl
107109
currentIndex={this.currentIndex}
108110
isSwitching={this.isSwitching}
109111
cardScale={this.cardScale}
110-
getWrapAttribute={this.getWrapAttribute}
111112
swiperItemLength={this.swiperItemLength}
113+
swiperWidth={this.swiperOffset.width}
112114
props={{ ...this.$props, ...swiperItem.propsData }}
113115
>
114116
{swiperItem.children}
@@ -142,6 +144,17 @@ export default mixins(Vue as VueConstructor<SwiperVue>, classPrefixMixins, getGl
142144
this.updateSwiperItems();
143145
this.setTimer();
144146
this.showArrow = this.navigationConfig.showSlideBtn === 'always';
147+
148+
const resizeObserver = new ResizeObserver(([entry]) => {
149+
const parent = entry.target.parentNode as HTMLElement;
150+
if (parent) {
151+
this.swiperOffset = {
152+
width: parent.offsetWidth,
153+
height: parent.offsetHeight,
154+
};
155+
}
156+
});
157+
resizeObserver.observe(this.$refs.swiperWrap as HTMLElement);
145158
},
146159

147160
updated() {
@@ -226,10 +239,6 @@ export default mixins(Vue as VueConstructor<SwiperVue>, classPrefixMixins, getGl
226239
}
227240
return this.swiperTo(this.currentIndex - 1, context);
228241
},
229-
getWrapAttribute(attr: string) {
230-
const parent = (this.$refs.swiperWrap as Element)?.parentNode as HTMLElement;
231-
return parent?.[attr as keyof HTMLElement];
232-
},
233242
renderPagination() {
234243
const fractionIndex = this.currentIndex + 1 > this.swiperItemLength ? 1 : this.currentIndex + 1;
235244
const { ChevronLeftIcon, ChevronRightIcon } = this.useGlobalIcon({

0 commit comments

Comments
 (0)