diff --git a/dist/tabs/index.js b/dist/tabs/index.js index b602278dc..dbb509ff4 100644 --- a/dist/tabs/index.js +++ b/dist/tabs/index.js @@ -223,9 +223,11 @@ VantComponent({ const offsetLeft = tabRects .slice(0, currentIndex) .reduce((prev, curr) => prev + curr.width, 0); - this.setData({ - scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2, - }); + if (navRect && (navRect === null || navRect === void 0 ? void 0 : navRect.width) && tabRect && (tabRect === null || tabRect === void 0 ? void 0 : tabRect.width)) { + this.setData({ + scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2, + }); + } if (!scrollWithAnimation) { nextTick(() => { this.setData({ scrollWithAnimation: true }); diff --git a/lib/tabs/index.js b/lib/tabs/index.js index e16ace9d0..1613c97f2 100644 --- a/lib/tabs/index.js +++ b/lib/tabs/index.js @@ -246,9 +246,11 @@ var relation_1 = require("../common/relation"); var offsetLeft = tabRects .slice(0, currentIndex) .reduce(function (prev, curr) { return prev + curr.width; }, 0); - _this.setData({ - scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2, - }); + if (navRect && (navRect === null || navRect === void 0 ? void 0 : navRect.width) && tabRect && (tabRect === null || tabRect === void 0 ? void 0 : tabRect.width)) { + _this.setData({ + scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2, + }); + } if (!scrollWithAnimation) { (0, utils_1.nextTick)(function () { _this.setData({ scrollWithAnimation: true }); diff --git a/packages/tabs/index.ts b/packages/tabs/index.ts index 0af7cd7b0..476cf3af0 100644 --- a/packages/tabs/index.ts +++ b/packages/tabs/index.ts @@ -277,9 +277,11 @@ VantComponent({ .slice(0, currentIndex) .reduce((prev, curr) => prev + curr.width, 0); - this.setData({ - scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2, - }); + if (navRect && navRect?.width && tabRect && tabRect?.width) { + this.setData({ + scrollLeft: offsetLeft - (navRect.width - tabRect.width) / 2, + }); + } if (!scrollWithAnimation) { nextTick(() => {